Ruslan,
The termination condition, fprev < fnext, must be based on scalars, as you
pointed out. There is some inconsistency in your call to POLYVAL along these
lines.
As a start, you might want to compare your LM function against the built-in
MATLAB version, which should be visible if you have the Optimization
Toolbox:
edit( fullfile(matlabroot,
'toolbox\optim\optim\private\levenbergMarquardt.m') )
In the built-in LM solver, the terminating condition is predicated on
whether the sum of squares of the error term is reduced from iteration to
iteration:
if ~isempty(YDATA)
trialCostFun = trialCostFun - YDATA; % If fitting to data, compute
residuals
end
<snip>
trialSumSq = trialCostFun'*trialCostFun; % Scalar value
if trialSumSq < sumSq % Terminating condition
You might want to consider a terminating condition along these lines.
Best,
Manu Raghavan
Post by ruslanhi, I still have problem with this algorithm. I want to find gloabl
minimum point. I coded these codes. but still I cant solve problem. I can
not evaluate function value, since I get nX1 matris not single value in
the scope of while loop2. where p = *****. here I get a matrix, but I
think I should get single value. I've looked some codes on net,but it
seems very hard to understand from those codes.I know I miss something.
any help pleaase ...
x = linspace(0,11,12)';
y = [12 10.5 9 6 4 3.5 3.2 2 1 2 4 7]';
order = 12;
coef = polyfit(x,y,order);
yy = polyval(coef,x);
%%% function is defined %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
iteration = 30;
mu = 0.01;
e = 0.01;
fnext = 0;
zz = ones(1,12) + 8;
z = zz';
list = [ 12 11 10 9 8 7 6 5 4 3 2 1 ];
d = (coef(1:12).*list);
k = 0;
loop1 = 1;
while loop1
k = k + 1;
fprev = polyval(coef,z);
xx = z';
jacobian = d*[(xx').^11 (xx').^10 (xx').^9 (xx').^8 (xx').^7 (xx').^6
(xx').^5 (xx').^4 (xx').^3 (xx').^2 (xx').^1 (xx').^0]';
loop2 = 1;
while loop2
p = -inv(jacobian'*jacobian + mu*eye(12))*jacobian'*e;
z = z + p;
fnext = polyval(coef,z);
if (fnext < fprev)
mu = mu / 10;
loop2 = 0;
else mu = mu*10;
z = z - p;
end
end
if (k == iteration)
loop1 = 0;
end
end