Week 3 - Solving second order ODEs
Explanation:- i got error in video that not playing properly but i create images and then convert it into video.
clear all
close all
clc
b=0.05;
g=9.81;
l=1;
m=1;
%initial condition
theta_0 =[0;3];
%time points
t_span = linspace(0,20,100);
%solve ode
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
ct=1;
for i=1:length(results(:,1));
x0=0;
y0=0;
x1=l*sin(results(i,1));
y1=-l*cos(results(i,1));
subplot(5,4,[1,2,3,4,5,6,7,8,9,10,11,12]);
plot([-1 1],[0 0],'linewidth',4);
hold on
line([x0 x1],[y0 y1],'linewidth',2,'color','b');
hold on
plot(x1,y1,'-o','markers',20,'MarkerFaceColor','r');
grid on
axis([-1.5 1.5 -1.5 0.5])
pause(0.01);
hold off
K(ct) =getframe(gcf);
pause(0.9)
image_name = sprintf('%05d.png',ct);
saveas(gca,image_name)
ct=ct+1;
end
movie(K)
videofile=VideoWriter('simplependulum_simulation.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,K)
close(videofile)
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