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];
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;
dtheta = zeros(2, 1);
dtheta(1) = theta(2);
dtheta(2) = -g/l*theta(1);
end
Best Answer