Gauss-Newton Convergence: mine in 2000 iterations, theirs in 13. What's wrong with my code?

9 views (last 30 days)
I have been working on an assignment where I have to approximate the Gauss-Newton method by supplying an m function that calculates the Jacobian approxiation for the Hessian, and then inputting that function into pre-given Newton's Method code. However, I found all-in-one Gauss Newton code for Mathematica that, starting with an initial value of gets the convergence I want to the point where the minimum occurs, , in only 14 iterations. Mine takes over 2000. Is my code wrong? And if so, short of combining everything into one m file (I'd prefer not to do that), how do I make it right? (Note: I also neither know how to code in Mathematica nor own a copy of the software).
Here are screenshots of my Jacobian approximation file:
function[val,g,H]=givenfGNM(x)
syms a b c
val=(1/2)*((2*a-(b*c)-1)^2+(1-a+b-exp(a-c))^2+(-a-2*b+3*c)^2);
if(nargout>1)
g=gradient(val, [a b c]);
end
if(nargout>2)
J=jacobian(val, [a b c]);
K=transpose(J);
H=mtimes(K,J);
a=x(1);
b=x(2);
c=x(3);
H=double(subs(H));
end
a=x(1);
b=x(2);
c=x(3);
g=double(subs(g));
val=double(subs(val));
end
And my Newton's Method file:
function [x,pointlist] = Newton(func,x0,t,itmax,tol)
% Newton's method with two-slope test
% No testing of erroneous input is performed
% func -- name of function to minimize
% x0 -- starting point
% t -- starting stepsize
% itmax -- maximum number of iterations
% tol -- norm of gradient to stop
x= x0;
iter = 0;
alpha = 0.3;
tau = t;
opts.SYM = true;
verysmall = 1.0e-10;
if (nargout > 1)
pointlist = transpose(x);
end
while ( iter < itmax )
[val, grad, H] = func(x);
gn = norm(grad);
fprintf('it=%3d f=%.5f |grad f|=%.5f',iter,val,gn);
fprintf(' x =');
for j=1:length(x)
fprintf(' % .5f',x(j));
end
fprintf('\n');
if ( gn < tol )
break;
end
[d,kappa] = linsolve(H,-grad,opts);
if ( kappa < verysmall )
s = 0;
else
s = dot(grad,d);
end
if ( (s >= 0) || (tau == 0) )
d = -grad;
s = -gn^2;
t0 = t/gn;
steepest = 1;
else
t0 = 1;
steepest = 0;
end
[x,tau] = DirMin(func,x,d,s,val,t0,-0.1*s);
if ( steepest && (tau==0) )
break
end
if (nargout > 1)
pointlist = [pointlist ; transpose(x)];
end
iter = iter + 1;
end
end
And here's this other person's Mathematica code:
And their output list:

Answers (0)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!