1

I'm working a program which implements Newton's method on an m file containing the system of equations and Jacobians

function x = fun(x,mode)

% compute F(x)
  if mode == 1
      x   = [ 3*x(1)^2 + 5*x(2)^2 + x(2) - 1;
             x(1)^2 - 6*x(1) - 3*x(2) - 2];
  end 

% compute the Jacobian DF(x)
  if mode == 2
     x  = [ 1*x(1) 3*x(2)+1; 6*x(1)-2  -2];
  end 
return;

Here is the computational file that I've started:

results.stat = 0;
if nargin == 3 
   epsilon = param.epsilon;
   nMax    = param.nMax;
else 
   epsilon = 1.0e-8; 
   nMax = 500;  
end

F = fun(x,1)
J = fun(x,2)
end

for k = 1:nMax 
    dx = -J\f
    x=x+dx  

I eventually want to check convergence and save the errors norm and also check if the max iteration count is reached without convergence. Does this look ok so far?

carcinogenic
  • 169
  • 1
  • 2
  • 10
  • This looks like it should work just fine (apart from some missing/misplaced `end`'s), do you have any specific question(s)? – David Apr 10 '14 at 02:22
  • The Jacobian you show is not the jacobian for the function you provide. – dustincarr Apr 10 '14 at 19:08
  • 1
    No, this will not work in the intended way since you evaluate function value and derivative only once outside of the loop. What will happen is that it is always the same dx that gets added, so that the loop could be replaced with the formula `x=x-n*J\f`. So move the evaluations inside the loop. – Lutz Lehmann Apr 11 '14 at 17:26

0 Answers0