Objective function is returning undefined values at initial point. lsqcurvefit cannot continue.

42 visualizzazioni (ultimi 30 giorni)
Hello guys, I know it's possibly too simple but I have a question;
xdata=[2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018];
y= [0.1,0.2,0.5,1,5,6,7,19,25,48,258,842,3007,5109];
plot(t,N(t), 'ro')
F = @(x,xdata)x(1)*((1-exp(-(x(2)+x(3))).^xdata)./(1+((x(3)/x(2))*exp(-(x(2)+x(3)).^xdata))));
x0=[0.1,0,0];
[x,resnorm,~,exitflag,output]=lsqcurvefit(F,x0,t,y);
hold on
plot(t,F(x,t))
hold off
When I wrote this code, I got
Error using lsqncommon (line 15)
Objective function is returning undefined values at initial point. lsqcurvefit
cannot continue.
Error in lsqcurvefit (line 278)
lsqncommon(funfcn,xCurrent,lb,ub,options,defaultopt,optimgetFlag,caller,...
Error in EMM1 (line 10)
[x,resnorm,~,exitflag,output]=lsqcurvefit(F,x0,t,y);"
How can i fix this problem ?
  1 Commento
Adam Danz
Adam Danz il 25 Mag 2021
Modificato: Adam Danz il 25 Mag 2021
Welcome to the forum. I've edited your question to format your code. In the future please use the code/text toggle in rich text editor to format your code.

Accedi per commentare.

Risposta accettata

Alex Sha
Alex Sha il 26 Mag 2021
The best results are as below, hard to be obtained since the initial-start values are impossible to be guessed reasonally.
Root of Mean Square Error (RMSE): 26.4992273987196
Sum of Squared Residual: 9830.92673820673
Correlation Coef. (R): 0.999856022308787
R-Square: 0.99971206534715
Parameter Best Estimate
---------- -------------
x1 6001.07347038251
x2 2.38080287700935E-243
x3 1.00314103540412
Note the value of x2, extremely small, but can not to be zero.
  2 Commenti
Eren Ulusoy
Eren Ulusoy il 8 Giu 2021
Hi Alex, thanks for the response. Can you please share the code that gives this result? I'm still struggling to find it.
Alex Sha
Alex Sha il 9 Giu 2021
Hi, the results were obtained by another package called "1stOpt", the code is as below, guessing of initial start values is not required:
Algorithm = DE1;
Function y = x1*((1-exp(-(x2+x3))^xdata)/(1+((x3/x2)*exp(-(x2+x3)^xdata))));
Data;
xdata=[2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018];
y=[0.1,0.2,0.5,1,5,6,7,19,25,48,258,842,3007,5109];
When running above code in 1stOpt, the result below will be got:
Root of Mean Square Error (RMSE): 26.4992273987196
Sum of Squared Residual: 9830.92673820672
Correlation Coef. (R): 0.999856022308634
R-Square: 0.999712065346844
Parameter Best Estimate
---------- -------------
x1 6001.07347055296
x2 2.38080287816193E-243
x3 1.00314103540412
====== Output Results ======
No. Observed y Calculated y
1 0.1 6.5324798657854E-6
2 0.2 3.54011266002914E-5
3 0.5 0.000192868545632992
4 1 0.00105637532045177
5 5 0.00581694072945544
6 6 0.0322030188245443
7 7 0.179235582439475
8 19 1.00287006514251
9 25 5.63817036463822
10 48 31.7561469332492
11 258 176.252497298153
12 842 885.290308114866
13 3007 2993.18985796258
14 5109 5112.53339393831

Accedi per commentare.

Più risposte (1)

Andreas Apostolatos
Andreas Apostolatos il 25 Mag 2021
Hi Eren,
Welcome to the forum from me as well.
The code snippet you shared cannot be executed because variable 'x' is nowhere defined. Nevertheless, I assume that you would like to pass 'xdata' as third argument to function 'lsqcurvefit', namely,
xdata=[2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018];
y= [0.1,0.2,0.5,1,5,6,7,19,25,48,258,842,3007,5109];
% plot(t,N(t), 'ro')
F = @(x,xdata)x(1)*((1-exp(-(x(2)+x(3))).^xdata)./(1+((x(3)/x(2))*exp(-(x(2)+x(3)).^xdata))));
x0=[0.1,0,0];
[x,resnorm,~,exitflag,output]=lsqcurvefit(F,x0,xdata,y);
This results in the error message you mentioned, that is,
Error using lsqncommon (line 15)
Objective function is returning undefined values at initial point.
lsqcurvefit cannot continue.
The error is in this sense self-explaining: You provide an initial guess to function 'lsqcurvefit', where the value of the objective function has undefined values. As a matter of fact, you can check that by evaluating the objective function yourself on the starting point, namely,
F(x0, xdata)
ans =
Columns 1 through 12
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Columns 13 through 14
NaN NaN
If you change the initial guess 'x0' to another value where the objective function is defined, then the algorithm works without such an error. For instance, try to change the initial guess from 'x0=[0.1,0,0];' to 'x0=[0.1,0.1,0.1];', namely,
xdata=[2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018];
y= [0.1,0.2,0.5,1,5,6,7,19,25,48,258,842,3007,5109];
% plot(t,N(t), 'ro')
F = @(x,xdata)x(1)*((1-exp(-(x(2)+x(3))).^xdata)./(1+((x(3)/x(2))*exp(-(x(2)+x(3)).^xdata))));
x0=[0.1,0.1,0.1]; % Modified initial guess
[x,resnorm,~,exitflag,output]=lsqcurvefit(F,x0,xdata,y);
this already yields a solution in terms of function 'lsqcurvefit'.
I hope that this information helps you further.
Kind regards,
Andreas
  2 Commenti
Eren Ulusoy
Eren Ulusoy il 9 Giu 2021
Hello Andreas,
When I write this corrected code to panel, I can not get the values of X's as result. Is there anything wrong that might done by me?
Andreas Apostolatos
Andreas Apostolatos il 9 Giu 2021
Hi Eren,
Indeed you do not get an optimal solution in the latter case because MATLAB asserts the following warning,
Local minimum possible.
lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the value of the function tolerance.
If you check the return value of variable 'exitflag', you will notice that it is 3 in the latter case, namely,
exitflag =
3
which according to the documentation it means that "Change in the residual is less than the specified tolerance." see the following link for more information,
The latter means that an optimal solution is not found. You can try changing the initial guess 'x0' to a value where the objective is defined and where the 'lsqcurvefit' algorithm converges with a value for 'exitflag' equal to 1, so that "Function converged to a solution x.", see the latter documentation page for more information about the possible values of 'exitflag' and their meanings.
When I change the initial guess of your particular optimization problem to 'x0=[0,0,0.001]', then I get the following results,
>> [x,resnorm,~,exitflag,output]=lsqcurvefit(F,x0,xdata,y)
Initial point is a local minimum.
Optimization completed because the size of the gradient at the initial point
is less than the value of the optimality tolerance.
<stopping criteria details>
x =
1.0e-03 *
0 0 1.0000
resnorm =
3.5923e+07
exitflag =
1
output =
struct with fields:
firstorderopt: 0
iterations: 0
funcCount: 4
cgiterations: 0
algorithm: 'trust-region-reflective'
stepsize: 1
message: '↵Initial point is a local minimum.↵↵Optimization completed because the size of the gradient at the initial point ↵is less than the value of the optimality tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The final point is the initial point.↵The first-order optimality measure, 0.000000e+00, is less than↵options.OptimalityTolerance = 1.000000e-06.↵↵'
Therefore, it is advisable to try out different initial guesses 'x0' for your setup until the algorithm converges with 'exitflag' equal to 1.
I hope this helps you with the understanding.
Kind regards,
Andreas

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by