Solving ODE in Matlab
OBJECTIVE:
Solving ODE for pendulum in Matlab and creating animation.
INTRODUCTION TO ODE:
The ordinary differential equation is a differential equation contains one independent variable and the derivatives of a function. In many engineering applications, ODE is used to calculate or determine the transient behavior of the system.
PENDULUM:
The pendulum is a string that is fixed at one end and carrying mass on the other. 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. By using Ordinary differential equation, Displacement and velocity of a pendulum can be determined.
EQUATION:
D2 (Theta) +(b/m) *D(Theta)+(g/l)sin(Theta)=0------> 1
Where,
D= d/dt.
Theta=angle;
b=Damping Co-efficient;
m=Mass(kg);
g=Gravity accelaration(m/sec2 ).
l=Lenght(m).
PROGRAM:
For this particular equation here program has been created using Matlab.Here constant and initial values are given in program itself.
clear all
close all
clc
%Constants
b=0.05; %Damping Co-efficient
g=9.81; %Gravity accelaration
L1=2; %length of the string
m=1; %mass of weight
%Initial value and time intervel
theta=[0;3]; %initial displacement and velocity
x=linspace(0,20,500); %time intervel
%Intergration
[T,Theta]=ode45(@(t,theta)fun1(theta,b,g,L1,m),x,theta); %intergration function
%Plot graph for Displacement Vs Time and Velocity Vs Time
figure(2) %plot 2
plot(T,Theta(:,1)) %plot graph displacement
hold on
plot(T,Theta(:,2)) %plot graph velocity
xlabel(\'Time(s)\')
ylabel(\'Velocity (m/s^2),Displacement(m/s)\')
hold off
a=length(Theta); %No of values in Theta
l=1;
for i=1:a % loop start
arc=Theta(i,1); %Value of displacement
length=(arc/L1); %opposite/hypotenues
angle=asind(length);%find angle for particular diviation
x1=L1*sind(angle);%X length
y1=L1*cosd(angle);%Ylenght
r=0.5; %circle radius
theta=[0:360]; %360 angle
x=x1+r.*cosd(theta); %x length of circle from x-end point of string
y=y1+r.*sind(theta); %y length of circle from y-end point of string
figure(1) %plot 1 for animation
plot([0 x1],[0 -y1],\'linewidth\',1) %plot string of pendulum
hold on % hold plot
plot(x,-y,\'marker\',\'*\') %plot circle
hold off %hold off
axis([-3 3 -4 0]) %axis constrain
pause(0.1) %pause 0.1 sec
M(l)=getframe(); %capture frame with respect to l
l=l+1; %increment l
end
movie(M) %create movie using M
video=VideoWriter(\'Pendulum_5\',\'Uncompressed avi\') %video name and type
open(video)%open video
writeVideo(video,M)%write frame on video
close(video) %close video
ODE45:
Comment \"ODE45\" solves Ordinary Differential Equation. Initial displacement, velocity, and time interval gave as input. The pendulum equation simplified and created as a function. Here,
Assume,
D(Theta)=theta1; ---------->2
D(Theta)=D(theta1)=theta2; --------->3
Where D=d/dt.
Substitute 2,3 in 1 Then,
dtheta1_dt=theta2; ---------->4
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1); ---------->5
function [x]=fun1(theta,b,g,L1,m)
theta1=theta(1);
theta2=theta(2);
dtheta1_dt=theta2; %from equation 4
dtheta2_dt=-(b/m)*theta2-(g/L1)*sin(theta1); %from equation 5
x=[dtheta1_dt;dtheta2_dt]
end
GRAPH:
ANIMATION:
Using ode45 we get velocity and displacement values as Theta. This value then converts into angles and string moves according to concurrent angle. Using \"movie(),VideoWriter(),open(video) ,writeVideo(video,M),close(video)\" comment animation created.
YOUTUBE LINK: https://www.youtube.com/watch?v=Td2mjplpEhA
CONCLUSION:
Graph and animation created using Matlab.