Given function that integrates the pendulum nonlinear differential equation, plot the graph with the given conditions
8 views (last 30 days)
Show older comments
Mark Chernyshov
on 9 Oct 2018
Commented: Walter Roberson
on 11 Oct 2018
Use the MATLAB function that integrates the pendulum nonlinear differential equation to find the trajectory of a pendulum of length 1m, with an initial displacement of pi/2 rad and -pi rad/s initial velocity. Integrate between 0 and 10s. Save your shot as PNG file.
function pendulum (1, x0, T)
% PENDULUM Computes trajectory of pendulum
% PENDULUM (1, x0, T)
% 1 - the length of the pendulum
% x0 - the comlumn vector of initial conditions:
% angular displacement and velocity
% T - the end time
g = 9.81; % m/s^2
options = odeset('MaxStep', 0.01, 'Stats', 'on');
sol = ode45(@(t, x) f(t, x, g, l), [0 T], x0, options);
subplot(2, 1, 1)
plot(sol.x, sol.y)
legend('angular displacement (rad)', ...
'angular velocity (rad/s)', ...
'Location', 'southwest')
title('waveforms')
xlabel('time (s)')
subplot(2, 1, 2)
plot(sol.y(1,:), sol.y(2,:))
title('phase plane')
xlable('angular displacement (rad)')
ylable('angular velocity (rad/s)')
end
function xdot = f(~, x, g, l)
% F pendulum differential equation
% x(1) is the angular displacement from the vertical
% x(2) is the angular velocity
% g is the acceleration of gravity
% l is the length of pendulum
xdot = [
x(2);
-g/l * sin(x(1))
];
end
Accepted Answer
Walter Roberson
on 9 Oct 2018
You cannot have a number as a parameter in a function statement.
We recommend against using lowercase L as the name of a variable, as it is easy to confuse it with the digit 1
More Answers (1)
Mark Chernyshov
on 9 Oct 2018
1 Comment
Walter Roberson
on 11 Oct 2018
According to the documentation, the second parameter needs to be
% x0 - the comlumn vector of initial conditions:
% angular displacement and velocity
Your parameter [pi/2: -pi], is a pair of numbers with the colon operator between them. The colon operator with two operands creates a vector starting from the first value and increasing by 1 each time until the second operand is reached or exceeded. When the second operand is less than the first, the colon operator returns the empty vector. Since -pi is less than pi/2, you are passing the empty vector.
Perhaps you mean [pi/2; -pi] . That would correspond to an angular displacement of pi/2 and a velocity of -pi . Seeing pi in a velocity looks odd, but it is not impossible.
See Also
Categories
Find more on Classical Mechanics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!