A program using Matlab to simulate simple Pendulum
Objectives:
Write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping.
`(d^2theta)/dt^2+b/m (d theta)/dt+g/Lsintheta`
from the above equation, we get two-equation
`(d theta1)/(dt)=theta2`
`(d theta2)/(dt)= -b/m theta2-g/L sin theta1`
Procedure:
Step1:
With Given Inputs such as :
L= 1 meter.
m= 1 kg.
b=0.05.
g=9.81 m/s2.
To simulate the program with inputs such as :
Time =0-20 sec,
Angular Displacement = 0,
Angular velocity =3 rad/sec at time t=0,
Program to calculate ODE:
% Program that sloves the following ODE its represents the equation of motion of a simple pendulum.
clear all
close all
clc
Step1:
%inputs
%Damping co efficient
b=0.05;
%Acceleration due to gravity -m/s^2
g=9.81;
%mass of the ball-kg
m=1;
%length of the string-m
l=1;
%initial condition
theta_0=[0;3];
%time points
t_span=linspace(0,20,500);
Step2:
%sloving ode
[t,results]=ode45(@(t,theta) ode_func(t,theta, b,g,l,m),t_span,theta_0);
Step3:
%plotting
figure(1)
plot(t,results(:,1),\'linewidth\',1)
hold on
plot(t,results(:,2),\'linewidth\',1)
axis([0 25 -3 3])
xlabel(\'Time-sec\')
ylabel(\'Angular velocity & Displacement\')
legend(\'change in displacement\',\'change in angular velocity\')
hold off
figure(2)
plot(results(:,1),results(:,2))
xlabel(\'position-rad\')
ylabel(\'Angular velocity -rad/sec\')
theta2=results(:,1);
ct=1;
step4:% program which used to simulate pendulum
for i=1:length(theta2);
theta3=theta2(i);
x0=0;
y0=0;
x1=1*sin(theta3);
y1=-1*cos(theta3);
figure(3)
plot([-1.1 1.1],[0,0],\'linewidth\',2);
hold on
plot([x0 x1] ,[y0 y1],\'color\',\'r\',\'linewidth\',2);
hold on
plot(x1,y1,\'o\',\'markers\',20,\'markerfacecolor\',\'g\')
hold off
grid on
axis([-1.5 1.5 -1.5 0.5])
pause(0.004)
M(ct)=getframe(gcf);
ct=ct+1;
end
%Animation or Movie making.
movie(M)
Videofile=VideoWriter(\'pendulum.avi\',\'uncompressed avi\');
open(Videofile)
writeVideo(Videofile,M)
close(Videofile)
Step2:
In this step we need to solve the ODE, for ode45 in this we have ode_func, this function is used to take variable data from another program.
Program for 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
Step3:
1. Plotting Fig(1) with change in angular velocity & Displacement with respect to time.
2. Plotting Fig(2) in which with the angular velocity with respect to the position.
Step 4:
program which is used to simulate pendulum:
with help of for loop command, all inputs data are iterated and developing pendulum motion.
using getframe command all data are collected and store to iterate with respect to counter
Finally creating movie Or animation with the help of movie,videofile, videowriter, open & close commands.
OUTPUTS:
Animation link :
Conclusion:
Successfully coded using MATLAB for motion of Simple pendulum according to the change in velocity and displacement with damping co-efficient.