Given function that integrates the pendulum nonlinear differential equation, plot the graph with the given conditions
이전 댓글 표시
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
댓글 수: 1
Walter Roberson
2018년 10월 9일
What is your question?
채택된 답변
추가 답변 (1개)
Mark Chernyshov
2018년 10월 9일
0 개 추천
댓글 수: 1
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.
카테고리
도움말 센터 및 File Exchange에서 Classical Mechanics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!