Week 3 - Solving second order ODEs
AIM :
To simulate the transient behaviour of a simple pendulum and to create an animation of it's motion on MATLAB.
OBJECTIVE :
1.To write a program that solves the second order ODE corresponding to motion of a simple pendulum and to plot its angular displacement and angular velocity wrt time
2.To create the animation of the motion of pendulum.
EQUATIONS :
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.
Θ = displacement
GIVEN DATA :
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2
CODE :
clear all
close all
clc
%Second order ode
% input parameters
l=1;
m=1;
b=0.05;
g=9.81;
%where l is length (m),m is mass (kg),g is gravity (ms^2)
% initial condition for a given angular displacement and angular velocity
theta_0 = [0;3];
% time points for 100 values in between
t_span = linspace(0,20,100);
% performing ODE45 command to solve second order ODE
%where ode is inbuilt software and tits defined iniside brackets
[t, results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span, theta_0);
% plots of time vs angular displacement & time vs angular velocity
figure(1)
plot(t,results(:,1))
hold on
%hold on is used to plot multiple coomands
plot(t,results(:,2))
%labelling the axis
ylabel('Plot')
xlabel('time')
% using for loop command for different values of time of the simple pendulum
figure (2)
ct=1;
%for loop is used
for i=1:length(results(:,1))
THETA=results(i,1);
%Coordinates
x0=0;
y0=0;
x1=1*sin(THETA);
y1=-l*cos(THETA);
plot([-1.5 1.5],[0 0])
hold on
plot([x0 x1],[y0 y1],'color','b')
% plotting of mass by creating a sphere
plot(x1,y1,'-o','markersize',30,'color','r')
hold off
axis([-1.5 1.5 -1.5 5.5])
pause(0.0001)
M(ct) = getframe(gcf);
ct = ct+1
grid on
end
% movie making
movie(M)
videofile=VideoWriter('second_ode_pendulum.avi','uncompressed AVI');
%for creating video file
open(videofile)
writeVideo(videofile,M)
close(videofile)
Calling function command :
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
%For callinfg the function command
CODE EXPLANATION :
1.The program is started of with close all , clear all and clc which aids in clearing the variables , workspace and command window .
2.The input parameters and given details are input in to the matlab the details of inputs are
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s^2.
3.Now to solve the two first order ode , we need to specify the inintial conditions i.e the angular displacements and angular velocity .
4. The time limit of 0 to 20 sec for the motion of simple pendulum ,and 100 intervals in between(assumed). By using linspace command we can get equal values .
5.Now the ODE 45 command is used to solve the second order ODE, for this to happen we need to call for the function, which in the program is called by "ode_func" which is shown in 2nd program, this will help us to simply the program and ODE is solved in here & when it is called it to the main function assigning its input partameters, it provides us the required output.
6.Plot command is used in the main program to obtain a plot of time vs angular displacement & time vs angular velocity as shown in the program. Now using the for loop command for obtaining various angles of displacements of the simple pendulum for different values of time in the ODE is created by giving the initial condition x0 and y0, and then x1 y1 conditions are specified for the pendulum.
7.Then to create a movie and save our plot so that the animation would be like a continuous form, we have a command called ct and let us set the counter to be 1 to save movie, and the frames of various positions of the pendulum will be then stored to ct = ct+1; which will be then called in by getframe command to call all the plotted frames which can be displayed with the given values of pause making it like a movie.
OUTPUT :
Plot obtained between displacement and velocity :
NOTE : Blue curve represents displacement and red curve represents velocity .
SIMULATION OF SIMPLE PENDULUM GOOGLE DRIVE LINK :
https://drive.google.com/file/d/1uHWdLCajbg4h7shAdgWAtjgHkxV-6jxv/view?usp=sharing
ERRORS FACED :
1. The x label was mistakenly typed in caps on .The caps on was corrected and program ran sucessfully
CONCLUSION :
Thus , with the help of MATLAB , the simulation of simple pendulum corresponding to the solution of a second order ODE is created .