Simulate the Motion of the Periodic Swing of a Pendulum
This example shows how to simulate the motion of a simple pendulum using Symbolic Math Toolbox™. Derive the equation of motion of the pendulum, then solve the equation analytically for small angles and numerically for any angle.
Step 1: Derive the Equation of Motion
The pendulum is a simple mechanical system that follows a differential equation. The pendulum is initially at rest in a vertical position. When the pendulum is displaced by an angle and released, the force of gravity pulls it back towards its resting position. Its momentum causes it to overshoot and come to an angle (if there are no frictional forces), and so on. The restoring force along the motion of the pendulum due to gravity is 。因此,根据牛顿第二定律,质量times the acceleration must equal 。
symsmagtheta(t)eqn = m*a == -m*g*sin(theta)
eqn(t) =
For a pendulum with length , the acceleration of the pendulum bob is equal to the angular acceleration times 。
。
Substitute for
by usingsubs
。
symsreqn = subs(eqn,a,r*diff(theta,2))
eqn(t) =
Isolate the angular acceleration ineqn
by usingisolate
。
eqn = isolate(eqn,diff(theta,2))
eqn =
Collect the constants and into a single parameter, which is also known as thenatural frequency。
。
symsomega_0eqn = subs(eqn,g/r,omega_0^2)
eqn =
Step 2: Linearize the Equation of Motion
The equation of motion is nonlinear, so it is difficult to solve analytically. Assume the angles are small and linearize the equation by using the Taylor expansion of 。
symsxapprox = taylor(sin(x),x,'Order',2); approx = subs(approx,x,theta(t))
approx =
The equation of motion becomes a linear equation.
eqnLinear = subs(eqn,sin(theta(t)),approx)
eqnLinear =
Step 3: Solve Equation of Motion Analytically
Solve the equationeqnLinear
by usingdsolve
。Specify initial conditions as the second argument. Simplify the solution by assuming
is real usingassume
。
symstheta_0theta_t0theta_t = diff(theta); cond = [theta(0) == theta_0, theta_t(0) == theta_t0]; assume(omega_0,'real') thetaSol(t) = dsolve(eqnLinear,cond)
thetaSol(t) =
Step 4: Physical Significance of
The term is called thephase。The cosine and sine functions repeat every 。The time needed to change by is called the time period.
。
The time period is proportional to the square root of the length of the pendulum and it does not depend on the mass. For linear equation of motion, the time period does not depend on the initial conditions.
Step 5: Plot Pendulum Motion
Plot the motion of the pendulum for small-angle approximation.
Define the physical parameters:
Gravitational acceleration
Length of pendulum
gValue = 9.81; rValue = 1; omega_0Value = sqrt(gValue/rValue); T = 2*pi/omega_0Value;
设置初始条件。
theta_0Value = 0.1*pi;% Solution only valid for small angles.theta_t0Value = 0;% Initially at rest.
Substitute the physical parameters and initial conditions into the general solution.
var = [omega_0 theta_0 theta_t0];值=(ω_0Value theta_0Value theta_t0Value]; thetaSolPlot = subs(thetaSol,vars,values);
Plot the harmonic pendulum motion.
fplot(thetaSolPlot(t*T)/pi, [0 5]); gridon; title('Harmonic Pendulum Motion'); xlabel('t/T'); ylabel('\theta/\pi');
After finding the solution for , visualize the motion of the pendulum.
x_pos = sin(thetaSolPlot); y_pos = -cos(thetaSolPlot); fanimator(@fplot,x_pos,y_pos,'ko','MarkerFaceColor','k','AnimationRange',[0 5*T]); holdon; fanimator(@(t) plot([0 x_pos(t)],[0 y_pos(t)],'k-'),'AnimationRange',[0 5*T]); fanimator(@(t) text(-0.3,0.3,"Timer: "+num2str(t,2)+" s"),'AnimationRange',[0 5*T]);
Enter the commandplayAnimation
to play the animation of the pendulum motion.
Step 6: Determine Nonlinear Pendulum Motion Using Constant Energy Paths
To understand the nonlinear motion of the pendulum, visualize the pendulum path by using the equation for total energy. The total energy is conserved.
Use the trigonometric identity and the relation to rewrite the scaled energy.
Since energy is conserved, the motion of the pendulum can be described by constant energy paths in the phase space. The phase space is an abstract space with the coordinates
and
。Visualize these paths usingfcontour
。
symsthetatheta_tomega_0E(theta, theta_t, omega_0) = (1/2)*(theta_t^2+(2*omega_0*sin(theta/2))^2); Eplot(theta, theta_t) = subs(E,omega_0,omega_0Value); figure; fc = fcontour(Eplot(pi*theta, 2*omega_0Value*theta_t), 2*[-1 1 -1 1],。..'LineWidth', 2,'LevelList', 0:5:50,'MeshDensity', 1+2^8); gridon; title('Constant Energy Contours in Phase Space ( \theta vs. \theta_t )'); xlabel('\theta/\pi'); ylabel('\theta_t/2\omega_0');
The constant energy contours are symmetric about the axis and axis, and are periodic along the axis. The figure shows two regions of distinct behavior.
The lower energies of the contour plot close upon themselves. The pendulum swings back and forth between two maximum angles and velocities. The kinetic energy of the pendulum is not enough to overcome gravitational energy and enable the pendulum to make a full loop.
The higher energies of the contour plot do not close upon themselves. The pendulum always moves in one angular direction. The kinetic energy of the pendulum is enough to overcome gravitational energy and enable the pendulum to make a full loop.
Step 7: Solve Nonlinear Equations of Motion
The nonlinear equations of motion are second-order differential equations. Numerically solve these equations by using theode45
solver. Becauseode45
accepts only first-order systems, reduce the system to a first-order system. Then, generate function handles that are the input toode45
。
Rewrite the second-order ODE as a system of first-order ODEs.
symstheta(t)theta_t(t)omega_0eqs = [diff(theta) == theta_t; diff(theta_t) == -omega_0^2*sin(theta)]
eqs(t) =
eqs = subs(eqs,omega_0,omega_0Value); vars = [theta, theta_t];
Find the mass matrixM
of the system and the right sides of the equationsF
。
[M,F] = massMatrixForm(eqs,vars)
M =
F =
M
andF
refer to this form.
To simplify further computations, rewrite the system in the form 。
f = M\F
f =
Convertf
to a MATLAB function handle by usingodeFunction
。The resulting function handle is the input to the MATLAB ODE solverode45
。
f = odeFunction(f, vars)
f =function_handle with value:@ (t, in2) [in2(2:);罪(in2 (1,:)).*(-9.81e+2./1.0e+2)]
Step 8: Solve Equation of Motion for Closed Energy Contours
Solve the ODE for the closed energy contours by usingode45
。
From the energy contour plot, closed contours satisfy the condition
,
。Store the initial conditions of
and
in the variablex0
。
x0 = [0; 1.99*omega_0Value];
Specify a time interval from 0 s to 10 s for finding the solution. This interval allows the pendulum to go through two full periods.
tInit = 0; tFinal = 10;
Solve the ODE.
sols = ode45(f,[tInit tFinal],x0)
sols =struct with fields:solver: 'ode45' extdata: [1x1 struct] x: [0 3.2241e-05 1.9344e-04 9.9946e-04 0.0050 0.0252 0.1259 0.3449 0.6020 0.8591 1.1161 1.3597 1.5996 1.8995 2.2274 2.4651 2.7028 2.9567 3.2138 3.4709 3.7150 3.9511 4.2483 4.5759 4.8239 5.0719 5.3182 5.5764 5.8346 6.0803 6.3072 6.5981 ... ] y: [2x45 double] stats: [1x1 struct] idata: [1x1 struct]
sols.y(1,:)
represents the angular displacement
andsols.y(2,:)
represents the angular velocity
。
Plot the closed-path solution.
figure; yyaxisleft; plot(sols.x, sols.y(1,:),'-o'); ylabel('\theta (rad)'); yyaxisright; plot(sols.x, sols.y(2,:),'-o'); ylabel('\theta_t (rad/s)'); gridon; title('Closed Path in Phase Space'); xlabel('t (s)');
Visualize the motion of the pendulum.
x_pos = @(t) sin(deval(sols,t,1)); y_pos = @(t) -cos(deval(sols,t,1)); figure; fanimator(@(t) plot(x_pos(t),y_pos(t),'ko','MarkerFaceColor','k')); holdon; fanimator(@(t) plot([0 x_pos(t)],[0 y_pos(t)],'k-')); fanimator(@(t) text(-0.3,1.5,"Timer: "+num2str(t,2)+" s"));
Enter the commandplayAnimation
to play the animation of the pendulum motion.
Step 9: Solutions on Open Energy Contours
Solve the ODE for the open energy contours by usingode45
。From the energy contour plot, open contours satisfy the condition
,
。
x0 = [0; 2.01*omega_0Value]; sols = ode45(f, [tInit, tFinal], x0);
Plot the solution for the open energy contour.
figure; yyaxisleft; plot(sols.x, sols.y(1,:),'-o'); ylabel('\theta (rad)'); yyaxisright; plot(sols.x, sols.y(2,:),'-o'); ylabel('\theta_t (rad/s)'); gridon; title('Open Path in Phase Space'); xlabel('t (s)');
Visualize the motion of the pendulum.
x_pos = @(t) sin(deval(sols,t,1)); y_pos = @(t) -cos(deval(sols,t,1)); figure; fanimator(@(t) plot(x_pos(t),y_pos(t),'ko','MarkerFaceColor','k')); holdon; fanimator(@(t) plot([0 x_pos(t)],[0 y_pos(t)],'k-')); fanimator(@(t) text(-0.3,1.5,"Timer: "+num2str(t,2)+" s"));
Enter the commandplayAnimation
to play the animation of the pendulum motion.