lsqcurvefit for several data sets at once

Hello,
I would like to generate a function using lsqcurvefit. I do have a few data sets from several experiments. Every experiment has different parameters and one variable. Now I would like to generate a function with lsqcurvefit. But I want this function to be optimised for all data sets at once. Is there a way to do this? Or should I use another tool for that?
For example my data looks like this:
% Experiment 1
a1 = 0.416;
b1 = 2;
c1 = 2;
x1 = [0 0.33 0.67 1 1.33 2];
y1 = [1 0.89 0.8 0.44 0.62 0.37];
% Experiment 2
a2 = 0.801;
b2 = 0.67;
c2 = 4;
x2 = [0 0.17 0.33 0.5 0.67];
y2 = [1 0.8 0.84 0.83 0.81];
% and so on...
Thanks for your answers!

4 commentaires

Adam Danz
Adam Danz le 11 Mar 2019
lsqcurvefit() is a function. Why don't you just loop through each of your data sets?
Fabian Hartmann
Fabian Hartmann le 11 Mar 2019
I would like to create one function fitting for all data sets, not for each. Sorry I think that was unclear. In the end I would like to input 5 data sets and get 1 function that fits all the data.
Adam Danz
Adam Danz le 11 Mar 2019
Modifié(e) : Adam Danz le 11 Mar 2019
So you'd like to combine the data across experiments and fit it is one large data set? You mentioned that data from each experiment has different parameters which would potentially weaken the goodness of fit and/or variance of the data. Maybe I don't understand still. What would your inputs look like?
Yes you´re right, I would like to combine them into one large data set. I know I wouldnt get the best fit for the function but I need one function that works for all experiments.
In my example from before I have the constants a, b and c for experiment 1-5. Then I have my variable x and the result y for experiment 1-5.
The function I would like to have could look like this:
fun = @(xi,x)x(1)*exp(x(2)*xi/b1)+x(3)*(ai/ci)
% with i = 1, 2, 3, 4, 5
This function should be optimised for all data sets at once and just give me one value for x(1) , x(2), x(3) which fit all data sets.
I hope you can understand my explanation.

Connectez-vous pour commenter.

 Réponse acceptée

Adam Danz
Adam Danz le 11 Mar 2019
Modifié(e) : Adam Danz le 12 Mar 2019
Continuing from the comment section under the question, to combine data sets (x1, x2....) and (y1, y2, ...) just combine them in the same order to preserver the 1:1 correspondence between the xdata and ydata inputs. For the x0 initial starting points and the upper/lower bounds, you should systematically choose the best options from your individual data sets.
x = lsqcurvefit(fun,x0,[x1, x2], [y1, y2],lb,ub)

7 commentaires

Ok great, thank you! I am now at this with my code:
a1= 0.416;
b1 = 2;
x1 = [0 0.33 0.67 1 1.33 2];
y1 = [1 0.89 0.8 0.44 0.62 0.37];
a2 = 0.801;
b2 = 0.67;
x2 = [0 0.17 0.33 0.5 0.67];
y2 = [1 0.8 0.84 0.83 0.81];
xdata = linspace(0,2);
fun = @(x,xdata)x(1)*exp(x(2)*xdata/b1);
x0 = [1,1];
x = lsqcurvefit(fun,x0,[x1, x2],[y1, y2])
How can I now take into account the different constants for each data set? Right now it just has b1 as an input for fun = ...
Adam Danz
Adam Danz le 12 Mar 2019
When you combine the data sets, it becomes one set of data that is fed into the function. lsqcurvefit() finds the coefficients to best fit the nonlinear function defined in 'fun' to the 'ydata'. Your x and y data should all come from (roughly) the same underlying function and lsqcurvefit() will find the best coefficients to fit the function. If your data sets come from very different underlying functions, you shouldn't combine them.
John D'Errico
John D'Errico le 12 Mar 2019
Modifié(e) : John D'Errico le 12 Mar 2019
Adam is correct, in that if the sets of data come from the same populations, and the same sets of parameters, then just combine them.
Be careful though, as at least as far as I can see, they don't seem to do so. The relevant thing to look at is a plot like this:
plot(x1/b1,y1,'ro',x2/b2,y2,'bs')
See that I've carefully scaled x1 and x2 by the corresponding b1 and b2 values. So be very careful. In order to combine the two sets, you need to scale each x vector by the corresponding b, FIRST, before you combine them.
If the red and blue markers all clearly lie along the same line/curve, then you are ok But clearly here, they are not even close. All that seems consistent between those curves is that at x==0, y==1. For larger x, the two curves seem to diverge significantly.
So I would SERIOUSLY reconsider modeling these two sets of data as one single curve, with one common set of parameters. At least, if this is your real data, then that would seem to be a rather poor assumption.
Fabian Hartmann
Fabian Hartmann le 12 Mar 2019
Hey John,
the data is from an experiment. I have performed 5 testing series using different concretes. Each testing series/experiment has been performed the same way, the only thing changed is the concrete. The concrete has different parameters which I labeled as a1, a2... & b1, b2... Within each testing series I have my variable x1, x2... For every x1 I have the constant a1 and so on.
And a1 and b1 are not the only constants which is why your plot probably doesnt show a common sense.
I now need to somehow find a function to model these experiments. Maybe lsqcurvefit isnt the best option. Do you have an idea?
Adam Danz
Adam Danz le 12 Mar 2019
If you're comparing the results between concrete types, why would you combine the data? Wouldn't you fit the data to each concrete type and then compare the coefficients?
Fabian Hartmann
Fabian Hartmann le 12 Mar 2019
I was hoping to skip the step of comparing each coefficient and have matlab figure out what works best for all. But thanks a lot, your answers have already helped me a lot.
Adam Danz
Adam Danz le 12 Mar 2019
Modifié(e) : Adam Danz le 12 Mar 2019
I enjoying helping and I learn a lot by doing so. It seems that the question you're asking isn't quite clear and I think the questions should be rock solild before you start with the solutions. For example, if you're trying to measure the difference in performance between the different concretes, you could fit a function to each data set and plot out the coefficients to determine the influence of each parameter on each concrete type. If you're just determining if there is a statistically significant difference between the concrete types a simple analysis of variance would be a better measure than curve fitting.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with Curve Fitting Toolbox dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by