Challenge

Week 3 - Solving second order ODEs

Aim

To solving 2nd order ODE in matlab

D^theta/dt^2+b/m*dtheta/dt+g/l*sintheta =0

b-damping coefficient

m-mass in kg

g- acceleration of due to gravity

l- length of pendulum

d*theta/dt=dtheta1/dt=theta2

d^2theta/dt^2 = d^2theta1/dt^2 = d/dt(d*theta1/dt)= dtheta2

d^2theta/dt^2+b/m(dtheta1/dt)+g/l*sin(theta1)=0

dtheta2/dt =-b/m (theta1)-g/l*sin(theta1)

d/dt[theta1_theta2]= [theta2­_(-b/m(theta2)-g/l*sin(theta1)]

get from given datas

g=9.81 ms^2

b=0.05

l=1m

m=1kg

theta_0=[0;3];

initial condition of theta is been mentioned array where are two diff values and respenting the angular displacement and angular velocity and respectively in colum vector

t_span = linspace(0,10,500);

time interval from 0-10sec with 500 values and have used linspace command.

[t_result]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);

ODE calculation used ode45 are matlab to slove defferential equation. Another function separately name ode_func.

%code of solve pendulum transient
clear all
close alll
clc
g=9.81;
l=1;
b=0.05;
m=1;
%initial condition
theta_0=[0:3];
%time point
t_spen=linspace(0,10,500);
%ODE
[t,result]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_o);
%plotting
plot(t,result(:,1),'color','g')
hold on
plot(t,reult(:,2),'color','b')
xlabel('time')
ylabel('angular displacement & velocity')
legend('change in angular displacement;changein angulaar velocity')
hold off
%animation pendulum
ct=1;
i=1:length(result(:,1)
x0=0;
y0=0;
x1=1*sin(result(i,1);
y1=-1*cos(result(i,1);
plot([-1 1],[0 0],'linewidth,'4','color','k')
hold on
plot([x0 x1],[y0 y1],'linewidth','1',color,'b')
hold on
plot(x1,y1,'o',marker's,'20','markerfacecolor','r')
hold off
axis([-2 2 -2 2])
pause(0.003)
m(ct)=detframe(gcf);
ct=ct=1;
end
moive(M)

videofile=videowriter('2nd_order_ODE_simple_pendulum.avi,'uncompressed AVI');
funtion[dtheta_dt]=ode_func(t,theta,b,g,l,m)
open(video file) write video(video file ,M)
close(video file)




Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.