Simulation of Motion of Pendulum by solving second order O D E using MATLAB
Aim:-To Simulate the motion of Pendulum by solving second order O.D.E
Objective:-
- To convert second order O.D.E to first order in order to use the bring the variables in ode45 format.
- Plotting the results of theta over the given time span.
- Using the marker command to create a pendulum
Introduction:-
Motion of the pendulum is given by the following second order O.D.E
.......................................................Equation 1
where theta is the dependent variable and t is the independent variable;
In the above equation,
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
Assuming Inputs:-
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
tstart=0 sec
tend=20 sec
ω(Angular Velocity)=F(θ,t)
ω=3 rad/sec at θ=0,t=0;
For converting Second order O.D.E into first order O.D.E we assume;
Θ = Θ1
dθ/dt =dΘ1/dt=Θ2
(d^2 θ)/(dt^2 )= dΘ2/dt
Substituting value of the Θ,dθ/dt,(d^2 θ)/(dt^2 ) in equation 1 we get;
dΘ2/dt+b/m*Θ2 +g/l sinΘ1=0
Stepwise Explanation:-
- Inputing the intial conditions and the timespan for the motion is to be observed.
- dtheta_dt is function of (t,theta,b,g,l,m) in which t and theta varies so;dtheta_dt is an array which represnts value of angular acceleration and angular velocity.
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
3.Variables x0,y0 are fixed at the origin.For loop brings different value of theta each time which gives the position of the variables x1,y1.
4. Plotting a straight line reference and then by using marker command making a pendulum bob at the end of the co-ordinates x1,y1.
5.M is array which contains images of the pendulum at various angle;this pictures are then used for making a animation by using the movie command.
The programme is written below:-
clear all
close all
clc
%Inputs
b=0.05;
g=9.81;
l=1;
m=0.1;
ct=1;
%intial condition
theta_0=[0;3];
%time interval
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));
figure(2)
for i=1:length(results(:,1));
Results=results(i,1);
x0=0;
y0=0;
x1=l*sin(Results);
y1=-l*cos(Results);
plot([-1.5 1.5],[0 0],[x0 x1],[y0 y1],x1,y1,\'-o\',\'markersize\',20,\'markerfacecolor\',\'y\')
axis ([-1.5,1.5,-1.5,1]);
pause(0.3);
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile = VideoWriter(\'pendulum_animation.avi\',\'Uncompressed AVI\')
open(videofile)
writeVideo(videofile,M)
close(videofile)
Fig.Plot of Time vs Displacement which is getting damped
Youtube link for the animation is :-https://youtu.be/ZLNseDblUqY
Errors done: -
1.Error in typing axis array in which the start end point was selected as negative .
Conclusion:- By using a O.D.E solver simulated the motion of the pendulum and created animation of the its motion by defining input variables.