Movie Generation of motion of simple pendulum
Solving Second Order ODE:
The motion of a simple pendulum is defined with the following equation.
simplication of the equation.
%Coding to animate a simple pendulum motion
clear all
close all
clc
%General pendulum specifications
b=0.05;
g=9.81;
l=1;
m=1;
%Input condition
theta_0=[0;3];
%Time points
t_span=linspace(0,20,500);
%solving second order ODE
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
%Ploting change in Displacement & Velocity with respect to time
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel(\'plot\')
xlabel(\'time\')
ct=1;
%For loop for creating Animation
for i=1:length(results(:,1))
x0=0;
y0=0;
x1=l*sin(results(i,1));
y1=-l*cos(results(i,1));
figure(2)
plot([-1 1],[0 0],\'linewidth\',4,\'color\',\'r\');
hold on
line([x0 x1],[y0 y1],\'linewidth\',4,\'color\',\'k\');
hold on
plot(x1,y1,\'O\',\'markers\',20,\'markerfacecolor\',\'c\');
grid on;
axis([-1.5 1.5 -1.5 0.5])
pause(0.01);
hold off
M(ct)=getframe(gcf);
ct=ct+1
end
%Movie Generation
movie(M)
video_file=VideoWriter(\'pendulum.avi\',\'uncompressed AVI\')
open(video_file)
writeVideo(video_file,M)
close(video_file)
Code Explanation:
General pendulam specifications:
- The values for damping coefficient (b), Gravity (g), Length of the string (l) & mass of the ball (m) are taken from the question given.
Input condition:
- The displacemnt is 0 and velocity is 3 at the starting point (i.e) at t=0.
Time points:
- To specify the values for \'t\' linspace command is used .
Solving second order ODE:
- ode45 command is used to solve the ODE with respect to time and to store the values for displacement & velocity.
- The ode_func is a function used to give values for first order & second order ODE.
Calling Function:
%Defining a function to solve ODE
function [dtheta_dt]=ode_func(t,theta,b,g,l,m)
theta1=theta(1)
theta2=theta(2)
dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end
- Specify the variables theta1 & theta2
- The derivation of theta1 will be theta 2 & the derivation of theta2 will be -(b/m)*theta2 -(g/l)*sin(theta1) from the simplified equation.
- The function will give the output for displacement & velocity.
Ploting the change in displacement & velocity with respect to time:
- Ploting the first column of results will give the curve for displacement.
- Plotting the second column of results will give the curve for velocity.
For loop for creating animation:
- specify the length of itration to be the length of the column of results.
- specify the values for x0 & y0 as 0 as they are the starting coordinates.
- x1 & y1 will be denoting the the end point of the pendulum string.
- plot a straight line through the coordinates x0 & yo which will be acting as a fixed column.
- give hold on command to pause the plotes column.
- use line command to join x0 y0 with x1 y1 so as to get the string in the plot .
- The values of x1 & y1 will be changing according to the time.
- Thus the string line will be moving for each itration.
- use pause command to hold the frame for specified time.
- hold off command can be used to show a single frame at a time.
- plot the ball using \'o\' command & mention its size & color using \'markers\' and \'markerfacecolor\' commands.
Result:
Figure (1):
Figure (2):
Error 1:
- Did\'nt specify the variables before calling the function in the ode45 command.
- @(t,theta) is included before calling the function to overcome the error.
Error 2:
- While defining x1 & y1 the equations are written as
x1=l*sind(results(i,1));
y1=-l*cosd(results(i,1));
- in figure(2) there is only less movement of the string.
- This error is because the values in results is in radians.
- the commands sind & cosd are only used for the values in degrees.
- By changing the equation as
x1=l*sin(results(i,1));
y1=-l*cos(results(i,1)); The error can be overcome.