clear all
close all
clc
b=0.05; % damping force
g=9.81; %gravity in m/s^2
l=1;%length of pendulum in mtr.
m=1; %mass of ball in kg
%starting points fixed position
x0=0;
y0=0
%angular displacement and angular velocity at time t=0
theta_0=[0;3];
%timespan of pendulum
t_span=linspace(0,20,500);
% solve ode45
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
%plotting figure
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2));
% animation of pendulum by using for loop
ct=1
for i= 1:length(t)
y1=-l*cos(results(i,1)) % position of ball repective to the time
x1=l*sin(results(i,1))
figure(2)
plot([x0,x1],[y0,y1],'linewidth',3,'color','b');
hold on
plot(x1,y1, '.','markersize',20)
text(0,-2,['time=',num2str(t(i))])
axis([-1.5 1.5 -3 0])
hold off
pause(0.01);
end
%creating video files
movie(M)
videofile= VideoWriter('pendulum_challenge.AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)