MATLAB: Can I use lsqcurvefit even if the data is not consistent in length? Or do I have to use fmincon

lsqcurvefitmultiple data fittingOptimization Toolbox

I have a dataset like this:
x1=1:1:5; x1=repmat(x1',1,5); y1=rand(5,5);
x2=0.5:1:5.5; x2=repmat(x2',1,5); y2=rand(6,5);
x3=1:2:5; x3=repmat(x3',1,5); y3=rand(3,5);
First I wanted to try this with lsqcurvefit:
xdata=[x1 x2 x3]; ydata=[y1 y2 y3];
But that doesn t work because of the different lengths of the Matrices. Can I use lsqcurvefit like this:
xdata=[x1;x2;x3]; ydata=[y1;y2;y3]
But then it is not really datafitting, because I bring different datasets together. I hope you know what I mean.
Thank you for your answers in advance.

Best Answer

There are no rules about how the shapes and sizes of xdata and ydata relate to one another in lsqcurvefit. It is perfectly fine, for example, to arrange your xdata and ydata as below. Notice, in particular, that use of repmat is not required here for any reason.
x1=1:1:5; y1=rand(5,5);
x2=0.5:1:5.5; y2=rand(6,5);
x3=1:2:5; y3=rand(3,5);
xdata=[x1,x2,x3];
ydata=[y1;y2;y3];
The only requirement is that your model function F(p,xdata) must produce output satisfying,
numel( F(p,xdata) ) = numel( ydata )
And, of course, the ordering of elements in the output of F(p,xdata) must be consistent with ydata. In other words, the i-th element of F(p,xdata) must be the model of ydata(i).
Related Question