Mathematical modelling and simulating the Transient motion of simple pendulam using ODE45 in matlab
AIM:
Write a program in Matlab that will simulate the pendulum motion.
INTRODUCTION:
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 resting force due to gravity that will accelerate it back toward the equilibrium position. When released, the restoring force acting on the pendulum\'s mass causes it to oscilate about the equilibrium position, swinging back and forth. The time for one complete cycle, a left swing and a right swing, is called the period depends on the length of the pendulum and also to a slight degree on the amplitude, the width of the pendulum\'s swing.
Mathematical model for pendulam:
`(d^2theta)/dt^2 + (b/m)((d theta)/dt) +g/l(sintheta)=0`
`(d theta)/dt = velocity = v`
`(dv)/dt = a`
`(dv)/dt + (b/m)v + (g/l) sinx=0`
`y=[[x],[v]]`
`(dy)/dt = [[(dx/dt)],[((dv)/dt)]] = [[v],[-(g/l)*sin(x)-(b/m)*v]]`
Mathlab code for Solving the ODE:
clear all
close all
clc
%initial condition
y0 = [0;3]; % at time t=0 the disp = 0 , velocity = 3
tspan= linspace(0,20,500); % span of time taken as 0 to 20 seconds
[tsol,ysol]=ode45(@(t,y) pendulamfunc(t,y),tspan,y0);%ode45
%plotting
figure(1)
plot(tsol,ysol(:,1))%plotting t vs displaceent
hold on
plot(tsol,ysol(:,2 ))%plotting t vs velocity
axis([0 20 -5 5])%settinfg x axis as to 0 to 20 and y as 5 to 5
ct=1;
for i=1:length(ysol(:,1))%calling displacement function
x0=0;%fixed point
y10=0; %fixed point
l=1;
x1=l*sin(ysol(i,1));
y1=-l*cos(ysol(i,1));
figure(2)
plot([x0 x1],[y10 y1],[-1 1],[0 0],\'linewidth\',3,\'color\',\'k\')%plotting the length of the string
hold on %to prevent overwriting
plot(x1,y1,\'o\',\'MarkerSize\',30,\'Markerfacecolor\',\'r\')% plotting the marker
axis([-2 2 -2 0])
pause(0.03)
hold off %turing off to create new position
f(ct)=getframe(gcf)
ct=ct+1;
end
movie(f)
videofile=VideoWriter(\'pendulam_simulation.avi\',\'Uncompressed AVI\')
open(videofile)
writeVideo(videofile,f)
close(videofile)
Function command:
function fval = pendulamfunc(t,y)
%matrix function defined by displacement and velocity
x=y(1);
v=y(2);
%defining constants
l=1;
m=1;
b=0.05;
g=9.81;
fval(1,1)= v
fval(2,1)=-(g/l)*sin(x)-(b/m)*v
end
plotting of time vs displacement and velocity
Explanation of the code:
- The code is started with initial condition at time t=0 therefore x(t) =0 y(t) =3;
- Then the ODE 45 command is executed as t as independent variable and y as dependent variable comprising the solution of displacement and velocity.
- The function (t,Y) is executed in separate program and coresponding values of (dx/dt) and (dy/dt) equated in terms of formula
- After ODE 45 is executed for given span of time , the results are available in `int(dx/dt)dt = int_0^tdx = x` and `intdv/dtdt = int_0^tdv = v`
- The results of displacement and velocity id plotted against time period of tspan
- Next the position (x,y) is plotted against various values of displacement obtained from ode
- The bob is added with use of marker command
- Finally animation is saved with file array v with filename pendulam.avi .
for animation video file :
click on the below link