Week 3 - Solving second order ODEs
AIM
To write code for the digerential eqation of simple pendulum and getting the vedio of simple pendulum damping animation
Aprroch
like in general mathematics
we cut down the higher oder differntial eqation
`(d^2 theta)/dt+b/m((d theta)/dt)+g/lsin theta=0`
`theta =theta 1(theta q=1 is velocity)`
`d/dt((d theta 1)/dt)+b/m ((d theta 1)/dt)+g/l sin theta 1=0`
`(d theta 1)/dt=theta 2(theta 2 is displacement)`
`(d theta2)/dt=-b/m(theta 2)-g/l sin theta1`
i wrote all this eaqtion as a code that repeats for the length of t _span using ode 45 fuction
Result and code
clear all
close all
clc
%Damping_constants
l=1;%length
m=1;%mass of bob
b=0.05;%damping constants
g=9.81;%gravitational force
%%
%intial condtions ,the velocticy is zero and dispacement is 5
theta_0=[0;3];
%time
t_span=linspace(0,20,500);
%sloving of ode ,ode is a solver to solve the higher order different
%eqations with in a single line of code
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
% t and results are independent and dependent variables
%results contain two coloumns 1st is the theta1 angular velocity w.r.t time
%2nd is the displacemt w.r.t time (theta 2)
figure (1)
plot(t,results(:,1),'color','y','linewidth',1.5);
hold on
plot(t,results(:,2),'color','black','linewidth',1.5);
xlabel('time');
ylabel('plot');
figure(2)
subplot(2,1,1)
plot(t,results(:,1),'color','y','linewidth',1.5);
xlabel('time');
ylabel('angular velocity');
subplot(2,1,2)
plot(t,results(:,2),'color','black','linewidth',1.5);
xlabel('time');
ylabel('angular displacement');
%%
%animation
X0=0;
Y0=0;
ct=1;
for i=1:length(t_span)
X1=l*sin(results(i,1));
Y1=-l*cos(results(i,1));
figure(3)
plot([-0.5,0.5],[0,0],'linewidth',6,'color','y');
axis([-2,2,-2,2]);
line([X0,X1],[Y0,Y1],'linewidth',2,'color','b');
hold on
plot(X0,Y0,'O','markersize',3,'markerfacecolor','g');
plot(X1,Y1,'O','markersize',20,'markerfacecolor','r');
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter("damping of simple pendulum.avi",'uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
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
%%
%here i crated a template of function with the name called ode_func
%dtheta_dt is the solution vector having two components velocity and
%displacement as thetha 1 and theta 2
%here we cut down the higer order difeerential equation
ERRORS
- i faced lot of syntax errors
- by reading the error statement i over come all those
- after writing the plot command for damping of pendulum animation the command reperensts all the 500 pendulum images with in time span of 20 sec
- right bedore i never heard off about hold off
- by using the hold off after counter i over came that error

DAMPING OF SIMPLE PENDULUM AVI
https://drive.google.com/file/d/1YBgYMjvVCB-PiPkM9ZSGHWU0fmQ8E-NH/view?usp=sharing