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

MATLABpendulumplotpng

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

Best Answer

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