Infinite Recursion in own Levenberg-Marquardt Code
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello there,
I am writing an Levenberg-Marquardt Algorithm to calculate a circle out of ten points.
x1 and y1 are 10 points of a circle randomly generated. I am this far:
f=@(x)((x1-x(1)).^2+(y1-x(2)).^2-x(3).^2); %Function of the circle with x, y and r as x(1), x(2), x(3)
DF = @(x) [2.*((sqrt((x1-x(1)).^2 + (y1-x(2)).^2) - x(3)).*(x(1)-x1))./(sqrt((x1-x(1)).^2 + (y1-x(2)).^2));
2.*((sqrt((x1-x(1)).^2 + (y1-x(2)).^2) - x(3)).*(x(2)-y1))./(sqrt((x1-x(1)).^2 + (y1-x(2)).^2));
2.*(x(3)-sqrt((x1-x(1)).^2 + (y1-x(2)).^2))]; % DF = Derivates of x, y, r of f
x0=[0,0,1]
mu0=1; %some tolerances
beta0 = 0.3;
beta1 = 0.9;
maxit = 100;
tol = 1*10^-5;
%algorithm
n=length(x0);
k=0;
mu=mu0;
x=x0;
s=-(DF(x0)*DF(x0)' + mu^2*eye(n))\(DF(x0)*f(x0)');
while norm(s)>tol && k<maxit
[s,mu] = korrektur(f,DF,x,mu,beta0,beta1);
x=x+s;
k=k+1;
end
function[s,mu] = korrektur(f,DF,x,mu,beta0,beta1)
n=length(x);
s=-(DF(x)*DF(x)'+mu^2*eye(n))\(DF(x)*f(x)'); %%%%%%%%% Error appears because of this line %%%%%%%%%%%
eps_mu=(f(x)*f(x)'-f(x+s)*f(x+s)')\(f(x)*f(x)'-(f(x)'+DF(x)'*s)*(f(x)'+DF(x)'*s)');
if eps_mu <= beta0
[s,mu]=korrektur(f,DF,x,mu,beta0,beta1);
elseif eps_mu >= beta1
mu=mu/2;
end
end
Everytime I run this, i get this Error because of the marked line above.
Out of memory. The likely cause is an infinite recursion within
the program.
Can someone explain me how to fix this?
Thanks for the help
0 Kommentare
Antworten (2)
Ameer Hamza
am 3 Dez. 2020
Inside the function korrektur, you are again calling korrektur with the same value of input parameter
[s,mu]=korrektur(f,DF,x,mu,beta0,beta1);
So the recursion never breaks. Maybe, the correct is to pass eps_mu instead of mu
[s,mu]=korrektur(f,DF,x,eps_mu,beta0,beta1);
2 Kommentare
Steve Eddins
am 3 Dez. 2020
I see that korrektur is calling itself recursively. I noticed also that the recursive call appears to be identical to the top-level call:
[s,mu]=korrektur(f,DF,x,mu,beta0,beta1);
It looks like none of the quantities f, DF, x, mu, beta0, and beta1 are altered before the recursive call. That implies that eps_mu is always going to be same, which would explain why the elseif branch never gets executed to stop the recursion.
I think you need to examine this recursion more carefully to make sure that it is doing what you intend and that it will terminate successfully.
Siehe auch
Kategorien
Mehr zu Dialog Boxes finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!