clear all
m = 0.001;  % masa tega (kg)
r = 0.1;    % poluprecnik tega (m)
l = 0.7;    % duzina kanapa (m)
g = 9.81;   % gravitacija (m/s^2)
teta0=pi;   % ugao otklona (rad)
v0=0;       % pocetna brzina
O = [0 0];  % koordinatni pocetak

tend=50;

mi = 1.827e-5;  % vazduh (kg/ms)
%mi = 8.9e-4;    % voda

B=2*pi*r*mi;

f=@(t,x) [x(2); -g/l*sin(x(1))];            % bez otpora
%f=@(t,x) [x(2); -B/m*x(2)-g/l*sin(x(1))];  % sa otporom

init=[teta0; v0/l];

[t,x]=ode45(f,[0,tend],init);

figure(1)  % grafik theta(t) i v(t)
plot(t,x(:,1));       
hold on
plot(t,x(:,2)*l);
xlabel('Vreme');
legend('Ugao teta','Brzina');
hold off

figure(2)  % klatno
axis(gca, 'equal');       % odnos x i y ose (isto sto naredba: axis equal)
axis([-1 1 -1 1]*1.2*l);  % granice za x i y osu u zavisnosti od l (20% vise)
%grid on;

O_circ = viscircles(O,0.01);          % postolje
for i = 1: length(t)
   P = l*[sin(x(i,1))  -cos(x(i,1))];    % polozaj tega   
   pend = line([O(1) P(1)], [O(2) P(2)]);  % stap
   ball = viscircles(P, 0.05);           % teg
   
   pause(0.05);
   %pause(t(i+1)-t(i))

    if i< length(t)
        delete(pend);
        delete(ball);
    end
end
