SImple pendulum animation using matlab
Aim: To Write a program in Matlab\\Octave that will simulate the pendulum motion, just like the one shown in the start of this challenge.
Apparatus: Matlab
Procedure:
1. create clear, close commands inorder to clear the tabs and erros
2. create ode_func based on the equation given
3. use these function to create pendulum plot by using the intial parameters as given.
4. use for loop and create a range of variable to create the plot of pendulum motion
5. use marker command and use letter \'0\' in order to create the bob shape at the end of the plot
6. use intial values of x and y as zero to create line to pendulum points
7. capture the frames using movie and getframe commands to create video file.
8. Run the code and save the video.
clear all
close all
clc
%fixed input parameters for simple pendulum
l=1;
m=1 ;
b=0.05;
g=9.81;
theta_0=[1;0];
x1=0;
y1=0;
% formulae for pendulum calculation
tspan=linspace(0,15,200);
[t,result]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),tspan,theta_0);
pause(0.1);
k=result(:,1);
u=k.\';
f=1;
% creating loop to plot single points on each iteration to have a momemtum
for i=1:length(u)
motion=u(i);
x2=(sin(motion));
y2=-(cos(motion));
plot([x1 x2],[y1 y2],\'linewidth\',2)
yline(0);
yline(0.1);
yline(0.05,\'--b\')
xline(0,\'--r\');
hold on
plot([x2],[y2],\'o\',\'MarkerSize\',20,\'MarkerFaceColor\',[1 .6 .6]) ;
plot(y2,\'-\')
hold off
axis([-1.5 1.5 -1.5 0.2]) ;
pause(0.02);
M(f)=getframe(gcf);
f=f+1;
end
movie(M) % play the movie using frames stored in M once
videofile = VideoWriter(\'ped.avi\',\'Uncompressed AVI\'); % function to assign video file format and create video
open(videofile) % opening the video file
writeVideo(videofile,M) % writing the frames stored in M to videofile.
close(videofile) % close and save the video file in saved location of programm
Conclusion:
using new commands yline ive been able to create the dashed lines..
learned how to use sin and cos functions for creating plots
learned how to convert columns to row
learned where to use hold off command for releasing the values on the plot