Week 3 - Solving second order ODEs
Matlab program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping.
`(d^2theta)/dt^2+b/m (d theta)/dt+g/Lsin theta=0`
here
g=gravity=9.81 meter/sec^2
`b=damping cofficient=0.05`
m=mass =1 Kg
L=length=1 meter
Matlab code that solves this equation and create the animation of simple pendulam motion
clear all
close all
clc
%inputs
b=0.05;
g=9.81;
l=1;
m=1;
%initial conditions
%at t=0 angular displacement=0 and angular velocity=3
theta_0=[0;3];
%Time points
t=linspace(0,20,150);
%solve ODE
[t,result]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t,theta_0)
%plotting of the angular displacement and angular velocity
plot(t,result(:,1))
hold on
plot(t,result(:,2))
%labelling
xlabel('time')
ylabel('plot')
legend('angular displacement','angular velocity')
%Animation of pendulam motion
%counter value
ct=1;
%initial point of rod
x0=0;
y0=0;
for i=1:length(t)
x=l*sin(result(i,1));
y=-l*cos(result(i,1));
figure(2)
plot([-1 1],[0 0],'linewidth',6,'color','g')
line([x0,x],[y0,y],'linewidth',3,'color','b')
hold on
plot(x,y,'o','markersize',15,'markerfacecolor','r')
axis([-1.5,1.5,-1.5,1.5]);
hold off
M(ct)=getframe(gcf); %create a frame of all the plotes
ct=ct+1;
end
%creation of movie
movie(M)
videofile=VideoWriter('pendulam_avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
Outputs
the plot of the angular diplacement and velocity with respect of time is given

the link of the created animation is given below
https://drive.google.com/file/d/1TaR1DcO5GZ9zTINIPLAsv4jsXbZCLFcX/view?usp=sharing