Solving second order ODE and Simulation of pendulum motion using Matlab
SOLVING SECOND ORDER ODE USING MATLAB.
CONTENTS:
- Objective.
- Theory explaining simple pendulum.
- Program.
- Errors.
- Result and link for the animation.
- Conclusion.
OBJECTIVE:
To write a proram to stimulate trasient behaviour of a simple pendulum and to create an animation of its own.
And the other objective here is to write a program to solve the following ODE,
This ODE represents the equation of motion of a simple pendulum with damping.MATLAB is used to write the program.
THEORY:
A simple pendulum consists of mass 'm' hanging along the string of length 'L' which is fixed at a pivot point 'p'. When displaced to an intial angle and released the pendulum will swing back and forth with periodic motion, by applying Newtons second law of rotational systems the equation for motion of pendulum can be obtained .
where,
g=gravity in m/s^2;
L=length of the pndulum in m;
b=damping coefficient;
m=mass of the ball in kg;
PROGRAM:
now from the question let,
L=1m;
m=1Kg;
b=0.05;
g=9.81;
clear all %command to clear the command screen
close all %command to close the workspace
clc % command to clear the command screen
%inputs from the given values in the question
b = 0.05;
g = 9.81;
l = 1;
m = 1;
%intial codition
theta_0 = [0;3];
%time points
t_span = linspace(0,20,500);
%solve ODE ,where ode45 is an inbuilt solver in matlab
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%plotting displacment and time
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
legend('Angular displacement','Angular velocity')
hold off
axis([0 20 -10 10])
%for finding motion of pendulum at different angle and time
for i= 1:length(t)
angle = results(:,1)
Angle = angle(i)
x1=0;
y1=0;
x2=l*sin(Angle);
y2=-l*cos(Angle);
figure(2)
plot([-1 1],[0 0],'linewidth',5,'color','r');
hold on
plot([x1 x2],[y1 y2],'linewidth',3,'color','g');
hold on
plot(x2,y2,'.','markersize',50)
hold off
axis([-2 2 -2 2])
M(i) = getframe(gcf);
pause(0.01)
end
movie(M)
videofile=VideoWriter('pendulum_animation.avi','uncompressed.AVI');
open(videofile)
writevideo(videofile,M)
close(videofile)
PROGRAM WHERE FUNCTION CALLING IS DONE:
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 = [dthta1_dt;dtheta2_dt];
end
ERRORS:
ERROR1:
I did a case sensitive error and then after the error occured i corrected it.
ERROR2:
I did a spelling error and corrected it later after the error occured.
RESULT:
LINK FOR THE PENDULUM ANIMATION :
https://drive.google.com/file/d/1u8f4S0OZqy1pwD0O4pRMoTbn8qHayvMp/view?usp=sharing
GRAPH BETWEEN DISPLACEMENT AND VELOCITY OF THE PENDULUM:
CONSLUSION:
The pendulum motion is simulated successfully and the respective pendulum motion animation can be watched with the help of the link stated above and also graph between displacemnt and velocity of the pendulum is showed above.