Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
53 lines (48 sloc) 1.45 KB
function [lambda, n] = GaussNewton(f, xi, y, lambda0, tol, nmax, pmax)
syms a b;
currentLambda = lambda0';
g = y - f(xi);
dg = jacobian(g, [a b]);
n = 0;
while n < nmax
lambda = currentLambda;
[currentLambda, sigma, p] = computeNext(xi, currentLambda, dg, g, pmax);
n = n + 1;
if norm(sigma/2.^p, 2) < tol
lambda = currentLambda;
break;
end
end
function [lambda, sigma, p] = computeNext(xinput, currentLambda, dg, g, pmax)
syms x;
dgCurrent = ones(size(dg));
gCurrent = ones(1, length(g));
a = currentLambda(1);
b = currentLambda(2);
for i=1:length(g)
x = xinput(i);
temp = g(i);
temp = subs(temp);
gCurrent(i) = temp;
end
for i=1:size(dg, 1)
for j=1:size(dg, 2)
x = xinput(i);
temp = dg(i, j);
temp = subs(temp);
dgCurrent(i, j) = temp;
end
end
dgCurrentTdgCurrent = dgCurrent'*dgCurrent;
dgCurrentTgCurrent = -(dgCurrent'*gCurrent');
sigma = linsolve(dgCurrentTdgCurrent, dgCurrentTgCurrent);
p = 0;
while norm(currentLambda + (sigma / 2.^p), 2) >= norm(currentLambda) && p < pmax
p = p+1;
end
if p == pmax
p = 0;
end
lambda = currentLambda + (sigma/2.^p);
end
end
You can’t perform that action at this time.