% Harjutustund nr. 7, ülesanne 3
% 1. Newtoni meetodiga
% joonis
ezplot('20*x^2=1-2*x^3+4*y^3')
hold on
ezplot('2*x^3=3*y^2+10*y-5')
hold off
grid on
% alglähendid (-0.5,0.5),(0.5,0.5) ja (3.5,4.5)

F=@(z) [1-2*z(1).^3+4*z(2).^3-20*z(1).^2;3*z(2).^2+10*z(2)-5-2*z(1).^3];
Ftuletis=@(z) [-6*z(1).^2-40*z(1),12*z(2).^2;-6*z(1).^2,6*z(2)+10];

% a) alglähendi (-0.5,0.5) korral
z=[-0.5;0.5];
i=0;
while (norm(F(z),1))>10^-6
    z=z-inv(Ftuletis(z))*F(z); 
    i=i+1;
end
lahend1n=z
sammud1n=i

% a) alglähendi (0.5,0.5) korral
z=[0.5;0.5];
i=0;
while (norm(F(z),1))>10^-6
    z=z-inv(Ftuletis(z))*F(z); 
    i=i+1;
end
lahend2n=z
sammud2n=i

% c) alglähendi (3.5,4.5) korral
z=[3.5;4.5];
i=0;
while (norm(F(z),1))>10^-6
    z=z-inv(Ftuletis(z))*F(z); 
    i=i+1;
end
lahend3n=z
sammud3n=i

% 2. lahendame hariliku iteratsioonimeetodiga
% uurime koonduvust
x=-0.5;
y=0.5;
Gtuletis=[(-3*x^2)/(20*sqrt(1/20*(1-2*x^3+4*y^3))),(3*y^2)/(10*sqrt(1/20*(1-2*x^3+4*y^3)));3/5*x^2,-3/5*y];
norm(Gtuletis,1) % sobib

x=0.5;
y=0.5;
Gtuletis=[(-3*x^2)/(20*sqrt(1/20*(1-2*x^3+4*y^3))),(3*y^2)/(10*sqrt(1/20*(1-2*x^3+4*y^3)));3/5*x^2,-3/5*y];
norm(Gtuletis,1) % sobib

x=3.5;
y=4.5;
Gtuletis=[(-3*x^2)/(20*sqrt(1/20*(1-2*x^3+4*y^3))),(3*y^2)/(10*sqrt(1/20*(1-2*x^3+4*y^3)));3/5*x^2,-3/5*y];
norm(Gtuletis,1) % ei sobi

% leiame lahendid hariliku it. meetodiga kahe esimese alglähendi korral
% a) (-0.5,0.5)
z=[-0.5,0.5];
i=0;
G=@(z) [-sqrt(1/20*(1-2*z(1)^3+4*z(2)^3)),1/10*(2*z(1)^3-3*z(2)^2+5)];
while (norm(F(z),1))>10^-6
    z=G(z);
    i=i+1;
end
lahend1h=z
sammud1h=i
% b) (0.5,0.5)
z=[0.5,0.5];
i=0;
G=@(z) [sqrt(1/20*(1-2*z(1)^3+4*z(2)^3)),1/10*(2*z(1)^3-3*z(2)^2+5)];
while (norm(F(z),1))>10^-6
    z=G(z);
    i=i+1;
end
lahend2h=z
sammud2h=i