A program to solve second order differential equations and simulate
%pendulum motions
clear all
close all
clc
%initial conditions for solving differential equations
initial_condition = [0;3];
%inputs
l = 1;
m = 1;
b = 0.05;
g = 9.81;
time = linspace(0,20,100);
%solving ODE
[t,parameters] = ode45(@(t,initial_condition) pendulum(t,initial_condition,l,m,b,g),time,initial_condition);
%plotting results
figure(1)
plot(t,parameters(:,1),'-o',t,parameters(:,2),'*')
legend('Angular Displacement','Angular Velocity');
xlabel('Time')
%creating animation
x0 = 0; y0 = 0; temp =1;
y1=-5;
for i=1:100
figure(2)
x1=y1*sind(parameters(i,1));
plot([x0 x1],[y0 y1],'linewidth',3)
hold on
plot(x1,y1,'o','MarkerSize',15,'MarkerEdgeColor','red','MarkerFaceColor','red')
hold off
axis([-5 5 -10 10]);
pause(0.3);
M(temp)=getframe(gcf);
temp = temp+1;
end
simulation= VideoWriter('Simple Pendulum','Uncompressed AVI');
open(simulation);
writeVideo(simulation,M);
close(simulation)