Motion of a pendulum Solution of the ODE using MATLAB
Program to simulate the transient behavior of a simple pendulum;
- ODE[Ordinary Differential Equation] - A differential equation containing one or more functions of one independent variable and the derivatives of those functions.
- Second Order[ODE] - The equation consists of a second derivative and not more than that.
- Simplification of the given Pendulum ODE;
`(d^2theta)/dt^2+b/m*(d*theta)/dt+g/lsintheta = 0`
Assume `theta = theta_1`
Differentiating `theta`gives,
`(d(theta))/dt = (d(theta_1))/dt`
let, `(d(theta_1))/dt = theta2`
Now, Differentiating `(d(theta))/dt` gives,
`(d^2(theta))/dt^2 = (d^2(theta_1))/dt^2 = d/dt((d(theta_1))/dt) = d/dt(theta_2)`
Hence,
The given ODE becomes
`(d(theta_2))/dt+b/m(theta_2)+g/Lsintheta_1 = 0`
- Given values
- L = 1m
- m = 1 kg
- b = 0.05
- g = `9.81m/s^2`
- Sample working Code;
clear all
close all
clc
%given inputs of the pendulam and forces acting on the pendulam.
%where L,m,b,g are length,mass,damping coefficient and gravity respectively
L = 1;
m = 1;
b = 0.05;
g = 9.81;
%the initial conditions for the pendulum[displacement;veloctiy]
theta_0 = [0;3];
%given work time of the pendulam
P_travel = linspace(0,20,228);
%solving ode
[t, result] = ode45(@(t,theta) ode_function(t,theta,b,g,L,m), P_travel, theta_0);
%plotting the result
%plotting the curves of the Displacement
plot(t,result(:,1))
hold on
%plotting the result of the velocity
plot(t,result(:,2))
xlabel(\'time\')
ylabel(\'Displacement&angular velocity\')
legend(\'displacement\',\'angular_velocity\')
grid on
hold off
%creating the avi file(animation of pendulum travel)
ct = 1;
for i = 1:length(result(:,1))
%the base of the pendulum attach has zero motion.
x0 = 0;
y0 = 0;
%the free end of the pendulum in motion
x1 = L*sin(result(i,1));
y1 = -L*cos(result(i,1));
%plotting the base attachment
plot([-0.5 0.5],[0 0],\'linewidth\',2,\'color\',\'b\')
hold on
%plotting the free end
plot([x0 x1],[y0 y1],\'linewidth\',2,\'color\',\'r\')
%plotting the face(ball/nob) of the pendulum
plot(x1,y1,\'o\',\'markers\',28,\'markerfacecolor\',\'g\');
hold off
grid on
axis([-1.5 1.5 -1.5 1.5])
pause(0.228)
%creating the M array containing all the resulting frames
M(ct) = getframe(gcf);
ct = ct+1;
end
%creating the movie of the array M.
movie(M)
videofile = VideoWriter(\'simple_pendulum.avi\',\'Uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)
- Sample Function code;
%creating the function of the ode of pendulum
function [dtheta_dt] = ode_function(t,theta,b,g,L,m)
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta(2);
dtheta2_dt = -(b/m)*theta2-(g/L)*sin(theta1);
dtheta_dt = [dtheta1_dt;dtheta2_dt];
end
- Graphical result;
- Link redirecting to the animation of the motion of pendulum;
- Explanation;
- The given ODE is in second-order and so in order to generate the MatLab code that solves that ODE, it is first simplified into the single order (or) simple ODE.
- The simplification of the ODE is carried out as shown above.
- The simplified ODE is used to create the function code to carry out the program.
- The ode function contains the variables that are prespecified.
- The ode function has further simplified the form of the given ODE.
- This ode function is incorporated in the main program.
- The program has a linspace command that helps in generating equally spaced numbers or constants between prespecified numbers.
- Theta is given as 0 & 3, where 0 is the angular displacement and 3 is angular velocity which is given.
- Solving the ode using \'ode45\' command which integrates the ode function and also the total travel of the pendulum.
- plotting the results for the displacement and velocity curve.
- labeling THE AXES USING THE \'LABEL\' COMMAND.
- The legend command helps in naming/identifying the curves.
- Finally creating a movie/ animation of the results by compiling all the results of the given ode between the specified time period.
- First, creating the model of the pendulum by giving its dimensional analysis.
- As we know that the pendulum displaces according to the time, we just integrate the results of the angular displacement to represent the motion of the pendulum.
- At the base joint, the displacement is zero. Hence, x0 and y0 are 0.
- At another end of the pendulum, the angular displacement occurs.
- The pendulum has length L and it displaces against gravity. Hence, the equation is used as shown in the program.
- Plotting the pendulum motion at each given interval creates each result.
- The plot has the pendulum ball created by using the markers command where the \'o\' command specifies the shape and marker face color is given as one\'s perspective.
- The \'pause\' command avoids the overwriting of the results at each interval.
- Creating an array M which is subjected to carry all resulting frames and then creating a movie of M.
- The attached graph sample shows the result of the ode function.
- The provided link shows the animation of the motion of a pendulum.
- CONCLUSION;
The report gives the idea of how the ODE is solved using the MatLab program and its consequences in detail.