simulation for the transient behaviour of a simple pendulum and to create an animation of it's motion.
AIM :
Write a program to simulate the transient behaviour of a simple pendulum and to create an animation of it's motion.
SOLUTION :
The governing ODE for the motion of simple pendulum with damper is given by
`(d^2theta)/dt+b/m*(d theta)/dt+g/l*sintheta`
the above equation is 2nd order ODE therefore we have to breakdown into 2 simple ODE
`theta=theta1`
`(d theta)/dt=(d theta1)/dt`
`d theta2=(d^2 theta1)/dt`
Subsituting the obtained values in the ODE
`(d theta2)/dt=-b/mtheta2-g/lsintheta1`
`d/dt[[theta1],[theta2]]=[[theta2],[-b/mtheta2 -g/l theta1 ]]`
PROGRAM :
- In order to solve ODE we should use the in-built solver (ode45)
[t,results]=ode45(@(t,theta)ode_fun(t,theta,b,m,g,l),t_span,theta_0)
Here the t & theta are independant and dependant variables followed up by a Function and the time interval and initial conditions
- program for calling ODE function
function [ode]=ode_fun(t,theta,b,m,g,l)
theta1=theta(1)
theta2=theta(2)
dtheta1_dt= theta2
dtheta2_dt= -((b/m)*theta2)-((g/l)*sin(theta1))
ode= [dtheta1_dt;dtheta2_dt]
end
PROGRAM FOR SIMULATION
close all
clear all
clc
%inputs parameters
b=0.7
g=9.81 %m/(s^2)
l=1 %m
m=1 %kg
%initial conditions
theta_0=[0;3]
t_span=linspace(0,10,200)
%solution
[t,results]=ode45(@(t,theta)ode_fun(t,theta,b,m,g,l),t_span,theta_0)
%plotting
figure(1)
plot(t,results(:,1),'linewidth',1.5)
hold on
plot(t,results(:,2),'linewidth',1.5)
xlabel('Time')
ylabel('Angle')
legend('Angular displacement','Angular Velocity')
hold off
%Animation of simple pendulum
ct=1
for i=1:length(t)
%angle=t,results(:,1);
%Angle=angle(i);
x0=0;
y0=0;
x1=l*sin(results(i));
y1=-l*cos(results(i));
%plotting base line
%plotting bob
figure(2)
plot(x1,y1,'.','MarkerSize',30,'Markerfacecolor','g')
hold on
line([-1 1],[0 0],'linewidth',5,'color','b')
axis([-2 2 -2 2])
hold off
hold on
plot([x0 x1],[y0 y1],'color','r')
hold off
V(ct)= getframe(gcf)
ct=ct+1;
end
movie(V)
videofile = VideoWriter('simple_pendulum.avi','uncompressed avi')
open(videofile)
writeVideo(videofile,V)
close(videofile)
Here 'Marker' command is used to create the sketch of the bob
plot(x1,y1,'.','MarkerSize',30,'Markerfacecolor','g')
legend creates a legend with descriptive labels for each plotted data series
legend('Angular displacement','Angular Velocity')
ERRORS FACED :
This error is rectified by giving correct axis limits
here the error is due to incorrect inputs of damping coefficient and inapporopiate use of Hold on and hold off commands.
RESULT:
SIMULATION :
CONCLUSION :
- In MATlab Higher order ODE can be solved by breakingdown to respective no of ODE and using in built solver ode45.