Solution of second order ODE and animation of motion of a pendulum by MATLAB programming
Aim:-
To solve second order ODE and animate the motion of the pendulum by MATLAB programming
Governing equation:-
Equation that play main role to solve second order ODE:-
dtheta/dt2+b/m d2theta/dt+g/L sintheta=0
where,
m=mass of the pendulum,
l=length of the pendulum
g=gravitational acceleration
b=damping coefficient
Now,
Let, theta=theta1 where theta=angular displacement
or, d2theta/dt=dtheta1/dt=theta2(say)
or, dtheta2/dt=-b/m* theta2+g/l * sin(theta1)
Objective:-
The main objective of this project is to solve second order ODE for a simple pendulum to animate it's motion and plot it's graph.
Brief Theory:-
Simple pendulum:- It is nothing but a small ball of mass m suspended by a string (mass less) of length L.
Acceleration of pendulum:- It is given by the equation a = -g sin_theta
Time period:- It is the time taken by a simple pendulum to complete one oscillation (full) and is given by T = 2π/omega
Second order differential equation for simple pendulum:- It is given by the governing equation
d2theta/dt2+b/m dtheta/dt+g/l sintheta=0
If theta=theta1 , then d2theta/dt=d2theta1/dt2=d/dt (dtheta1/dt) = d/dt (theta2)
Equation becomes
dtheta2/dt + b/m theta2 + g/l sintheta1=0
Workflow:-
1) Inputs are assumed
2) Initial conditions are given and differential equation is solved by a function that later defined.
3)Plots and animations are created as usual.
Brief content of this project:-
This project has been done by MATLAB programming. The code and the function has been attached below.
%Solving second order ODE by MATLAB programming
clear all
close all
clc
% taking inputs
%damping coefficient
b=0.05;
%gravitational acceleration
g=9.81;
%mass of the bob(pendulum)
m=1;
%length in metre
l=1;
%primary condition
t_span=linspace(0,20,200);
%angular displacement=0
%angular velocity=3
theta1=[0;3];
%solving the differential equation
[t,results]= ode45(@(t,theta)pendulum_ode(t,theta,l,g,b,m),t_span,theta1)
%plotting of graph
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
hold on
grid on
xlabel('time')
ylabel('angular dispacement&angular velocity')
legend('angular dispacement','angular velocity')
%Creation of animation
ct = 1;
for i=1:length(results(:,1))
x0=0;
y0=0;
x1= l*sin(results(i,1));
y1= -l*cos(results(i,1));
figure(2)
plot([-1 1],[0 0],'linewidth',6,'color','g');
%axix
axis([-2 2 -2 2]);
line([x0 x1],[y0 y1],'linewidth',2,'color','b');
hold on;
plot(x1,y1,'o','markersize',20,'markerfacecolor','r','markeredgecolor','r');
hold off;
pause(0.1)
M(ct) = getframe(gcf);
ct = ct + 1;
end
%Conversion of animation
movie(M)
videofile = VideoWriter('simple_pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Code for the function:-
%Code for the function
function [dtheta_dt]= pendulum_ode(t,theta,l,g,b,m)
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta(2);
dtheta2_dt = -(b/m)*theta2 - (g/l)*sin(theta1);
dtheta_dt =[dtheta1_dt;dtheta2_dt]
end
Algorithm of the program:-
1) Screen is cleared firstvby clear all,close all,clc.
2) Inputs are put as per requirement.
3) In the primary condition time is integrated and angular displacement and angular velocity is written.
4) Differential equation is put using the function named as pendulum_ode() .
5) In figure 1 time vs displacement , time vs angular velocity is plotted.
6) x axis, y axis are defined.
7) While creating animation initial location and final location are put.
8) In figure 2 a beam is created.
9) Axis are fixed.
10) The line from initial value to final value are created using 'line'.
11) Figure is plotted. Here markersize,linewidth, colour are given.
12) Using 'getframe' command frames are created.
13) Animation is created by 'movie' command.
14) Video file is opened, written and closed.
** Here to solve second order ODE 'ODE45' is used.
Algorithm for the function:-
1) Pendulum_ode() function is created which is taking values of t, theta,l,g,b,m.
2) Displacement, velocity and angular velocity, acceleration are written.
3) Ending of function after creating column vector of dtheta_1 dt and dtheta_2 dt.
Output plot:-
Plot of ODE(pendulum oscillation)
Animation:-
Below the YouTube link of the animation:-
Conclusion:-
In the above program we have used ODE45 by which we can solve any other higher order differential equation problems easily to convert it into first order ODE to get the desired result. Lastly, through the whole project we have learnt to solve pendulum problems easily .