So I am trying to make a Levernberg-Marquadt method in matlab. And I am somehow keep getting horzcat error.. Can someone help me fix the problem so that I can stop getting the horzcat error?
This is my code:
function [xc] = Lev_Mar(x0, lambda, max_it, Tol)
for i = 1:max_it
[r,A]=rdr_1(x0);
fprintf('x(%2d) = [%12.5e %12.5e %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
%% For problem 1:
function [r, dr]=rdr_1(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, E.*t.^(x(3)), E.*x(2).*log(t).*t.^(x(3)), -E.*x(2).*t.^(x(3)+1)];
%% 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;
and my x0 = [1 1 2 0.5]; lambda = 0.01; max_it = 500; Tol = 10^-5

Antworten (1)

Matt J
Matt J am 12 Dez. 2020

0 Stimmen

dr = [t.^0, E.*t.^(x(3)), E.*x(2).*log(t).*t.^(x(3)), -E.*x(2).*t.^(x(3)+1)];

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 12 Dez. 2020

Beantwortet:

am 12 Dez. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by