I'm relatively new to mathematica and stack exchange.
Problem
Generally my problem concerns finding a simultaneous fit of multiple functions to multiple datasets with shared parameters. I have looked around on the site and found answers that are related, but I do not understand them well enough to apply them to my problem.
Data set
I have two datasets
Angle1 = {{0, 0.001267}, {1, -0.141422}, {2, -0.133453}, {3, -0.0169253}, {4, \
-0.140393}, {5, 0.118724}, {6, -0.0233602}, {7, 0.149894}, {8,
0.100233}, {9, 0.0537179}, {10, 0.126666}, {11, -0.0862719}, {12,
0.0427405}, {13, -0.154478}, {14, -0.063259}, {15, -0.0935049}, \
{16, -0.102047}, {17, 0.0430433}, {18, -0.0514891}, {19,
0.145682}, {20, 0.0302002}, {21, 0.12978}, {22, 0.0692114}, {23,
0.0112025}, {24,
0.0429632}, {25, -0.115417}, {26, -0.0113211}, {27, -0.150221}, \
{28, -0.0382864}, {29, -0.068477}, {30, -0.0196468}, {31,
0.0622656}, {32, 0.0154166}, {33, 0.141016}, {34, 0.0246072}, {35,
0.110946}, {36, -0.00387868}, {37,
0.00243512}, {38, -0.0389535}, {39, -0.0981468}, {40, -0.0393236}, \
{41, -0.118592}, {42, 0.00494229}, {43, -0.0531087}, {44,
0.0596777}, {45, 0.0391946}, {46, 0.0741465}, {47, 0.0876917}, {48,
0.0284071}, {49, 0.0652053}, {50, -0.0481605}, {51,
0.00220623}, {52, -0.0970869}, {53, -0.0443616}, {54, -0.077955}, \
{55, -0.041659}, {56, -0.00255205}, {57, -0.00286267}, {58,
0.0752706}, {59, 0.0288197}, {60, 0.100016}, {61, 0.0221289}, {62,
0.0572026}, {63, -0.0166022}, {64, -0.0182085}, {65, -0.0491495}, \
{66, -0.0709576}, {67, -0.0415291}, {68, -0.0682095}, {69,
0.00631128}, {70, -0.0213288}, {71, 0.0597776}, {72,
0.0275423}, {73, 0.0765956}, {74, 0.0406725}, {75, 0.0414907}, {76,
0.0144316}, {77, -0.0216552}, {78, -0.0219976}, {79, -0.0687343}, \
{80, -0.0331619}, {81, -0.0689351}, {82, -0.00725226}, {83, \
-0.0267323}, {84, 0.0350728}, {85, 0.0236445}, {86, 0.0581202}, {87,
0.0458593}, {88, 0.040742}, {89,
0.0287969}, {90, -0.00708384}, {91, -0.00761179}, {92, -0.0520955}, \
{93, -0.0301516}, {94, -0.0634684}, {95, -0.0195791}, {96, \
-0.0356969}, {97, 0.015318}, {98, 0.00879205}, {99, 0.0458155}, {100,
0.037456}}
and
Angle2 = {{0, 0}, {1, 0.143818}, {2, -0.132713}, {3,
0.0118337}, {4, -0.1458}, {5, -0.117606}, {6, -0.0345583}, {7, \
-0.134432}, {8, 0.10405}, {9, -0.0336773}, {10, 0.15337}, {11,
0.0818693}, {12, 0.0735132}, {13, 0.115748}, {14, -0.0661012}, {15,
0.0490807}, {16, -0.152187}, {17, -0.0455817}, {18, -0.112748}, \
{19, -0.0861975}, {20, 0.0170691}, {21, -0.0494257}, {22,
0.133856}, {23, 0.0183578}, {24, 0.142837}, {25, 0.0526415}, {26,
0.0404222}, {27,
0.0327021}, {28, -0.0913037}, {29, -0.0105467}, {30, -0.149699}, \
{31, -0.0282741}, {32, -0.0930239}, {33, -0.00641377}, {34,
0.0300158}, {35, 0.025835}, {36, 0.123172}, {37, 0.0284471}, {38,
0.119973}, {39, -0.0084316}, {40,
0.0314016}, {41, -0.0515769}, {42, -0.0687385}, {43, -0.0557139}, \
{44, -0.107006}, {45, -0.00768534}, {46, -0.064696}, {47,
0.0593166}, {48, 0.0142202}, {49, 0.0896455}, {50, 0.0649965}, {51,
0.0536615}, {52, 0.0561598}, {53, -0.0265258}, {54,
0.00763876}, {55, -0.0924048}, {56, -0.0314241}, {57, -0.0950168}, \
{58, -0.0282001}, {59, -0.0328297}, {60, 0.0074833}, {61,
0.0483554}, {62, 0.0364819}, {63, 0.0911143}, {64, 0.0267823}, {65,
0.069726}, {66, -0.0174401}, {67,
0.00725685}, {68, -0.0594089}, {69, -0.046907}, {70, -0.0610184}, \
{71, -0.0558964}, {72, -0.015532}, {73, -0.0222802}, {74,
0.0463841}, {75, 0.0190395}, {76, 0.080154}, {77, 0.0313585}, {78,
0.0619664}, {79, 0.00657388}, {80,
0.00616336}, {81, -0.0306139}, {82, -0.0466691}, {83, -0.0448602}, \
{84, -0.0614553}, {85, -0.0201856}, {86, -0.0338365}, {87,
0.0273543}, {88, 0.0093166}, {89, 0.0624234}, {90, 0.0327679}, {91,
0.0584536}, {92, 0.0206134}, {93,
0.0178648}, {94, -0.0131709}, {95, -0.0305871}, {96, -0.037543}, \
{97, -0.0538897}, {98, -0.0300648}, {99, -0.0395105}, {100, 0.00579}}
Desribing the pitching and yawing motion of an object flying $100$ m. The first element in each curly bracket is position (meters), the second is an angle (radians).
Code and question
To perform a simultanous fit of multiple functions I, perhaps naively, tried the ResourceFunction called MultiNonLinearModelFit as follows
nlmPitch = ResourceFunction["MultiNonlinearModelFit"][{Angle1, Angle2},
{
K1 E^(\[Lambda]1 x)
Cos[\[Phi]1 + \[Phi]1dot x + \[Phi]1dotdot *x^2/2] +
K2 E^(\[Lambda]2 x)
Cos[\[Phi]2 + \[Phi]2dot x + \[Phi]2dotdot *x^2/2] + K3 Cos[p x],
K1 E^(\[Lambda]1 x)
Sin[\[Phi]1 + \[Phi]1dot x + \[Phi]1dotdot *x^2/2] +
K2 E^(\[Lambda]2 x)
Sin[\[Phi]2 + \[Phi]2dot x + \[Phi]2dotdot *x^2/2] +
K3 Sin[p x] + K4
},
{K1, \[Lambda]1, \[Phi]1, \[Phi]1dot, \[Phi]1dotdot,
K2, \[Lambda]2, \[Phi]2, \[Phi]2dot, \[Phi]2dotdot, K3, p, K4}, x]
All the parameters in the two trial solutions are real.
Unfortunately, I ended up with the following error message
NonlinearModelFit::sszero: The step size in the search has become less than the tolerance prescribed by the PrecisionGoal option, but the gradient is larger than the tolerance specified by the AccuracyGoal option. There is a possibility that the method has stalled at a point that is not a local minimum.
Consequently, my solution is not at all well-behaved or correct.
Is there a way to remedy this issue, or perhaps another more fruitful approach I can try?
Additional notes
Both the datasets and fitting solutions can be written in complex notation by replacing sines and cosines by exponents. The problem can then be viewed as fitting one complex function to a set of points in the complex plane, but I'm not sure whether or not it is useful.
Angle1
using the linear modeleasyFit=With[{k=0.48},Fit[Angle1,{Cos[k*n],Sin[k*n],1},n]]
. Thek=0.48
was adjusted by hand. TryListPlot[{Angle1[[;;,2]],Table[easyFit,{n,0,100}]}]
, it looks reasonable. Similar forAngle2
. Perhaps you can use this to choose reasonable starting values. $\endgroup$toyData=Table[{n,Cos[0.64*n]},{n,0,100}]
. Simply callingNonlinearModelFit[toyData,Cos[b*n],{b},n]
fails to converge. With a good starting value of say0.6
(meaning replace{b}
by{{b,0.6}}
) it manages to discoverCos[0.64*n]
. But already for0.59
it fails. To understand why, plot the error as a function ofb
usingWith[{f=Sum[(Cos[0.64*n]-Cos[b*n])^2,{n,0,100}]}, Plot[f,{b,0.5,0.8},PlotRange->All,AxesLabel->{"b","fiterror"}]]
. $\endgroup$