Week 3 - Solving second order ODEs
Aim-To solve second order ordinary differential equation using matlab
Objectives- To solve the second order ODE with graph of velocity v/s displacement
Data- L= 1 meter
t=0
M=1 kg
b=0.05
g=9.81 m/s^2
Program-
1. code function of ODE45 for main program-
function[dtheta_dt]=ode_func(t,theta,b,g,l,m)
theta1=theta(1)
theta2=theta(2)
dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end
2. Main program-
clear all
close all
clc
b=0.05;
g=9.81;
l=1;
m=0.1
%initial condition
theta_0=[0;5];
%time points
t_span=linspace(0,20,500);
% to solve ode
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('time')
xlabel('velocity')
legend('Displacement','Anglular velocity')
grid on
hold off
displacement=results(:,1);
velocity=results(:,2);
%fixed coordinates
x0=0;
y0=0;
ct=1;
for i = 1:length(displacement)
%coordinates
x1=1*sin(displacement(i));
y1=-1*cos(displacement(i));
figure(2)
%plotfixed link
plot([-2 2],[0 0],'linewidth',15,'color','b')
hold on
plot([x0 x1],[y0 y1],'linewidth',4)
plot(x1,y1,'-o','markersize',20,'markerfacecolor','b')
%axisetting
axis([-2 2 -2 2])
% to chnage the frame movie
M(ct)=getframe(gcf);
ct=ct+1;
hold off
end
movie(M)
videofile=VideoWriter('simple pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Results-

Postion of the setup-

Animation-