Week 3 - Solving second order ODEs
AIM/OBJECTIVE:
Write a program in Matlab that will simulate the pendulum motion, just like the one shown in the start of this challenge.
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
CONCEPT:`(d theta) / (d t) = (d theta 1) / (d t) = theta2`
(0)
Breaking the below second order ode into first order ode and the formulating in matlab will give us the required answers.
`(d theta) / (d t) = (d theta 1) / (d t) = theta2` (1)
`(d^2 theta) / (dt^2) = (d^2 theta 1) / (d t^2) = d/dt((d theta1)/dt) =d/dt(theta 2)` (2)
By substituting 1 and 2 in 0 we get:
Final equation:
`(d theta2)/dt + b/m(theta2)+g/l(sintheta1) = 0`
MAIN CODE:
clear all
close all
clc
% Parameters given in the question
b=0.05;
g=9.81;
l=1;
m=1;
%initial condition
theta_0 = [0;3];
% time points
t_span = linspace(0,20,400);
% 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')
legend('Displacement','Velocity')
figure(2)
ct = 1;
for i = 1:length(results(:,1))
Theta_1 = results(i,1);
% coordinates
x0 = 0;
y0 = 0;
x1 = 1*sin(Theta_1);
y1 = -1*cos(Theta_1);
%plotting
plot([-0.5 0.5], [0 0],'color','y')
line([x0 x1],[y0 y1],'color','b')
hold on
plot(x1,y1,'marker','o','markersize',20,'markerfacecolor','r')
grid on
axis([-1.5 1.5 -1.5 1])
hold off
pause(0.005)
M(ct) = getframe(gcf);
ct=ct+1;
end
movie(M)
videofile = VideoWriter('Pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
FUNCTION USED:
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
RESULTS:
D-T and V-T graph:
PENDULUM ANIMATION YOUTUBE LINK:
https://youtu.be/jkpfLrNV_ME