clear
hold off
%nihe on defineeritud hääbuva võnkumise võrrandiga 
%1. leidke graafilise meetodiga ligikaudne aeg nihkeni 3,5
k=0.7
w=4
f=@(t)exp(t.*(-k)).*cos(t.*w).*9
fplot(f,[0,6])
hold on
%x=0:0.01:6;
%y=3.5;
%plot(x,y,'o')
grid on
legend('nihe sõltuvus ajast; y=9*e^(-k*t)*cos(w*t)')
xlabel('aeg')
ylabel('nihe')
%graafiku järgi ligikaudne aeg nihkeni 3.5 võrdub 0.3 s

%Newtoni meetod %0.4 1.1 2 2.75 3.5
x=[0.4 1.1 2 2.75 3.5 4.25 5 6]; %algl
alglahendid=x
t=0;
i=0;
for i=1:8
    t=x(i);
    %y=x(i);
    samm=0;
    while abs(f(t))>10^-5 %-6.3.*exp(-k.*t).*cos(w.*t))-36.*(exp(-k.*t).*sin(w.*t)
        t=t-((9.*exp(-k.*t).*cos(w.*t))/((-6.3.*exp(-k.*t).*cos(w.*t))-36.*(exp(-k.*t).*sin(w.*t))));
        samm=samm+1;
    end
    vastus_Newton(i)=t;
    sammud(i)=samm;
end
vastus_Newton
sammud

%lõikajate meetod %0.4 1.1 2 2.75 3.5 4.25 5 6
t=0;
i=0;
n=0.55;
for i=2:8
    t=x(i);
    samm=0;
    %a=9.*exp(-k.*t).*cos(w.*t);
    %b=9.*exp(-k.*x(i-1)).*cos(w.*x(i-1));
    while abs(f(t))>10^-5
        t=t-((t-(t-n))/((9.*exp(-k.*t).*cos(w.*t))-(9.*exp(-k.*(t-n))).*cos(w.*(t-n))))*(9.*exp(-k.*t).*cos(w.*t));
        %t=t-a.*((t-x(i-1))/(a-b));
        %t=t-((9.*exp(-k.*t).*cos(w.*t)).*(t-x(i-1)/((9.*exp(-k.*t).*cos(w.*t))-(9.*exp(-k.*x(i-1)).*cos(w.*x(i-1))))));
        samm=samm+1;
    end
    vastus_loikajad(i)=t;
    sammud(i)=samm;
end
vastus_loikajad
sammud