% harjutustund nr. 7, ülesanne 3
% joonis, alglähendid
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)
% 1. newtoni meetodiga
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) (-0.5,0.5) korral
z=[-0.5;0.5]; % !!! veeruvektor
i=0;
while (norm(F(z)))>10^-3
    z=z-inv(Ftuletis(z))*F(z);
    i=i+1;
end
lahend1newton=z
sammud1newton=i
% b) (0.5,0.5) korral
z=[0.5;0.5]; % !!! veeruvektor
i=0;
while (norm(F(z)))>10^-3
    z=z-inv(Ftuletis(z))*F(z);
    i=i+1;
end
lahend2newton=z
sammud2newton=i
% c) (3.5,4.5) korral
z=[3.5;4.5]; % !!! veeruvektor
i=0;
while (norm(F(z)))>10^-3
    z=z-inv(Ftuletis(z))*F(z);
    i=i+1;
end
lahend3newton=z
sammud3newton=i

% 2. hariliku it. meetodiga
% koonduvus
x=-0.25;
y=0.45;
Gtuletis=[1/(2*sqrt(1/20*(1-2*x^3+4*y^3)))*1/20*-6*x^2,(12*y^2)/(40*sqrt(1/20*(1-2*x^3+4*y^3)));
    x^2/((2*x^3)/3 - (10*y)/3 + 5/3)^(1/2),-5/(3*((2*x^3)/3 - (10*y)/3 + 5/3)^(1/2))];
norm(Gtuletis,1) % ei sobi
x=0.5;
y=0.5;
Gtuletis=[1/(2*sqrt(1/20*(1-2*x^3+4*y^3)))*1/20*-6*x^2,(12*y^2)/(40*sqrt(1/20*(1-2*x^3+4*y^3)));
    x^2/((2*x^3)/3 - (10*y)/3 + 5/3)^(1/2),-5/(3*((2*x^3)/3 - (10*y)/3 + 5/3)^(1/2))];
norm(Gtuletis,1) % ei sobi
x=3.5;
y=4.5;
Gtuletis=[1/(2*sqrt(1/20*(1-2*x^3+4*y^3)))*1/20*-6*x^2,(12*y^2)/(40*sqrt(1/20*(1-2*x^3+4*y^3)));
    x^2/((2*x^3)/3 - (10*y)/3 + 5/3)^(1/2),-5/(3*((2*x^3)/3 - (10*y)/3 + 5/3)^(1/2))];
norm(Gtuletis,1) % ei sobi