Why do I receive an error message when using LSQCURVEFIT in the Optimization Toolbox?

4 views (last 30 days)
I receive the error message
>> x = lsqcurvefit(fun,x0,xdata,ydata)
Error using lsqcurvefit (line 262)
Function value and YDATA sizes are not equal.
when using LSQCURVEFIT in the Optimization Toolbox.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 Jul 2019
Edited: MathWorks Support Team on 25 Jul 2019
This error message occurs when the size of the output from the objective function is different than the size of the ydata vector of values.
Please check your equation in the objective function MATLAB file to make sure that the size of the vector to be returned matches up with the ydata vector. (Pay special attention when you use () matrix multiplication or (.) element by element multiplication.)
A simple way to check the output of your objective function is to pass it your xdata and initial values (x0) to ensure that the values returned are in the right form. For example:
A = myobj(x,xdata)
% Where 'myobj' is the objective function.
Make sure that A is of the same size as the ydata vector.
Also, please check that input arguments order of the object function is correct.
In the object function for LSQCURVEFIT, the first input argument should be a coefficient x and the second input argument should be the input data xdata.
% myobj = @(xdata,x)x(1)*exp(x(2)*xdata); % Wrong
myobj = @(x,xdata)x(1)*exp(x(2)*xdata); % Correct

More Answers (0)

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!