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 Center および File ExchangeClassical Mechanics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by