program to simulate the transient behaviour of a simple pendulum and to create an animation of it s motion
Step wise Explanation of code
- First i given the input values ,intical conditions and time interval.
- The simple pendulum equation is an second order ode.so i splitted it into two first order ode\'s .
- i created a matlab function which is used in solving the ode.
- Then using ode45 solver i solved the second order ode .
- next i plotted figure 1 which tells us the transient behaviour of the simple pendulum.
- next step is to create the animation of the simple pendulum
- we have to calculate end point of the pendulum by using the for loop in order to get animation
- first plot the end point of the pendulum using marker and add axis limits
- next by using the line command we plot a line of simple pendulum
- by using title and num2str command we keep the title and the time of the animation
- to get a movie we have to use getframe which gets the images of the plot and combine all images to look like ananimation
- in the last we save the video in the desired format.
Errors Facing While programming
i got a lot of errors while programming the animation part
the first wrong i did is that i did not used the for loop and went in a different approach by using the angles then i realized that it is wrong.then i again whatched the video of robot arm kinematics the first challenge from that video i started using the for loop and the getframe commands for how to animate.
the second big errors i got in the execution of the for loop for finding the end points of the simple pendulum it took me a long time i referred the documentation that matlab provides and some videos from that i finally got executed the for loop
the next problem is in plotting the values.first i directly ploted the pendulum line using the line command and the marker then the animation is coming but the images are not removing.then i again ploted only the point and gave axis limits then i plotted then i plottedcthe2 line then the animation worked fine.
almost it took 2 days for me to sove this challenge.
clear all
close all
clc
% inputs
b=0.05;
g=9.81;
l=1;
m=1;
% initial condiations
theta_0 = [0;3];
% time points
t_span = linspace(0,20,500); %time interval
% ode solving
[t, results] = ode45(@(t,theta) ode_func(t, theta,b,g,l,m),t_span, theta_0);
figure(1)
axis([0 12 -3 3]);
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel(\'time\')
ylabel(\'plot\')
o = [0 0]; % origin point for pendulum
% loop for animation
for i =1:length(t)
% end point for pendulum
x(i) = l*sin(results(i,1));
y(i) = -l*cos(results(i,1));
%pendulum line
figure(2)
plot(x(i),y(i),\'.\',\'markersize\',60);
axis([-1.05 1.05 -1.5 0.2]);%axis limit
line([o(1) x(i)], [o(2) y(i)],\'color\',\'r\',\'Linewidth\',2);
title([\'PENDULUM MOTION at T =\',num2str(i*(20/500)), \'s\']);% pendulum motion for 20 seconds
f= getframe; % captures the images and form a movie
end
v = VideoWriter(\'ode_2nd_order_pendulum.avi\',\'Uncompressed AVI\'); % saves the video
open(v);
writeVideo(v,f); % read the video file
close(v);