% ENM 3.13
% Denna kod skrevs till övningen 2017-11-17 i SF1545

clear
format long

x = [0 0 0 0 0]'; % Denna startgissning funkar ej
x = [0.18 1.18 0.32 0.32 1e-4]'; % Denna funkar

% Newtons metod för system
t = Inf;
tol = 1e-12;
while norm(t) >= tol
  xtot = sum(x);
  % funktion
  f = [x(1) + x(3) + x(5) - 0.5
       x(2) + x(3) - x(5) - 1.5
       x(3) - 3*x(5) - x(4)
       2*x(4)*x(3) - x(1)*x(2)
       x(5)*x(2)*xtot^2 - 0.09*x(1)*x(4)^3];
  p = x(5)*x(2)*2*xtot;
  % Jacobimatris
  J = [1 0 1 0 1
       0 1 1 0 -1
       0 0 1 -1 -3
       -x(2) -x(1) 2*x(4) 2*x(3) 0
       p-0.09*x(4)^3 p+x(5)*xtot^2 p p-0.09*x(1)*3*x(4)^2 p+x(2)*2*xtot];
  t = J\f;
  x = x - t
  norm(t)
end
