I am trying to simultaneously fit two data sets that are separate functions of $x$ and $t$ with NonlinearModelFit.
Here is an example of the data:
datax = {{1, 2}, {2, 3}, {3, 4}, {4, 5}};
datat = {{1, 4}, {2, 7}, {3, 10}, {4, 13}};
Here is an example of models:
modelx[a_,b_,x_]:=a*x+b;
modelt[a_,b_,t_]:=a*t+b;
The parameters a and b are shared across the two models.
Here is an example of the code I'd hoped to use:
ResourceFunction["MultiNonlinearModelFit"][Join[{datax, datat}],
{modelx[a, b, x], modelt[a, b, t]}, {a, b}, {x, t}];
The resulting error message is:
NonlinearModelFit: Number of coordinates (2) is not equal to the number of variables (3).
Edit
Consider the two data sets below. The first is a decaying exponential, the second is a polynomial. The first is a function of time, the second is a function of position.
nPoints = 256;
noiseLevel = 10^-1.5;
dataDecay = Table[{t, Sin[2 [Pi] 1.0 t] Exp[-0.5 t] + RandomReal[NormalDistribution[0, noiseLevel]]}, {t, 0, 10, 10/nPoints}] // Chop;
dataPolys = Table[{x, 1 + (1.0 x^2 + 0.5 x)/100 + RandomReal[NormalDistribution[0, noiseLevel]]}, {x, 0, 10, 10/nPoints}] // Chop;
I would like to create data sets that indicate whether the data belong to time or position:
dataDec = ArrayFlatten[{{Transpose[{ConstantArray[1, Length[dataDecay]]}], dataDecay}}];
dataPol = ArrayFlatten[{{Transpose[{ConstantArray[2, Length[dataPolys]]}], dataPolys}}];
I then model the data with the functions that created them:
modelDec[a_, b_, t_] := Sin[2 \[Pi] a t] Exp[-b t];
modelPol[a_, b_, x_] := 1 + (a x^2 + b x)/100;
So, in theory, when fitting the data, a should come out near 1 and b should come out near 0.5.
fit = NonlinearModelFit[Join[dataDec, dataPol], {modelDec[a, b, t] + modelPol[a, b, x]}, {{a, 1.0}, {b, 0.5}}, {t, x}, MaxIterations -> Infinity];
However, the results are far from this:
In[84]:= fit["BestFitParameters"]
Out[84]= {a -> 0.743405, b -> -0.203007}
ResourceFunction["MultiNonlinearModelFit"][Join[{datax, datat}], {modelx[a, b, x], modelt[a, b, x]}, {a, b}, {x}]Also, are you sure you actually want $a$ and $b$ to be the same in both models? Plotting your data, this clearly isn't the case ... – Domen Oct 09 '23 at 10:22xor justt. Also: why are you adding the models together? – Sjoerd Smit Oct 09 '23 at 12:51RandomReal[NormalDistribution[0, noiseLevel]]seems to be an odd construction. Might you wantRandomVariate[NormalDistribution[0, noiseLevel]]. – JimB Oct 09 '23 at 16:07MultiNonlinearModelFitas @Domen suggests. But to make your code work tryfit = NonlinearModelFit[Join[dataDec, dataPol], If[i == 1, modelDec[a, b, x], modelPol[a, b, x]], {{a, 1.0}, {b, 0.5}}, {i, x}, MaxIterations -> Infinity];. – JimB Oct 09 '23 at 17:01