% Harjutustund 6, ülesanne 6
% alglähendid jooniselt
ezplot('x^2+2*y-8=0')
hold on
ezplot('x+y^2-y-3=0')
hold off
grid on
yline(0)
xline(0)
% alglähendid: (-3.5,-2.1), (-1.6,2.7), (2.2,1.5) ja (2.9,-0.2)
% koondumise uurimine
x=-3.5;
y=-2.1;
Gtuletis=[0 -1/sqrt(8-2*y)
    -1/(2*sqrt(y+3-x)) 1/(2*sqrt(y+3-x))];
norm(Gtuletis,1) % <1, sobib

x=-1.6;
y=2.7;
Gtuletis=[0 -1/sqrt(8-2*y)
    -1/(2*sqrt(y+3-x)) 1/(2*sqrt(y+3-x))];
norm(Gtuletis,1) % <1, sobib

x=2.2;
y=1.5;
Gtuletis=[0 -1/sqrt(8-2*y)
    -1/(2*sqrt(y+3-x)) 1/(2*sqrt(y+3-x))];
norm(Gtuletis,1) % <1, sobib

x=2.9;
y=-0.2;
Gtuletis=[0 -1/sqrt(8-2*y)
    -1/(2*sqrt(y+3-x)) 1/(2*sqrt(y+3-x))];
norm(Gtuletis,1) % >1, ei sobi

% leiame esimesele kolmele alglähendile vastavad lahendid
F=@(z) [z(1)^2+2*z(2)-8,z(1)+z(2)^2-z(2)-3];
z=[-3.5,-2.1];
sammud=0;
while norm(F(z),1)>10^(-5)
 z(1)=-sqrt(8-2*z(2));
 z(2)=-sqrt(z(2)+3-z(1));
 sammud=sammud+1;
end
lahend1harilik=z
sammud1harilik=sammud

z=[-1.6,2.7];
sammud=0;
while norm(F(z),1)>10^(-5)
 z(1)=-sqrt(8-2*z(2));
 z(2)=sqrt(z(2)+3-z(1));
 sammud=sammud+1;
end
lahend2harilik=z
sammud2harilik=sammud

z=[2.2,1.5];
sammud=0;
while norm(F(z),1)>10^(-5)
 z(1)=sqrt(8-2*z(2));
 z(2)=sqrt(z(2)+3-z(1));
 sammud=sammud+1;
end
lahend3harilik=z
sammud3harilik=sammud

% neljanda alglähendi koonduvus
x=2.9;
y=-0.2;
Gtuletis=[0 -1/sqrt(8-2*y)
    1 2*y];
norm(Gtuletis,1) % 1. Arvestame, et ~<1
z=[2.9,-0.2];
sammud=0;
while norm(F(z),1)>10^(-5)
 z(1)=sqrt(8-2*z(2));
 z(2)=z(1)+z(2)^2-3;
 sammud=sammud+1;
end
lahend4harilik=z
sammud4harilik=sammud

% 2) lahendame Newtoni meetodiga
Ftuletis=@(z) [2*z(1) 2
    1 2*z(2)];