Program to simulate transient behaviour of a simple pendulum & create an animation of it's motion
Aim : Solve second order Ordinary Differential Equation of motion of Simple Pendulum by using MATLAB, simulate & create an animation of the motion.
ODE representing the equation of motion of simple pendulum with damping.
(d^2 θ)/〖dt〗^2 + b/m* dθ/(dt )+ g/L sinθ = 0
in the above equation,
g = gravity in m/s2 = 9.81 m/s2.
L = length of the pendulum in m = 1 metre
m = mass of the ball in kg = 1 kg
b=damping coefficient = 0.05
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec2 at time t=0.
The given equation is of the order 2. So the equation is broken down into two first order ODEs
Here theta = angular displacement (radians)
t = time in seconds
dtheta/dt = angular velocity
the two first order ODEs are,
Assuming,
θ= θ_1
(d^2 θ)/〖dt〗^2 =〖dθ〗_2/dt
Here,
θ_2= dθ/dt=〖dθ〗_1/dt
Now, writing the code in Matlab to create a plot & animation,
Using ode45, writing a function named pendulum in another file with the formulas derived here.
fundtion code:
function [dtheta_dt] = pendulum(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
Calling the function to the main program & giving enough input arguments.
clear all
close all
clc
b=0.05;
g=9.81;
l=1;
m=1;
theta_0=[0 5];
t_span = linspace(0,30,500);
[t,results] = ode45(@(t,theta)pendulum(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
plot(t,results(:,1),'color','r')
hold on
plot(t,results(:,2),'color','g')
xlabel('time')
ylabel('plot')
legend('angular displacement','angular velocity')
figure(2)
angular_displacement = results(:,1)
angular_velocity = results(:,2)
Plot :
The Above plot shows how angular displacement & angular velocity change with respect to time. When displacement is maximum velocity is zero & vice-versa. By the graph, we can observe that the displacement & velocity of pendulum are diminishing due to damping effect.
Writing the code for the animation of the motion of simple pendulum
for i = 1:length(angular_displacement)
x0 = 0; %starting position of the pendulum
y0 = 0;
x1 = l*cos(angular_displacement(i)-pi/2); %ending postion of pendulum
y1 = l*sin(angular_displacement(i)-pi/2);
plot([-1 1],[0.075 0.075],'color','r') %for upper base support of pendulum
hold on
plot([x0,x1],[y0,y1],'color','k') %for hanging string of pendulum
plot(x1,y1,'-o','markersize',35','markerfacecolor','m','markeredgecolor','k') %create the mass of pendulum
plot(x0,y0,'-o','markersize',5','markerfacecolor','r','markeredgecolor','b')
axis([-1.5 1.5 -1.5 1]);
displacement = angular_displacement(i);
velocity = angular_velocity(i);
pause(0.03);
M(ct) = getframe(gcf); %for animation of pendulum
ct = ct+1;
hold off
end
movie(M) %Exporting animation movie
movie2avi(M,'Simple_Pendulum_Animation.avi');