Week 3 - Solving second order ODEs
For solving The SHM motion of the pendlum the above equaltion used in which
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.
Given parameters
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
Code in MATLAB
clear all;
close all;
clc
b=0.02;
g=9.8;
l=1;
m=1;
%initial condition
theta_0=[0;5];
%time span
t_span=linspace(0,20,500);
%solve ODE
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
plot(t,results(:,1));
hold on
plot(t,results(:,2));
ylabel('plot');
xlabel('time');
%animating the pendulum motion
ct=1;
for i=1:length(results(:,1))
x0=0;
y0=0;
x1=l*sin(results(i,1)+pi);
y1=l*cos(results(i,1)+pi);
plot([0 0],[-1.5 0.5]);
hold on
plot([x0 x1],[y0 y1],'linewidth',1,'color','b');
axis([-1.5 1.5 -1.5 0.5])
plot(x1,y1,'o','Markersize',20,'color','r')
pause(0.09)
M(ct)= getframe(gcf)
hold off;
ct=ct+1;
end
%to create the animation file
movie(M)
v_f=VideoWriter('Simple_Pendulum','Uncompressed AVI');
open(v_f)
writeVideo(v_f,M)
close(v_f)
Sub program in MATLAB
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
Explanations
1. First we formulate the SHM equation for Pendulum.
2. Break the the 2nd order ode in the 1st order ODE in rough which will be used in sub program.
3. Puted the given parameters in coded form.
4. Ode solver ode45 is used in this matlab program.The results are stored in an array of row atrix of two coloumns, for the values of displacement and velocity at different time periods.
5. Ode_tunc is the function made to solve the given equation . where the asumptions are made in the from of code.
6. Graph is plotted to get the wave form of the displacement and the velocity from the initial condtions.
7. Animating the transient motion of the simple pendulum:
Here we use the concept of the forloop .as the the bob moves with a velocity ,and displaces for the different time period.And recorded it and animated the path of the oscilations
