Given function that integrates the pendulum nonlinear differential equation, plot the graph with the given conditions

8 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2018-10-9
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

更多回答(1 个)

Mark Chernyshov
Mark Chernyshov 2018-10-9
I need to plot this function, but the statement pendulum(1, [pi/2: -pi], 10) did not seem to do the plotting
  1 个评论
Walter Roberson
Walter Roberson 2018-10-11
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Classical Mechanics 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by