% Tund 7, ülesanne 2
% jooned joonisele
ezplot('x^2+2*y-8=0')
hold on
ezplot('x+y^2-y-3=0')
hold off
grid on
% alglähendid (-3.5,-2), (-1.6,2.7),(2.2,1.5),(3,0)
F=@(z) [z(1)^2+2*z(2)-8;z(1)+z(2)^2-z(2)-3];
z=[-3.5;-2]; % alglähend veeruvektorina
i=0;
while norm(F(z),1)>10^(-5)
  Ftuletis=[2*z(1),2;1,2*z(2)-1];
  z=z-inv(Ftuletis)*F(z);
  i=i+1;
end
lahend1=z
sammud1=i

% ....

% lahendame hariliku iteratsioonimeetodiga
% uurime koondumist ||G'(x,y)||<1 iga alglähendi korral
x=-3.5;
y=-2;
Gtuletis=[0,-1/sqrt(8-2*y);-1/(2*sqrt(y+3-x)),1/(2*sqrt(y+3-x))];
norm(Gtuletis,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) % 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) % sobib
x=2.9;
y=-0.7;
Gtuletis=[0,-1/sqrt(8-2*y);-1/(2*sqrt(y+3-x)),1/(2*sqrt(y+3-x))];
norm(Gtuletis,1) % ei sobi

z=[-3.5;-2];
i=0;
while norm(F(z),1)>10^(-5)
  z(1)=-sqrt(8-2*z(2));
  z(2)=-sqrt(z(2)+3-z(1));
  i=i+1;
end
lahend1h=z
sammud1h=i

z=[-1.6,2.7];
i=0;
while norm(F(z),1)>10^(-5)
  z(1)=-sqrt(8-2*z(2));
  z(2)=sqrt(z(2)+3-z(1));
  i=i+1;
end
lahend2h=z
sammud2h=i
z=[2.2,1.5];
i=0;
while norm(F(z),1)>10^(-5)
  z(1)=sqrt(8-2*z(2));
  z(2)=sqrt(z(2)+3-z(1));
  i=i+1;
end

lahend3h=z
sammud3h=i
z=[2.9,-0.7];
i=0;
while norm(F(z),1)>10^(-5)
  z(1)=sqrt(8-2*z(2));
  z(2)=-sqrt(z(2)+3-z(1));
  i=i+1;
end
lahend4h=z
sammud4h=i