Week 3 - Solving second order ODEs
Simulation of simple pendulum by solving matlab
Explanation: ODE represents the transients behaviour of simple pendulum,here the ODE is second order differential equation to solve in matlab we are breaking into two first order differential equation
Steps During Performing program:
Step1: Derive the equation of motion
Step2: Linearize the equation of motion
Step3: Solve equation of motion analytically
Step4: physical significance of omega0
step5: plot pendulum motion
Inputs:
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
Main Code:
clc;
close all;
%here we provide initial condition for displacement and velocity of pendulum
initial_condition=[0,3];
%here we provide time step or dependent variable
t=linspace(0,20,200);
%here is command to solve ODE
[t,y]=ode45('myfun1',t,initial_condition);
%plotting graph between displacement,velocity Vs time we have
figure(1)
title('Displacement&velocityVs time')
plot(t,y(:,1),'color','r','linewidth',2)
hold on
plot(t,y(:,2),'color','b','linewidth',2)
xlabel('time')
ylabel('velocity&displacement')
%motion animation
%here we provide coordinate of fixed end of pendulum string and also initiate counter for plotting figure
figure(2)
xlim([-1 1]);
ylim([-1.5 0]);
grid on;
x0=0;
y0=0;
l=1;
%here we use for loop to store above displacement value in zeroth column of array theta to array a
for i=1:length(t)
%here we calculate x1 and y1 and then alter sign to shift graph to third and fourth quadrunts
x1=-l*sin(y(i,1));
y1=-l*cos(y(i,1));
grid on;
string=line([x0,x1],[y0,y1]);
center=[x1 y1];
pendulum=viscircles(center,0.1);
F(i)=getframe(gcf);
delete(string);
delete(pendulum);
end
myvideo=VideoWriter('pendulum_Ketan.avi');
open(myvideo)
writeVideo(myvideo,F);
close(myvideo);
Function Code:
function [ydot]=myfun1(t,y)
l=1;% metre,
m=1;% kg,
b=0.05;
g=9.81; % m/s2.
ydot=zeros(2,1);
ydot(1)=y(2);
ydot(2)=(-b/m)*y(2)-(g/l)*sin(y(1));
end
Output:
Conclusion: