mirali

Untitled

Apr 14th, 2021
592
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function sol = newton(x0, N, epsilon)
  2.     function F = f(x)
  3.         f_1 = 3*x(1) - cos(x(2)*x(3)) - 0.5;
  4.         f_2 = 4*x(2)^2 - 625*x(2)^2 + 2*x(2) - 1;
  5.         f_3 = exp(-x(1)*x(2)) + 20*x(3) + (10*pi - 3)/3;
  6.         F = [f_1; f_2; f_3];
  7.     end
  8.    
  9.     function J = Jacobian(x)
  10.         J = [3, x(3)*sin(x(2)*x(3)), x(2)*sin(x(2)*x(3));
  11.             8*x(1), -1250*x(2) + 2, 0;
  12.             -x(2)*exp(-x(1)*x(2)), -x(1)*exp(-x(1)*x(2)), 20];
  13.     end
  14.    
  15.     sol = [x0; 0];
  16.     xk = x0;
  17.     for k = 1:N
  18.         F = f(xk);
  19.         J = Jacobian(xk);
  20.         y = -inv(J)*F;
  21. %         y = linsolve(J, -F);
  22.         xk = xk + y;
  23.         sol = [xk; k];
  24.         if norm(y, inf) < epsilon
  25.             break
  26.         end
  27.     end
  28. end
RAW Paste Data