I am trying to make a levenberg-marqurdt for nonlinear least square fitting. Why is this code keep giving me error? can someone help me fix this for me?

function [xc] = LM(x0, lambda, max_it, Tol)
for i = 1:max_it
[r,A]=rdr_4_25(x0);
fprintf('x(%2d) = [%12.5e %12.5e %12.5e] E=%12.5e \n', i, x0, 0.5*r'*r );
AtA=A'*A;
v=-( AtA+lambda*diag(diag(AtA)) )\(A'*r);
x1 = x0+v;
if norm(v,inf) < Tol,
break;
end
x0=x1;
end
xc = x1;
%% END
%% example 4.25
function [r, dr]=rdr_4_25(x)
t = [0 2 4 6 8 10 12 14 16 18 20 22 24]';
y = [1.00 3.95 5.33 4.59 3.35 2.35 1.72 1.36 1.18 1.08 1.04 1.02 1.01]';
E = exp(-x(4)*t);
f = x(1)+x(2)*t.^x(3).*E;
r = f - y;
dr=[1, t.^x(3).*E,log(t)*x(2)*t.^x(3).*E, 2*x(1)*x(2)*(t-x(3)).*E, t*x(2)*t.^x(3).*E];
%% plot the data points and the approximation curve
clf;
plot(t,y,'o'); hold on;
xp = [0:0.01:5]';
yp = x(1)+x(2)*xp^x(3)*exp(-x(4)*xp);
plot(xp, yp, '-');
pause;
%% END

4 Commenti

We can't see your error. You didn't post it.
What did you pass in for x0, lambda, max_it, and Tol?
Considering that the inputs are [1 1 2 0.5], 0.01, 500, 10^-5, it can be seen that you are trying to concatenate arrays that are not of same size into variable dr which will give an error.
The first element concatenated in dr is 1 a 1*1 element.However it seems that the oher elemenst concatenated in variable dr seem to be 13*1 sized array. Also some lements of 13*1 sized seem to following incorrect dimensions for matrix multiplication.
Therefore to correcedtly show dr you can do the following:
dr=[ones(1,13)', t.^x(3).*E,log(t)*x(2).*t.^x(3).*E, 2*x(1)*x(2)*(t-x(3)).*E, t*x(2).*t.^x(3).*E];

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Richiesto:

il 11 Dic 2020

Commentato:

il 15 Dic 2020

Community Treasure Hunt

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

Start Hunting!

Translated by