Week 3 - Solving second order ODEs
clc
clear all
close all
%Input Parameters
b = 0.05;
g = 9.81;
l = 1;
m = 1;
%Initial Conditions
alpha0 = [0;3];
time_span = linspace(0,20,500);
%Function Call Statement
[t,results] = ode45(@(t,alpha) pendulum_func(t,alpha,b,g,l,m),time_span, alpha0);
%Plot
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('Time')
ylabel('Plot')
grid on
figure(2)
%counter for frame
ct = 1;
for i = 1:length(time_span)
x0 = 0;
y0 = 0;
x1 = l*sin(results(i,1));
y1 = -l*cos(results(i,1));
plot([-0.6 0.6],[0 0],'linewidth',12)
hold on
plot([x0 x1],[y0 y1],'color','b','linewidth',3)
axis([-3 1 -3 1])
hold on
plot(x1,y1,'o','MarkerSize',35,'MarkerFaceColor','g')
hold off
axis([-1 1 -1.5 1])
pause(0.001)
M(ct) = getframe(gcf);
ct = ct+1;
grid on
end
%Animation
movie(M)
videofile = VideoWriter('ode_2nd_order.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
FUNCTION PROGRAM
function [dalpha_dt] = pendulum_func(t,alpha,b,g,l,m)
alpha1 = alpha(1);
alpha2 = alpha(2);
dalpha1_dt = alpha2;
dalpha2_dt = -(b/m)*alpha2-(g/l)*sin(alpha1);
dalpha_dt = [dalpha1_dt; dalpha2_dt];
end
OUTPUTS
PROJECT PUBLISH LINK
https://skill-lync.com/projects/ode-and-pendulum-animation
YOUTUBE LINK