Week 3 - Solving second order ODEs
Aim : To creat a animation of simple pendulum by using second order differential equation. A nd also to plot the graph between angular velocity and displacement.
Theory : A simple pendulum is a weight suspended from a pivot,so that it can swing freely.
It consists of a mass rod , frictionless pivot or point of suspension, angular displacement, bob.
Equation :
d^2 (theta) / dt^2 + b/m * d(theta)/dt + g/l *sin(theta).
here we are taking as
- l = length = 1 meter.
- b = damping = 0.05
- m = mass = 1 kg.
- g = gravity = 9.81 m/s^2.
Code explanation :
We start the code with clear all close all clc.
We give the initial conditions.
Next we give the initial points.
we solve the odes .
we plot the results and we give the xlabel and ylabel.
Next we aniamte the transit motion of the simple pendulum motion
Finally we start making video of simple pendulum.
Code :
clear all
close all
clc
b=0.05;
g=9.81;
l=1;
m=1;
% initial conditions
theta_0 = [0;3];
% initial 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);
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
% animation the transient motion of the simple pendulum.
ct=1;
for i= 1:length(results(:,1));
x0 = 0;
y0 = 0;
x1 = l*sin(results(i,1));
y1 = -l*cos(results(i,1));
figure(2)
plot([-1 1],[0 0])
hold on
plot([x0 x1],[y0 y1],'linewidth',3,'color','b')
axis([-2 2 -2 2])
plot(x1,y1,'o','markersize',20,'color','r')
pause(0.001)
grid on
M(ct) = getframe(gcf)
hold off
ct = ct+1
end
%making the file video
movie(M)
videofile = VideoWriter('simple pendulum.avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)
Function code :
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
Plot :

Animation video :
Errors occured :

Solution : small error that instered of 'M' we use small 'm'.