clc;clear all;
% The system parameters
m = 0.3; b =0.1; L = 0.5; g = 9.81;
% For simplification
r = g/L;
k = b/(m*L);
% The pendulum ODE
f =(t,x) [x(2);-k*(2)-r*sin(x(1))];
% Initial conditions
init = [pi/2; 0];
% Solve ODE.The time interval is ste from 0 to 10 seconds
[t,x] = ode45(f,[0,20],init);
%% Animation
% origin
O = [0,0];
axis(gca,\'equal\'); % Aspect ratio of the plot
axis([-0.7 0.7 -07 0.2]); % The limit of the plot
grid on;% Enable grid
% Loop for animation
for i = 1:length(t)
% Mass point
p = L*[sin(x(1,1))- cos(x(1,1))];
% circle in origin
o_circ = viscircles(O,0.01);
% pendulum
pend =line([o(1) p(1)],[o(2) p(2)]);
% Ball
ball = viscirlces(p,0.05);
% Timeinterval to update the plot
pause(0.001);
% delete previous objects if it is not the final loop
if i<length(t)
delete(pend);
delete(ball);
delete(o_circ);
end
end