fit multiple curves with external parameters (lsqcurvefit)

6 views (last 30 days)
Hello,
It's been a while I am looking of a way to pass extra parameters to a fitted function together with fitting a single model to several data sets. I have come with a solution that looks very convenient to me but I wonder if there are issues that I am not aware of. Please excuse me if this has been already asked many times.
I use the lsqcurvefit function from the optimization toolbox and proceed as follow:
[output]=lsqcurvefit(@model, x0, Xdata, Ydata, ...)
where, instead of having Xdata a vector, I pass a matrix. It appears that only the first column of the matrix Xdata is used for the optimization of the least square problem and so I use additional column to pass extra parameters.
Example: I need to fit a single model to a serie of curves that correspond to different external magnetic field strength. I thus need to also pass the magnetic field strength for each data point to my function "model". I then simply create the matrix.
Xdata=[x11, b11 ;
x12, b12;
...
x21, b21;
x22, b22;
... ];
YData=[y11; y12; ... ;y21; y22; ...];
where xij denotes the jth data point of the ith dataset and b a parameter (magnetic field, dataset tag, ...). This is typically very convenient to use dataset tag to handle data set with different length.
My questions are: Is this leading to some problems for the optimization process? Does that influence the confidence interval of my fitting parameters? Is there any known issues with passing a Xdata matrix instead of a vector? I have tried on my datasets, it looks fine, but I want to make sure that everything is right...
Many thanks in advance for your replies,
Vincent

Accepted Answer

Matt J
Matt J on 30 Sep 2014
Edited: Matt J on 30 Sep 2014
There is no restriction on the size or shape of Xdata, Ydata (or x0 for that matter). Xdata can be some size. Ydata can be a different size. x0 can have dimensions different from both Xdata and Ydata. All elements in Xdata will be used in the fit assuming your model() functions makes use of them in some way.
It is a bit peculiar to pass known curve parameters like b inside Xdata. More commonly, you would do something like this
fun=@(x,xdata) model(x,xdata,b);
[output]=lsqcurvefit(fun, x0, Xdata, Ydata, ...)
where in this case Xdata contains only your xij and not your bij.
Xdata=[x11 ;
x12 ;
...
x21 ;
x22 ;
... ];
However, I cannot see any ill-consequence of doing it your way, except that it might be less convenient if you later want to manipulate fun(x,xdata) as a function of the xij only ...
  1 Comment
Vincent
Vincent on 30 Sep 2014
Thank you very much! That's what I needed!
Yes, I was thinking of using this way of passing extra paramteter, as well as using global variables (but I try to avoid as much as possible to use them), but I found this other way very convenient. I really like that the parametera corresponding to the x data point x(i,1) is simply x(i,2:end). It becomes especially usefull in big dataset. On the other hand, I could also pass a matrix b with the size of xdata with the method you suggest and simply call b(i,:)...
Thanks again.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!