Appy lsqcurvefit to multiple data sets with multiple parameters
15 views (last 30 days)
Show older comments
Hello everyone,
I have a number of experimental data sets for which I need a general correlation with three coefficients, so I though using lsqcurvefit (or fitnlm).
The structure of one data set looks like this:
Xdata_1 = (x1, x2, x3, x4, x5, x6), each xi is a column vector,
Ydata_1 = (y1), a response column vector,
fun = @(c,X) c(1)*X(:,3).^(1-c(2)).* (X(:,2).*X(:,4)).^c(2).* 1./X(:,1).^(1+c(2)).* (X(:,5)./X(:,6)).^c(3);
c0 = [0.1, 0.8, 0.33];
I am able to apply a fitting function to one data set like:
c = lsqcurvefit(fun,c0,Xdata_1,Ydata_1);
However what I need is to fit fun to multiple Xdata_i, Ydata_i to obtain the 'best' set of c-coefficients? Of course I could loop it and get for each input a set of c's, but that is not of interest.
If that is possible then I don't know the syntax. Does anyone have an idea?
Thank you.
0 Comments
Answers (2)
Jon
on 1 Jun 2023
Can you stack your data, so that if for example, using your terminology, Xdata_1,Xdata__,.. Ydata_1, Ydata_2,...
Then fit Xdata = [Xdata_1(:);Xdata_2(:);...], Ydata = [Ydata_1(:);Ydata_2,...]
4 Comments
Jon
on 2 Jun 2023
Sorry, I should have looked more carefully at your problem statement. I missed that X had 6 columns. Assuming you had many data sets, each with a different number of rows, it would still be preferable to store the whole collection in one variable rather than working with a variable for each data set, e.g Xdata_1, Xdata_2, etc.
For this purpose you could make one cell array, call it Xdata, and put all of the data in it (just to illustrate)
Xall = {rand(20,6),rand(12,6),rand(4,6),rand(11,6)}
You could then stack your data, and and apply your function to it using
X = cell2mat(Xall(:))
Note, if you only have a few data sets, its not too bad to just vertically concatenate them as @Star Strider showed using something like
X = [Xdata_1;Xdata_2]
But if you have more than a few, it would probably be better to not include the indexing in the variable names, and do something like I show above
Star Strider
on 1 Jun 2023
If I understand the problem correctly, and all the data sets contain column vectors and are the same column size (they do not have to have the same row size), one option is to vertically concatenate them —
Xdata_1 = rand(5,6);
Ydata_1 = rand(5,1);
Xdata_2 = rand(6,6);
Ydata_2 = rand(6,1);
fun = @(c,X) c(1)*X(:,3).^(1-c(2)).* (X(:,2).*X(:,4)).^c(2).* 1./X(:,1).^(1+c(2)).* (X(:,5)./X(:,6)).^c(3);
c0 = [0.1, 0.8, 0.33];
Xdata_All = [Xdata_1; Xdata_2]
Ydata_All = [Ydata_1; Ydata_2]
c = lsqcurvefit(fun,c0,Xdata_1,Ydata_1) % Separately
c = lsqcurvefit(fun,c0,Xdata_2,Ydata_2) % Separately
c = lsqcurvefit(fun,c0,Xdata_All,Ydata_All) % Conocatenated
Try this to see if it produces the result you want.
.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!