Week 3 - Solving second order ODEs
AIM-To see simulation of simple pendulum by solving second order differential equation using matlab
THEORY- this ode represents the equation of motion of a simple pendulum with damping

In the above equation,
g=gravity in m/s^2
L=length of pendulum in meters
m=mass of the ball in kg
b=damping coefficient
clear all
close all
clc
%Inputs
b=0.1;
g=9.81;
l=1;
m=0.1;
%Initial condition
theta_0=[0;5];
%time points
t_span=linspace(0,10,500);
%solve ODE
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
hold on
plot(t,results(:,1))
plot(t,results(:,2))
ct=1
for i=1:length(t)
x=l*sin(results(i,1))
y=-1*cos(results(i,1))
figure(2)
plot([-1,1],[0,0],'linewidth',3,'color','b'); % allocating the co-ordinates for the support
hold on
plot([0 x],[0 y],'linewidth',2,'color','r'); % length of pendulum
%plot(x1,y1,'H','markersize',10,'markerfacecolor','r'); % allocating the co-ordinates for the pend
plot(x,y,'-o','markersize',15,'markeredgecolor','r','markerfacecolor','g');
grid on
xlim([-1.5 1.5])
ylim([-1.5 1.5])
pause(0.1);
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter('ode.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
Code Explanation-in the following code first we have allocated the useful parameter.After then we call the below code and then solve differential equation by using ode45,and then we plot time versus displacement and time versus velocity in the same figure using plot command.Also we have created the animation of pendulum for time span.
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
code explanation- in this code we have created the matrux for theta_dt so that we can caculate dispalcement and velocity.After that we will call this code in the main code by using function to see simulation of pendulum
plot for velocity and damping.

Below is the animation of simple pendulum.
simple pendulum