lsqnonlin evaluation limit problem

I solved nonlinear overdetermined equation system in MATLAB. But there is an error about evaluation limit:
lsqnonlin stopped because it exceeded the function evaluation limit,
options.MaxFunctionEvaluations = 4.000000e+02.
How can i solve it?
MY CODE is:
clear all
clc
x1=0;
y1=0;
z1=4;
x2=0;
y2=0.6;
z2=4;
x3=0.519;
y3=0.3;
z3=4;
x4=0.173;
y4=0.3;
z4=4.3;
x5=0.173;
y5=0.3;
z5=3.7;
t21=-0.00137932379670;%s
t31=-0.00164153231470;%s
t41=-0.00100559463380;%s
t51=-0.00100817984660;%s
% t21=0.00165340883470;%s
% t31=0.00183950027910;%s
% t41=0.00018624479890;%s
% t41=0.00092230781380;%s
c=340.0;%m/s
D21=t21*c;%m
D31=t31*c;%m
D41=t41*c;%m
D51=t51*c;%m
k1=((x1)^2+(y1)^2+(z1)^2);
k2=((x2)^2+(y2)^2)+(z2)^2;
k3=((x3)^2+(y3)^2)+(z3)^2;
k4=((x4)^2+(y4)^2)+(z4)^2;
k5=((x5)^2+(y5)^2)+(z5)^2;
%A=D21^2+2*D21*D1==-2*x*(x2-x1)-2*y*(y2-y1)-2*z*(z2-z1)+k2-k1;
%B=D31^2+2*D31*D1==-2*x*(x3-x1)-2*y*(y3-y1)-2*z*(z3-z1)+k3-k1;
%C=D41^2+2*D41*D1==-2*x*(x4-x1)-2*y*(y4-y1)-2*z*(z4-z1)+k4-k1;
%E=D51^2+2*D51*D1==-2*x*(x5-x1)-2*y*(y5-y1)-2*z*(z5-z1)+k5-k1;
%D=D1^2==x^2+y^2-2*x*x1-2*y*y1-2*z*z1+k1;
f = @(x) [-2*x(1)*(x2-x1)-2*x(2)*(y2-y1)-2*x(3)*(z2-z1)+k2-k1-(D21^2+2*D21*x(4));
-2*x(1)*(x3-x1)-2*x(2)*(y3-y1)-2*x(3)*(z3-z1)+k3-k1-(D31^2+2*D31*x(4));
-2*x(1)*(x4-x1)-2*x(2)*(y4-y1)-2*x(3)*(z4-z1)+k4-k1-(D41^2+2*D41*x(4));
-2*x(1)*(x5-x1)-2*x(2)*(y5-y1)-2*x(3)*(z5-z1)+k5-k1-(D51^2+2*D51*x(4));
x(1)^2+x(2)^2-2*x(1)*x1-2*x(2)*y1-2*x(3)*z1+k1-(x(4)^2)]; % define f
for a=0:1:5
for b=0:1:5
for c=0:1:5
for d=0:1:5
x0 = [a;b;c;d]; % initial guess
[xs,fnorm2] = lsqnonlin(f,x0); % find solution xs
fnorm = sqrt(fnorm2);
if fnorm<0.1
break;
fnorm
end
end
end
end
end

Antworten (1)

Walter Roberson
Walter Roberson am 9 Aug. 2023

0 Stimmen

Initialize
opts = optimoptions('lsqnonlin', 'MaxFunctionEvaluations', 10000, 'MaxIterations', 10000);
LB = []; UB = []; A = []; B = []; Aeq = []; Beq = []; NONLCON = [];
Then change
[xs,fnorm2] = lsqnonlin(f,x0); % find solution xs
to
[xs,fnorm2] = lsqnonlin(f, x0, LB, UB, A, B, Aeq, Beq, NONLCON, opts); % find solution xs

4 Kommentare

Mustafa Duran
Mustafa Duran am 9 Aug. 2023
Thanks, it works but this time MATLAB gives the error of:
"lsqnonlin stopped because the final change in the sum of squares relative to
its initial value is less than the value of the function tolerance."
Also there is another problem about solution that it never converges. I wrote the loop for convergency but it doesn't work. I don't know how can i set initial guesses that will converge.
Suppose that you had
f = @(x) x.^2 - 2
with scalar x. Interpreting, the function would be looking for x^2 - 2 == 0, which is x^2 == 2 . Algebraically the solution is x == sqrt(2) .
But we are working with floating point numbers, not with algebraic numbers. We know that sqrt(2) is irrational: there does not exist any finite rational in integers A/B such that A/B exactly equals the irrational number sqrt(2) . There exists some representable floating point number X such that X^2 is just slightly less than 2, and such that the next representable number after X, when squared, is just slightly more than 2. In binary double precision, those values turn out to be Xlow = 1592262918131443/1125899906842624 1.41421356237309492343001693370752036571502685546875 and Xhigh = 6369051672525773/4503599627370496 = 1.4142135623730951454746218587388284504413604736328125
If you were to run such a function through lsqnonlin, the best possible it can do is to find either the floating point value that is just slightly less than the irrational sqrt(2), or else to find the floating point value that is just slightly more than the irrational sqrt(2) . For the one just slightly lower, x.^2-2 would be slightly negative; for the next representable number, x.^2-2 would be just slightly positive.
Now, how should lsqnonlin talk about either of these solutions that we can prove algebraically are the best possible floating point solutiosn in IEEE754 Binary Double Precision ? It would have determined that the residue was slightly negative or slightly positive. Should lsqnonlin report back to the user that it has definitely found the exact solution? No! Because it would not have found the exact solution! The exact solution would be the irrational number sqrt(2) that requires an infinite amount of storage to represent in decimal.
So... nearly all of the time, lsqnonlin should not report that it has "found the solution". Nearly all of the time, upon "success" then lsqnonlin should be reporting that it has gotten as close to a solution as you can reasonably get considering the user configuration and considering the limits of floating point arithmetic.
The only time that lsqnonlin could meaingfully report that it has "found the solution" would be if it managed to find parameters such the residue it calculated was exactly zero -- not "so closer to zero that it is clear that the left-over is due to floating point error": it can only report that it has found "the" solution if the model fit the data exactly including all difficulties due to floating point limitations.
Therefore, lsqnonlin is always going to be stopping for some reason, The calculated residue being exact 0 is very unlikely to happen for non-linear functions (more likely to happen for linear functions.)
The message about stopping because final change (etc.) is not an error condition or warning at all: it is one of the two main normal terminations when everything went very well. (The other main normal termination has to do with having reduced the step size to as small as possible.)
In any case where a residue of exactly zero cannot be achieved, then lsqnonlin is going to have to stop for some reason -- and the message you are seeing is for the best possible outcome in finite precision calculations.
Stephen23
Stephen23 am 9 Aug. 2023
Bearbeitet: Stephen23 am 9 Aug. 2023
"but this time MATLAB gives the error of.."
In addition to what Walter Roberson wrote, note that it is not an error message (error messages are always red and (usually) start with the text "Error..."), it is simply the default display text the the optimzation routines print to the command window, to inform you how the optimization went (Walter Roberson explained why this is required/useful information).
You can find other display options in the documentation:
Mustafa Duran
Mustafa Duran am 10 Aug. 2023
That clarified well to me and my code. Whatever i put as initial values, the results always converges only one solution. When i put that values into my equations, it fits.
Thanks.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 9 Aug. 2023

Kommentiert:

am 10 Aug. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by