Week 3 - Solving second order ODEs
Aim : Solve ODE equation of motion of a simple pendulum with damping using matlab.
Governing equation: Equation of motion of a simple pendulum with damping:
d^2theta/dt^2 + b/m*dtheta/dt +g/L*sintheta = 0
where,
b = Damping coefficient
m = Mass of pendulum bob
L = Length of pendulum
g = Gravity
Code:
Ode_func 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
Main_ODE code:
clear all
close all
clc
%Inputs
%Damping coeffient
b = 0.05;
%Acceleration due to gravity
g = 9.81;
%Length of the pedulum
l = 1;
%Mass of the bob
m = 0.1;
%Initial parameters
theta_0 = [0;3];
%time span
t_span = linspace(0,20,100);
%solve ODE
[t,result] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
plot(t,result(:,1));
hold on
plot(t,result(:,2));
xlabel('time');
ylabel('plot');
%Loop for animation
ct = 1;
for i = 1:length(result(:,1))
x0 = 0;
y0 = 0;
x1 = l*sin(result(i,1));
y1 = -1*cos(result(i,1));
figure(2)
plot([-1 1],[0 0],'linewidth',6,'color','r');
axis([-2 2 -2 2 ]);
line([x0 x1],[y0 y1],'linewidth',2,'color','g');
hold on;
plot(x1,y1,'o','markersize',20,'markerfacecolor','b','markeredgecolor','b');
hold off;
pause(0.1)
z(ct) = getframe(gcf);
ct = ct + 1;
end
movie(z)
videofile = VideoWriter('pendulum.avi','uncompressed.AVI');
openfile(videofile)
WriteVideo(videofile,z)
close(videofile)
Matlab Execution:


c
Graph:


Result :
Finally we have solved the motion of a simple pendulum with damping ODE and funcion and plot the graph and simulation of the pendulum.