ode for simple pendulum without damping
% ODE for simple pendulum (assignment)
clear all
close all
clc
%Given
g=9.81;
l=0.5;
m=0.5;
%Initial condition
theta = [0;5];
%Time span
t_span = linspace(0,8,500);
%solving ode
[t,results] = ode45(@(t,theta) ode_func_nd(t,theta,g,l,m),t_span,theta);
%plot
figure(1) %part 1 plot
plot(t,results(:,1),\'color\',\'b\')
hold on
plot(t,results(:,2),\'color\',\'r\')
%%Animating the simple pendulum
figure (2)
angle = results(:,1);
Xa1=1.5;
Ya1=2;
Xa2=-1.5;
Ya2=2;
%Creating loop
ct=1;
for i=1:length(t)
x0 = 0;
y0 = 2;
x1 = l*sin(angle(i));
y1= -l*cos(angle(i));
r = 0.15;
xc = x1;
yc = y1;
theta_c = linspace(0,2*pi);
x_c = r*cos(theta_c) + xc;
y_c = r*sin(theta_c) + yc;
plot([x0 x1],[y0 y1],[Xa1 Xa2],[Ya1 Ya2],x_c,y_c,\'linewidth\',2)
% plot([Xa1 Xa2],[Ya1 Ya2])
axis([-2 2 -2 3])
pause(0.03)
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter(\'pendulum_nodamping.avi\',\'uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)
For function to solve ODE
function [ansode] = ode_func_nd(t,theta,g,l,m)
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta2;
dtheta2_dt = -((g/(l*m))^2) *sind(theta1);
ansode = [dtheta1_dt;dtheta2_dt];
end
%%link for the animation%%
https://drive.google.com/open?id=1zFJAYy4SsZ_Ue-YCfpDMJYkF3mKcDdAn