Week 3 - Solving second order ODEs
BODY OF THE PROGRAM CONTENT :
% Solving the Second Order ODE’s
clear all
close all
clc
%inputs
b=0.05;
g = 9.81;
l = 1;
m = 1;
%initial conditions
theta_0 = [0;3];
%time _span
t_span = linspace(0,20,150);
% solve ode
[t,results] =ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
ODE FUNCTION LOOP
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
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('velocity')
xlabel('displacement')
hold off
theta = results(:,1);
ct = 1;
for i = 1:length(theta);
THETA = theta(i) ;
% coordinate
x = 0;
y = 0;
x1 = l*sin(THETA);
y1 = -l*cos(THETA);
%plotting
figure(2)
plot([x x1],[y y1],'linewidth',3)
hold on
plot(x1,y1,'o','MarkerSize',15,'MarkerFaceColor','r','MarkerEdgeColor','r')
axis([-1 1 -1.5 1.5])
pause(0.2)
M(ct)=getframe(gcf)
hold off
ct = ct+1
end
movie(M)
videofile = VideoWriter('pendulum','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
animation video link
https://drive.google.com/file/d/1iSn2202gY4tL6ExDviPjPoEAbvgzQUuZ/view?usp=sharing