Week 3 - Solving second order ODEs
%%
clear all;
close all;
clc
%%
L=1;
t=linspace(0,20,150);
x0=[0 3];
%this below step is for solving differential equation using ode45 (matlab in built funtion)
[t x] = ode45('pendulum_2nd_order',t,x0);
%%
%plotting for tracing of angular velocity and angular displacement
subplot(2,1,1)
plot(t,x(:,1),'-b','linewidth',3);
xlabel('time t in sec');
ylabel('Angular displacement');
subplot(2,1,2)
hold on;
plot(t,x(:,2),'-k','linewidth',3);
xlabel('time t in sec');
ylabel('Angular velocity');
grid on;
%%
% ANIMATION
X0=0;
Y0=0;
ct=1; % counter actually represents each and every frame
for i=1:length(t);
X1=L*sin(x(i,1));
Y1=-L*cos(x(i,2));
figure(2)
plot([-1 1],[0 0],'linewidth',6,'color','g');
axis([-2 2 -2 2]);
line([X0 X1],[y0 y1],'linewidth',2,'color','b');
hold on;
plot(X0,Y0,'O','markersize',3,'marker face color','r');
plot(X1,y1,'O','markersize',20,'marker face color','r');
title('Damping of simple pendulum');
legend('support rod','length of pendulum','fixed point','Bod of pendulum');
grid on;
hold off;
m(ct)=getframe(gcf);
ct=ct+1;
end
movie(m);
videofile=videowriter('simple_pendulum','Uncompressed AVI');
open(videofile);
writevideo(videofile ,M);
close(videofile);