Application of O.D.E to plot transient behaviour of simple pendulum on MATLAB
This project demonstrates plotting of transient behaviour of simple pendulum.
MATLAB CODE-
Part-1
clear all
close all
clc
%input parameters
b=0.05;
g=9.81;
l=1;
m=1;
%initial condition
theta_0 = [0;3];
%time points
t_span =linspace(0,20,500);
%solve ode
[t,results] =ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
legend('angular displacementc(rad)','angular velocity(rad/s)')
%plotting
x0=0;
y0=l;
theta1 = results(:,1)
ct=1
for i=1:length(theta1)
theta2= theta1(i)
x1= l*sin(theta2);
y1= l-l*cos(theta2);
grid on
hold on
figure(2)
a= plot([x0 x1],[y0 y1],'linewidth',3,'color','r');
b= plot(x1,y1,'o','MarkerSize',36,'MarkerFaceColor','b');
pause(0.001)
axis([-1.5 1.5 -0.5 1.5])
M(ct) =getframe(gcf);
ct=ct+1;
if i<length(theta1)
delete(a);
delete(b);
end
end
movie(M)
videofile =VideoWriter('simple_pendulum1.avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Part-2
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
Explanation -
firstly we take input values such as length of simple pendulum,mass,damping coefficient etc.
we define initial conditions.
for analysing transient behaviour of simple pendulum we need to reduce second order ode to first order ode for simplicity. a seperate function is created for this perpose.
now we define time span
then we call ode 45 to solve required function and get plot of angular speed and angular velocity with respect to time.and plot the results
after that we create movies for simple pendulum for that we have to create frmes for each position of pendulum and glue it .For loop is used for this perpose.to avoid overlaping of previous results we created if function and given condition such that it will continued clearing previous results exept final one.