A program in Matlab Octave that will simulate the pendulum motion
AIM :
- SOLVING SECOND ORDER ODE.
OBJECTIVE:
- Write a program in Matlab\\Octave that will simulate the pendulum motion.
- write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping
THEORY:
- WHAT IS SIMPLE PENDULUM?
A pendulum is a weight suspended from a pivot so that it can swing freely. When a pendulum is displaced sideways from its resting, equilibrium position, it is subject to a restoring force due to gravity that will accelerate it back toward the equilibrium position.
- THEORY OF SIMPLE PENDULUM :
A simple pendulum consists of a mass m hanging at the end of a string of length L. The
period of a pendulum or any oscillatory motion is the time required for one complete
cycle, that is, the time to go back and forth once.
- GOVERNING EQUATION :
second-order ODE representing the equation of a simple pendulum motion.
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.
- To write a program in Matlab\\Octave that will simulate the pendulum motion, just like the one shown in the start of this challenge.
- We are using,
- L=1 metre,
- m=1 kg,
- b=0.05.
- g=9.81 m/s2.
MATLAB CODE:
clear all
close all
clc
%input
b=0.05;
g=9.8;
l=1;
m=1;
%initial conditions
initial=[0,3];
%time points
time=linspace(0,150,500);
%solve ode
[t,results]=ode45(@(t,theta) ode(t,theta,b,g,l,m),time,initial);
%plot
C=1
for a=1:length(results(:,1))
x1=-l*sind(results(a,1))
y1=-l*cosd(results(a,1))
plot([0,x1],[0,y1],\'linewidth\',1)
hold on
plot(x1,y1,\'O\',\'color\',\'r\')
axis([-.2,.2,-1.1,0])
pause(.006)
%storing frames in m
K(C)=getframe(gcf)
C=C+1
end
%creatig videofile for m
movie(K)
videofile=VideoWriter(\'forwardkinamatics.avi\',\'uncompressed AVI\')
open(videofile)
writeVideo(videofile,K)
close(videofile)
close all
plot(t,results(:,1))
hold on
plot(t,results(:,2))
axis([0,50,-8,8])
title(\"displacement vs velocity\")
xlabel(\'time\')
ylabel(\'plot\')
legend(\'displacement\',\'velocity\')
MATLAB CODE OF FUNCTION \'ode.m\':
function [result]= ode(t,theta,b,g,l,m)
theta1=theta(1);
theta2=theta(2);
dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/l)*sind(theta1);
result=[dtheta1_dt;dtheta2_dt];
end
RESULTS:
- For better understanding, I am providing time=150 secs.
- Animation 01:
- https://youtu.be/B9KsNxtBJh8
- Animation 02:
- https://youtu.be/w9nz76nCBF8
ERRORS AND REMEDIES :
- error due to wrong initial value of the variable
solved by assigning value equal to 1.
REMARKS:
- The water-marks on the animation video files are due to the use of external software to reduce its size.