# Combined NonlinearModelFit [duplicate]

I'm trying to fit a number of data sets. Each of the sets should evaluate to a simple linear solution.ax+b. The problem is, all data sets have interference from a function of the form cx^2+dx. So, what I'm trying to do is evaluate the sets together such that each set gets a resulting fit of the form ax+b+e(cx^2+dx), where e varies per data set, where c and d are the same across all data sets, with a and b either seperate or together.

For instance three small sets:

data1={{11/1250, 1.96334}, {19/1250, 9.32588}, {3/125, 19.1426}, {17/500,28.4685},
{1/20, 40.2486}, {9/125, 54.9736}, {9/100, 65.772}, {11/100, 82.4604},
{33/250,93.2588},{81/500, 106.021}, {1/5,108.966}, {6/25, 117.801}, {7/25, 124.672},
{17/50, 142.342}};
data2={{3/125, 0.981672}, {9/250, 6.8717}, {27/500, 20.6151}, {7/100,26.5051},
{11/125, 36.6164}, {11/100, 48.1019}, {33/250,51.0469}, {79/500, 63.8087},
{24/125, 77.5521}, {23/100, 86.3871}};
data3={{1/1000, 0.}, {11/2500, 0.}, {11/1250, 5.89003}, {2/125,7.26437},
{1/40, 15.5104}, {17/500, 30.8245}, {13/250, 44.8624}, {7/100, 54.8755},
{41/500, 66.2629}, {27/250, 71.9566}, {33/250,78.6319}, {4/25, 97.7745},
{47/250,107.689}, {29/125, 117.212}};


Where each are Transpose[{xn,yn}]

I already tried this technique. I'm having some problems with the code there, but I doubt that's going to work.

alldata =
Join[Transpose[{ConstantArray[1, Length[x1]], x1, y1}],
Transpose[{ConstantArray[2, Length[x2]], x2, y2}],
Transpose[{ConstantArray[3, Length[x3]], x3, y3}]];
function[a_, b_, c_, d_, e_, f_] = a x + (d + e + f) (b x + c x^2);
params1 = {a, b, c, d};
params2 = {a, b, c, e};
params3 = {a, b, c, f};
fitall = NonlinearModelFit[alldata,KroneckerDelta[index - 1]*function[params1] +
KroneckerDelta[1, 2]*function[params2] + KroneckerDelta[1, 3]*function[params3],
{a, b, c, d, e, f}, x]


Which gives the rather obvious error:

NonlinearModelFit::fitc: Number of coordinates (2) is not equal to the number of variables (1). >>


A plot of the data sets and an approximate of what the end result should look like the following figure:

-

## marked as duplicate by Oleksandr R., m_goldberg, Artes, Sjoerd C. de Vries, sschJun 3 '13 at 17:20

Probable duplicate of either (15905) or (866). – Oleksandr R. Jun 2 '13 at 22:38

You can define you data set and model as :

alldata = Join[{1, Sequence @@ #} & /@ data1,
{2, Sequence @@ #} & /@ data2,
{3, Sequence @@ #} & /@ data3];

model[index_, x_, a_, b_, c1_, d1_, c2_, d2_, c3_, d3_] =
a x + b + KroneckerDelta[index, 1] (c1 x^2 + d1 x) +
KroneckerDelta[index, 2] (c2 x^2 + d2 x) +
KroneckerDelta[index, 3] (c3 x^2 + d3 x);


The fit is then done with :

nlm = NonlinearModelFit[alldata, model[index, x, a, b, c1, d1, c2, d2, c3, d3],
{a, b, c1, d1, c2, d2, c3, d3}, {index, x}];


Check :

Show[ListPlot[{data1, data2, data3}],
Plot[{nlm[1, x], nlm[2, x], nlm[3, x]}, {x, 0, 0.4}]]


-
The relations weren't entirely as I wanted them, but with this correct code format I could easily figure the rest out: model[index_, x_, a_, b_, c1_, d1_, c2_, d2_, c3_, d3_] = a x + b + KroneckerDelta[index, 1] c1 (d1 x^2 + d2 x) + KroneckerDelta[index, 2] c2 (d1 x^2 + d2 x) + KroneckerDelta[index, 3] c3 (d1 x^2 + d2 x); Thank you. – Feyre Jun 2 '13 at 14:48