codes are being shown below
clear all
close all
clc
b=0.05; % assigining values for damping co-efficient,gravity,length of arm and mass.
g=9.81;
l=2;
m=0.1;
% initial condition
theta=[0 5]; %theta initial condition
%time points
t=linspace(0,10,150); % time duration is being given
%solve ode
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t,theta); % calling a function
disp=results(:,1); %display the results two rows
plot(t,results(:,1)) % ploting the results with time and angle
hold on
plot(t,results(:,2)) % ploting the results with time and angle in second row
ylabel('plot') % label the graph
xlabel ('time')
%plotting animation
X0=0; % assigining values for x0 and y0 as 0
Y0=0;
ct=1; % ct is used to count the each time it executes the frame
for i=1:length(disp) % initilize the value of i from 1 to display length
X1=l*sin(disp(i,1)); % value of X1 and Y1 are given
Y1=-l*cos(disp(i,1));
figure(2)
plot([-2 2],[0 0],'linewidth',6,'color','b'); % the beam is drawn
hold on; % waits for next image to draw on same screen
plot([X0 X1],[Y0 Y1],'linewidth',2,'color','r'); % length of the rm is drawn
plot(X0,Y0,'o','markersize' ,3,'markerface','y'); % supporting point is being drawn
plot(X1,Y1,'o','markersize' ,20,'markerface','r'); % ball is being drawn
title('Motion of pendulum'); % title for the image is being given
legend('support beam','Arm length','pivot end','movable end'); % legend for each part is being given
axis([-2 2 -2 2]); % axis of the image is being set
hold off
%pause(0.3);
M(ct)=getframe(gcf); % get the each and every frame captured
ct=ct+1; % increments the next counter by 1
end
movie(M) % movie command is being used
videofile = Videowriter('motion_of_pendulum','Uncompressed AVI'); %video file is being assigned with video writer
open(videofile); % the video file is opended
writeVideo(videofile,M); % the file is being written
close(vieofile); % close the video file