Week 3 - Solving second order ODEs
Oscillation of a Simple Pendulum

A simple pendulum consists of a ball (point-mass) m hanging from a (massless) string of length L and fixed at a pivot point P. hen displaced to an initial angle and released, the pendulum will swing back and forth with periodic motion. By applying Newton's second law for rotational systems, the equation of motion for the pendulum may be obtained
−mgsinθL=mL2d2θ/dt2
We start the program by giving the input condition
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2
Time span between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.

ode45 is the inbuild function which solves differential equation to provide the value of t and results
this is plotted to understnd the damping of oscillation
for loop is used to simulate the oscillation of pendulum for changing time
there are 2 points (x0,y0) fixed points and (x1,y1) end points
(x0,y0) is fixed as(0,0)
and (x2,y2)=(Lsin(θ),Lcos(θ))
function[dtheta_dt] = ode_function(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
clear all
close all
clc
%inputs
b = 0.05; % damping
g = 9.81; %acceleration gravity
l=1; %length
m= 0.1; %mass
% Initial condition
theta_O =[0;3]
%time points
t_span = linspace(0,20,500);
%solve ODE
[t,results] = ode45(@(t,theta)ode_function(t,theta,b,g,l,m),t_span,theta_O);
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('plot')
ylabel('time')
hold off
grid on
figure
ct=1
for i=1:length(t_span)
x0= 0;
y0 = 0;
%determine end points
x1 = -(l*sin(results(i,1)));
y1= -(l*cos(results(i,1)));
plot([x0 x1],[y0 y1],[-0.5 0.5],[0 0],'linewidth',3)
hold on
plot(x1,y1,'o','color','r')
axis([-1.5 1.5 -1.5 0.5])
grid on
pause(0.05)
hold off
M(ct)= getframe(gcf);
ct = ct+1;
end
movie(M)
videofile = VideoWriter('simple_pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
https://drive.google.com/file/d/1Ty5HAFbvopb-KKhZxYBIEwMdV27k_fut/view?usp=sharing