Supplied objective function must return a scalar value - non linear regression

조회 수: 3 (최근 30일)
Hello,
I am trying to use fminunc to minimize the MSE for a nonlinear regression problem.
When I try to run it, I recieve the error "Supplied objective function must return a scalar value".

채택된 답변

Jon
Jon 2021년 10월 25일
편집: Jon 2021년 10월 25일
The objective function for fminunc must be a scalar. The function you supply to fminunc returns a vector because p is a vector, and this raises the error message.
You have many other problems though in your code besides this The main issue is that you seem to be unclear about what it is you are minimizing. You should be trying to minimize the error between your fitted values and your experimental value.
So your objective function should include for example something like:
mse = objective(p)
yc = p(1)*exp(p(2)*x)+p(3)*exp(p(4)*x); % vectorized don't need a loop to evaluate term by term
mse = sum((yc - y).^2)/numel(y)
You can use a nested function to pass x and y (better than using a global as you have).
Also you should plot the fitted and the experimental values on the same plot. In your plot statement you just plot the first and last fitted values with a line connecting them. This of course will be a straight line and doesn't show you anything more about your fit.
Please try to correct some of these problems and if you are still stuck I can try to get you moving again
  댓글 수: 1
John D'Errico
John D'Errico 2021년 10월 25일
편집: John D'Errico 2021년 10월 25일
I'd say this hits all of the high points. Good answer. (+1) I would want to add a few thoughts...
If the OP has the optimization toolbox to use fminunc, then why not use a tool like lsqnonlin or lsqcurvefit instead? That would save a step. It would use a tool designed to solve that class of problems, so perhaps more likely to gain a decent result.
Oh, one other point, 4 parameters with only 5 data points is asking for problems. Trying to fit a model as a sum of two exponentials makes it almost impossible to get any decent results out the end, when you have so few data points. If there is ANY noise at all in the data, you will find it corrups the estimates severely. Even rounding the data to only a few significant digits is effectively a source of noise in such a problem.
Finally, using a blanket arbitrary set of starting values like [1 1 1 1] is asking for even more insanity as a result. Better startng values are a HUGE asset in estimating a sum of exponentials model, as these models are classically terrible to estimate. One thing you do want to void is to use the same rate parameters for both exponential terms. That almost insures the optimizer will have problems from the very first iteration.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품

Community Treasure Hunt

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

Start Hunting!

Translated by