Simulation of pendulum using second order ODEs. In matlab.
`((d^2theta)/(dt^2))+(b/m)*((d theta)/(dt))+(g/l)*(sintheta)=0`
clear all
close all
clc
%inputs
b=0.5;
g=9.81;
l=1;
m=1;
%initial conditions
theta_0=[0;3];
%time plots
t_span = linspace(0,20,500);
%solve ODE
[t,results]= ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%initial conditions
x0=0;
y0=0;
res = results(:,1);
ct=1;
x2=l*sin(results);
y2=l*cos(results);
%animating the pendulum motion
for i=1:length(t_span)
angle = res(i);
x1=l*sin(angle);
y1=-l*cos(angle);
plot([x0,x1],[y0,y1],'linewidth',3,'color','b');
%plotting bob
pos = [x1-0.1 y1-0.1 0.2 0.2];
rectangle('Position',pos,'Curvature',[1 1],'facecolor','r');
%viscircle([x1 y1], 0.2)
%plotting support
rectangle('position',[-0.4 0 0.8 0.2],'facecolor','c');
axis([-1 1 -1.4 0.2]);
pause (0.03);
M(ct) = getframe(gcf);
ct= ct+1;
end
%Movie
movie(gcf,M);
videofile = VideoWriter('pendulum_animation.avi','Uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);
%plotting
figure(2)
plot (t_span,results(:,1));
hold on;
plot (t_span,results(:,2));
ylabel('Plot');
xlabel('Time (in secs)');