# Finding NonlinearModelFit of multiple data sets with the same parameters and in two dimensions

I have multiple sets of data (between 3 and 6 depending on the cases) dependent of space, time, and some parameters. The data are the response of a harmonic oscillator under a non-trigonometrical driving force. I want to fit them using a function dependent of space, time, and those parameters, which has the form:

f[x,t,par1,par2]


All of the parameters BUT ONE must be the same for all the data sets, while time is a continuous variable for all data sets, and space is a discrete variable, constant for each set. I already have minimized the chi squared, which is in the form:

Sqrt[Sum[Sum[(f[x,t,par1,par2][data[[i]]]-data[[i]]^2)^2/data[[i]]^2],{i,1,Length[data]}],{x,1,2,3,4}]


but the result is not satisfying and moreover it gives no Parameter errors.

The parameter which changes for each data set is the phase, which I insert in the formula by writing

f[x, t+phase, par1, par2]


Is there a way to make a non linear model fit of my data all in one go? I know similar questions have been asked, but my dataset has the further complication of depending on two variables, one of which is discrete, and adapting those answers to my problems is beyond my capabilities.

• The answer is a definite Yes (and it would depend on the exact model if NonlinearModelFit or use of LogLikelihood would be the best approach). Are you able to share either the complete model (including the error structure) and/or some sample data? – JimB Apr 21 '16 at 15:44
• I'm afraid that I cannot share the fitting function, and the datasets are rather large (~ 2x10000 matrixes), but I think that a good approximation would be a sine function dependent of time which suffers a phase shift if you change x (distance)... or maybe sine is too simple, I would have no problem fitting that, if I didn't have to use f. – maythemoonshine Apr 21 '16 at 16:12
• Anyway, my data are from a harmonic oscillator which reacts to an external non-trigonometrical driving force – maythemoonshine Apr 21 '16 at 16:19
• It's not sounding like you have "two dimensions" as in a two-dimensional response but rather from your comments it sounds like you have a single response variable with two predictors (x and t). The mix of continuous and discrete isn't problematic on the predictor side of the equation. Just concatenate the 3x10000 matrices with each row representing {x,t,y} where y is the response variable. (And, of course, initially testing this with just a subset of the data). – JimB Apr 21 '16 at 16:46

If you restructure your datasets to have each row representing {x,t,y} with y as the response variable, it should be pretty straightforward. Here is an example with some simulated data.

(* Define a function *)
f[x_, t_, p1_, p2_] := p1 + Sin[x + t p2]

(* Common parameters *)
p1 = 0.2;
p2 = 2;

(* Data set 1 *)
x1 = 1;
data1 = Table[Flatten[{x1, t/100., f[x1, t/100, p1, p2] +
0.05*RandomVariate[NormalDistribution[0, 1], 1]}], {t, 0, 100}];

(* Data set 2 *)
x2 = 0.1;
data2 = Table[Flatten[{x2, t/100., f[x2, t/100, p1, p2] +
0.05*RandomVariate[NormalDistribution[0, 1], 1]}], {t, 0, 100}];

(* Combine data *)
data = Join[data1, data2];

(* Get the fit *)
nlm = NonlinearModelFit[data, f[x, t, a, b], {a, b}, {x, t}];
nlm["BestFitParameters"]
(* {a -> 0.207475, b -> 2.01058} *)

ListPlot[{data1[[All, {2, 3}]], data2[[All, {2, 3}]],
Transpose[{data[[All, 2]], nlm["PredictedResponse"]}]},
PlotStyle -> {Green, Red, Black}]


• it works!!! But I've made a mistake: there's only the one parameter that changes for each data set, and it's making the whole rest of the fit wrong... How do I adapt what you wrote to this? (it's the phase shift, by the way) – maythemoonshine Apr 22 '16 at 12:24
• You'll have to be more specific. Possibly you could take the example function I've written above and modify it to match your needs. Does that parameter after the contribution of x? some function of t? some function of x and t? Editing your question to account for what you've stated in your comment would be in order. – JimB Apr 22 '16 at 13:03
• done, I hope it is clear enough – maythemoonshine Apr 22 '16 at 19:54
• I think if phase is included only as an addition to t, then there's nothing to change. In other words if f[x,5+7,par1,par2] results in the same value as f[x,2+10,par1,par2], then no adjustment is needed. If, however, phase (not a parameter but a predictor variable) has its own parameter, then you'll need to include {x,t,phase,y} for each row in the datasets and change f to allow for inputting of phase separately. – JimB Apr 22 '16 at 20:45
• it worked like a charm, thanks :) – maythemoonshine Apr 28 '16 at 12:51