clear %vahemälu puhtaks

%joonis alglähendite jaoks
ezplot('20*x^2-1+2*x^3-4*y^3',[-2,4,-1,5])
hold on
h=ezplot('2*x^3-3*y^2-10*y+5',[-2,4,-1,5])
set(h,'color','r')
grid on
legend('20^2=1-2x^3+4y^3','2x^3=3y^2+10y-5')
title('Alglähendite joonis')
hold off

%Jooniselt on näha, et süsteemil on 3 reaalset lahendit.
%Valime alglähenditeks (-0.2,0.5) ja (0.2,0.5) ja (3.7,4.5)

%kontrollime HIM koonduvust
%esimene lahend
x=-0.2;
y=0.5;
G(1,1)=-3*x^2/sqrt(20*(1-2*x^3+4*y^3));
G(1,2)=6*y^2/sqrt(20*(1-2*x^3+4*y^3));
G(2,1)=3*x^2/5;
G(2,2)=-3*y/5;
tuletis1=norm(G,1) %sobib

%teine lahend
x=0.2;
y=0.5;
G(1,1)=-3*x^2/sqrt(20*(1-2*x^3+4*y^3));
G(1,2)=6*y^2/sqrt(20*(1-2*x^3+4*y^3));
G(2,1)=3*x^2/5;
G(2,2)=-3*y/5;
tuletis2=norm(G,1) %sobib

%kolmas lahend
x=3.7;
y=4.5;
G(1,1)=(10*x+1.5*x^2)/(3*nthroot((0.25*(20*x^2-1+2*x^3))^2,3));
G(1,2)=0;
G(2,1)=0;
G(2,2)=(3*y+5)/(3*nthroot((0.5*(3*y^2+10*y-5))^2,3));
tuletis3=norm(G,1) %sobib

%Defineerime F(z)
F=@(z)[20*z(1)^2-1+2*z(1)^3-4*z(2)^3,2*z(1)^3-3*z(2)^2-10*z(2)+5];

%HIM abil lahendamine
%esimene lahend
z(1)=-0.2;
z(2)=0.5;
while norm(F(z),1)>10^-6
    z(1)=-sqrt(1/20*(1-2*z(1)^3+4*z(2)^3)); %valime -, sest x on negatiivne
    z(2)=1/10*(2*z(1)^3-3*z(2)^2+5);
end
lahend1=z

%teine lahend
z(1)=0.2;
z(2)=0.5;
while norm(F(z),1)>10^-6
    z(1)=sqrt(1/20*(1-2*z(1)^3+4*z(2)^3)); %valime +, sest x on positiivne
    z(2)=1/10*(2*z(1)^3-3*z(2)^2+5);
end
lahend2=z

%kolmas lahend
z(1)=3.7;
z(2)=4.5;
while norm(F(z),1)>10^-6
    z(2)=nthroot(0.25*(20*z(1)^2-1+2*z(1)^3),3);
    z(1)=nthroot(0.5*(3*z(2)^2+10*z(2)-5),3);
end
lahend3=z