https://youtu.be/zdhgBlqv990
% solving second order differential equation
clear all
close all
clc
% Input parameters
b = 0.05;% damping co-efficient
g = 9.81;%m/s2 - gravity
l = 1;%m - Length
m = 1;%kg - Mass
%initial condition
theta_0 = [0;3];%[Angular Displacement,Velocity]
%time points
t_span = linspace(0,10,500);%time span between 0 to 20 sec
%solve ODE
[t, results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%results as column vector - displacement and velocity
subplot(5,4,[13,14,17,18]);
plot(t,results(:,1))
hold on
plot(t,results(:,2))
hold off
ylabel('Displacement and Velocity')
xlabel('Time')
legend('Displacement','Velocity')
subplot(5,4,[15,16,19,20]);
plot(results(:,1),results(:,2))
xlabel('Position-Radians');
ylabel('Angular Velocity');
ct=1;
for i=1:length(results(:,1))
x0=0;
y0=0;
x1=1*sin(results(i,1));
y1=-1*cos(results(i,1));
subplot(5,4,[1:12]);
plot([-1 1],[0 0],'linewidth',4,'color','k');
hold on
line([x0 x1],[y0 y1],'linewidth',2,'color','b');
hold on;
plot(x1,y1,'-o','markers',20,'markerfacecolor','r'); %plotting sphere using marker and facecolor
grid on
axis([-1.5 1.5 -1.5 0.5])
pause(0.0003);
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
% command to create a movie file with all frames generated
movie(M)
% command to create a video file
videofile = VideoWriter('Simple_pendulum.avi','uncompressed AVI');
open(videofile)
% command to write a frame as videofile
writeVideo(videofile,M)
close(videofile)