Week 3 - Solving second order ODEs
- Aim- Solving second order ODEs.
- Objective- Write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping.
- Simple Pendulum-
To solve this problem, we have to see simple pendulum first.

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. When displaced to an initial angle and released, the pendulum will swing back and forth with periodic motion. By applying Newton's secont law for rotational systems, the equation of motion for the pendulum may be obtained...

now lets assume theta1=theta2

now lets assume
damping force=b=0.06
mass of bob=m=1 kg
gavity=g=9.81
string length=1 m
now to solve differential eqution, we have to use ode45 commond,this command is inbuid.But to solve this we have to make function for it.
%ODE function
function[dtheta_dt]=ode_func(t,theta,b,g,l,m)
theta1=theta(1)
theta2=theta(2)
%from equation 1 and 2
dtheta1_dt=theta2;
%dtheta/dt=theta2
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
%dtheta2/dt=-(b/m)*theta2-(g/l)*sin(theta1)
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end
now we write our program...
% Matlab program to solving second order ODE
close all
clear all
clc
% Inputs
b=0.06;
l=1;
g=9.81;
m=0.1;
% Intial condition
theta_0=[0;5]
% o is displacement and 5 is velocity
% Time Points
t_span=linspace(0,10,500);
%to solve ode function
[t,result]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
%plotting
plot(t,result(:,1))
hold on
plot(t,result(:,2))
xlabel('plot')
ylabel('time')
while running this problem i have faced with following errors

solution- I have used wrong command for it and its spelling is also wrong.

solution- I have missed 'g' in this expression
after solving these issues and running problem we will get output as below,

Now,as per our requirement we have to make animation for this. this is done by using movie function.
so, our new program will look like as below
%to create animation for pendulum
ct=1;
theta_1=result(:,1);
for i=1:length(result(:,1))
%initial condition
x_start=0
y_start=0
theta_2=theta_1(i)
%final condition
x_end=1*sin(theta_2);
y_end=-1*cos(theta_2);
%animation plotting
figure(2)
%pendulum base
plot([-1 1],[0 0],'linewidth',4,'color','r')
hold on
%pendulum string
plot([x_start x_end],[y_start y_end],'linewidth',3,'color','b')
%bob of pendulum
plot(x_end,y_end,'marker','o','markersize',28,'markerfacecolor','b')
axis([-2 2 -2 2])
pause(0.035)
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
%create movie
movie(M)
videofile=VideoWriter('simple_pendulum.avi','uncompressed avi');
open(videofile);
writeVideo(videofile,M);
close(videofile);
while solving this problem i have faced some errors as below-


after solving these all error my program is working fine
Output:-

YouTube Link- https://youtu.be/pt_I2sSLDIc