MATLAB: How to code simple pendulum motion using ode45

ode45simple pendulum

how to code simple pendulum motion (displacement vs time) using ode45

Best Answer

The equation of simple pendulum is , which can be converted to two first order ODEs
and then using ode45 like this
theta_ic = [0.5; 0]; % initial conditions: theta(t=0)=0.5; dtheta(t=0)=0.
tspan = [0 10];
[t, theta] = ode45(@odeFun, tspan, theta_ic);
plot(t, theta);
legend({'$\theta$', '$\dot{\theta}$'}, ...
'Location', 'best', ...
'Interpreter', 'latex', ...
'FontSize', 16)
function dtheta = odeFun(t, theta)
g = 9.8;
l = 1;
% theta(1) = theta, theta(2) = dtheta
dtheta = zeros(2, 1);
dtheta(1) = theta(2);
dtheta(2) = -g/l*theta(1);
end
Related Question