Week 3 - Solving second order ODEs
AIM:To write a program that solves the following ODE.
- To Write a program in Matlab that will simulate the pendulum motion.
OBJECTIVE:
- To solve the given defferntial equation. By coverting the 2nd order equation into 1st order.
- To use ode45 command in Matlab and determine this angular displacement and angular velocity of the given pendulum withe respect to its time
- To create a simulation of the pendulum motion with respect to time.
Explanation :
ODE(ordinary differential equation)- ODE is a differential equation which has one or more functions with dependent variable and one independent variable.
NOTE:In Matlab , only the first order differential equation can be solved.So before using ode45 command any equation of higher order must be converted to first order.
SIMPLE PENDULUM: It is a object that as small mass suspended to its end made of wire or a spring.It is free to move forward and back.
Let's consider an simple pendulum of,
lenght(l)=1m
mass(m)=1kg
damping co-efficient(b)=0.05
gravity(g)=9.81 m/s^2
Lets consider that the pendulum is making a zero angular displacement with a angular velocity of 3rad/sec initially as shown in fig(1)

Fig.1 simple pendulum
When the pendulum starts moving further whit the given initial condition,its angular displacement(θ) and angular velocity keeps on varying,the values of angular velocity and angular displacement(θ) keeps on changing with respect to time as show in fig(2).our first objective is to find its value for angular velocity and angular displacement .

Fig.2 simple pendulum with unknown values
The angular displacement(θ) made by a pendulum at given angular velocity with respect to time can be solved using the below deferential equation.In matlab using ode45 command.
whith the help of angular displacement(θ) with respect to time,the coordinates for the exact displacement made by pendulum can be obtained and by using this coordinates,the animation for the kinematics of the simple pendulum can be obtained.
SOLUTION:
Step1:convert the 2nd oder given equation in to first order
`d^2 /dt^2theta+b/md/dttheta+g/msin(theta)=0`.............(1)
lets take `theta`=`theta1`.........(2)
and `d/dttheta1=theta2`........(3)
applying eqution 2 and 3 in 1
`d/dttheta2+b/mtheta2+g/lsintheta1`
where, `d/dttheta2=-b/mtheta2-g/l sintheta1`.........(4)
the above equation (4) is used in ode45 command to get integrals of angular displacement (θ) and angular velocity
CODE 1:
clear all
close all
clc
%pendulam parameters
b=0.05; %dampercoefficent(b)
m=1; %mass(m) of 1kg
l=1; %length (l) 1meter
g=9.81;
%assigning the initial values
%theta_0=[angular displacement,angular velocity]
teta_0=[0,5];
%time
t_span=linspace(0,20,200);
%ode45 syntex
%ode45(@(IV,DV) equation,time,inistal values)
[t,result]=ode45(@(t,theta) fun_ode(theta,b,m,l,g),t_span,teta_0);
figure(1)
plot(t,result(:,1))
hold on
plot(t,result(:,2))
title('plot of angular diaplacemet and angular velocity V/s time')
xlabel('time');
ylabel('plot');
%creating animation
figure(2)
ct=1;
for i=1:length(t_span)
x=0;
y=0;
x1=l*sin(result(i,1));
y1=-l*cos(result(i,1));
plot([x x1],[y y1],'LineWidth',3)
hold on
plot(x1,y1,'o','Markers',25,'markerfacecolor','r')
hold off
axis([-1.5 1.5 -2 1]);
M(ct)=getframe(gcf);
ct=ct+1;
end
%creating animation
videofile=VideoWriter('pendulam.avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)
code 1 creating an animation of simple pendulum
Line 6-9:
b=0.05; -assigning the pentulam parametres
m=1;
l=1;
g=9.81;
Line 13-15:
teta_0=[0,5]; -assining the initial value of angular displacement and angular velocity withe time
t_span=linspace(0,20,200); period.
Line 19:
[t,result]=ode45(@(t,theta) fun_ode(theta,b,m,l,g),t_span,teta_0);
SYNTAX:
ode45(@(IV,DV) equation,time,inistal values)
It is a ode45 function initially we need to assing dependent and indepented variables of the given equation,acording to the pendulum equation t is a independent variable and theta is a dependent variable.and it is assigned as
ode(@(t,theta)
Next,the eqaution to be integrated must be given , since the given equation is complex. It is been simplefied using function and stoed as a function with the function name fun_ode(theta,b,m,l,g)
Then the inisial value and the time span need to be declaread
Line 21-27:
figure(1) -using plot command a plot of plot of angular diaplacemet and angular
plot(t,result(:,1)) velocity V/s time is ploted
hold on
plot(t,result(:,2))
title('plot of angular diaplacemet and angular velocity V/s time')
xlabel('time');
ylabel('plot');
Line 32:
for i=1:length(t_span) -using for loop for the itiration of 1 to the lentght of time (t_span) times
Line 33-36:
x=0; -assinging the vales for the cordinated
y=0;
x1=l*sin(result(i,1));
y1=-l*cos(result(i,1));
line 37:plot([x x1],[y y1],'LineWidth',3) -ploting the given variables and giveing a line width of 3
Line 39:
plot(x1,y1,'o','Markers',25,'markerfacecolor','r') -using marker to creat a spear in the plot and giving a size of 25
Line 42:M(ct)=getframe(gcf); -storying all the frame in side an array M for creating animation
Line 48-51:
videofile=VideoWriter('pendulam.avi'); -creat animation and saving it in pendulam.avi vedio file
open(videofile)
writeVideo(videofile,M)
close(videofile)
CODE 2:
%creating a function to solve the complex eqation
function [dtheta_dt]=fun_ode(theta,b,m,l,g)
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 2 for creating a function to solve the complex equation
Line 2: function [dtheta_dt]=fun_ode(theta,b,m,l,g) -creating a function by name fun_ode and assigning the inputvariables
Line 3-4:
theta1=theta(1) -assigning the values inside the equation
theta2=theta(2)
dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
Line 5:dtheta_dt=[dtheta1_dt;dtheta2_dt]; -assigning the value of dtheta1_dta nd dtheta2_dt inside a column matrix of dtheta_dt
OUT PUT:

Fig.3 plot of angular diaplacemet and angular velocity V/s time is plotted
The above plot represent change in angular displacement and angular velocity of the given pendulum with respect to time
LINK FOR ANIMATION:- https://youtu.be/6IAbCrlG0Kc
The above link shows the animation of the simple pendulam with respect to time
CONCLUTION
- With the help of the given deferential equation the angular displacement and angular velocity of the penulum is oBtained
- a plot of angular displacement and angular velocity V/s time is ploted