Solve the Second order ODE using Matlab Programming and create a Animation of pendulam and its motion.
Aim : Solve the Second order ODE using Matlab Programming and create a Animation of pendulam and its motion.
Given Problem Statement:

In given Equation
g = gravity (m/s2) = 9.81
m= mass of ball (kg) = 1
l = length of the pendulum (m) = 1
b =damping coefficient = 0.05
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
solving 2nd order ODE:
Assuming theta = theta1
diffrentiate dtheta_dt = dtheta1_dt = theta2
d^2theta_dt^2 = d^2theta1_dt^2 = dtheta2_dt
d^2theta_dt^2 + (b/m)dtheta_dt +(g/l)*sin(theta) = 0
d_theta2_dt + (b/m)theta2 +(g/l)sintheta1 = 0
dtheta1_dt = theta2
dtheta2_dt = -(b/m)*theta2 - (g/l)*sin(theta1)
Software Used : MATLAB
Program:
clear all
close all
clc
% Input variables
% b = daming
% m = mass
% g = gravity
% l =length
b = 0.05;
g = 9.81;
l = 1;
m = 0.1;
% Intial Condition
theta_0 = [0;3];
% time points
t = linspace(0,10,100);
% solve ODE
[t,results] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t,theta_0);
figure(1)
hold on
plot(t,results)
xlabel('time')
ylabel('theta & velocity')
legend('theta','velocity')
hold off
disp = results(:,1);
vel = results(:,2);
ct =1;
for i =1:length(disp)
x0 = 0;
y0 = 0;
x1 = l*sin(disp(i));
y1 = -l*cos(disp(i));
figure(2)
plot([x0,x1],[y0,y1],'linewidth',3,'color','b')
hold on
plot(x1,y1,'marker','o','markersize',15,'markerfacecolor','g')
axis([-1.5 1.5 -1.5 1])
plot([-1.5 1.5],[0 0],'linewidth',3)
pause(0.05)
hold off
M(ct) = getframe(gcf);
ct = ct+1;
end
movie(M)
videofile = VideoWriter('pendulum avi','uncompressed 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*theta2/m) - (g*sin(theta1)/l)
dtheta_dt = [dtheta1_dt; dtheta2_dt];
end
Output Plot:
