1
$\begingroup$

There are two sets of data: firstHalf and secondHalf. Both firstHalf and secondHalf are fitted using the models modelPositive and modelNegative, respectively. Both models use the same parameters: MS, Hc, HT, and XX. However, because I am fitting two different data sets, I obtain two different sets of parameter values.

ClearAll["Global`*"]

firstHalf = {{3957.67883`, 0.206670237`}, {3747.69873`, 
    0.20240899499999998`}, {3527.78992`, 0.198147753`}, {3300.52271`, 
    0.19495182149999998`}, {3073.10999`, 
    0.18962526899999999`}, {2855.59607`, 0.183233406`}, {2625.66479`, 
    0.181102785`}, {2395.82104`, 0.1736456115`}, {2206.3739`, 
    0.1736456115`}, {1966.55005`, 0.1651231275`}, {1744.19604`, 
    0.15553533299999997`}, {1526.84064`, 0.14914347`}, {1334.2569`, 
    0.142751607`}, {1101.63177`, 0.13209850199999998`}, {879.18555`, 
    0.117184155`}, {681.63696`, 0.1019721602463`}, {461.74333`, 
    0.08590014032594999`}, {231.90496`, 0.0572585218161`}, {2.14822`, 
    0.025234116344549997`}, {-202.69485`, -0.01138167085095`}, \
{-434.88812`, -0.0514932744522`}, {-631.9686`, -0.0772235058966`}, \
{-831.52631`, -0.09789212756235`}, {-1080.90509`, -0.1161188445`}, \
{-1302.42688`, -0.12996788099999998`}, {-1511.8634`, -0.142751607`}, \
{-1733.69843`, -0.151274091`}, {-1950.61102`, -0.1566006435`}, \
{-2162.59778`, -0.1651231275`}, {-2381.41199`, -0.1715149905`}, \
{-2600.92322`, -0.176841543`}, {-2843.09888`, -0.1821680955`}, \
{-3077.80054`, -0.1864293375`}, {-3305.41711`, \
-0.19175588999999998`}, {-3528.12366`, -0.19388651099999998`}, \
{-3733.56042`, -0.19921306349999998`}};

ListPlot[firstHalf]

secondHalf = {{-3943.71143`, -0.20347430549999998`}, {-3861.29358`, \
-0.20240899499999998`}, {-3616.36523`, -0.20027837399999998`}, \
{-3391.66431`, -0.1970824425`}, {-3174.16101`, \
-0.18962526899999999`}, {-2956.6748`, -0.18749464799999999`}, \
{-2736.69202`, -0.1800374745`}, {-2511.75232`, -0.1757762325`}, \
{-2289.64453`, -0.172580301`}, {-2049.62738`, -0.166188438`}, \
{-1819.69543`, -0.1587312645`}, {-1609.74976`, \
-0.15447002249999997`}, {-1397.15497`, -0.1438169175`}, \
{-1177.15759`, -0.13103319149999998`}, {-939.80179`, \
-0.12038008649999998`}, {-739.64438`, -0.1075963605`}, {-504.64529`, \
-0.0877758325233`}, {-277.0927`, -0.0659759576376`}, {-62.21652`, \
-0.02901767311635`}, {125.03458`, 0.006134366799045`}, {367.19287`, 
    0.03879424063695`}, {536.7597`, 
    0.06643553258729999`}, {748.62952`, 
    0.09175061252984999`}, {990.4913`, 
    0.11185760249999999`}, {1207.34729`, 0.1246413285`}, {1429.07471`,
     0.13742505449999998`}, {1648.3551`, 
    0.14701284899999997`}, {1885.0177`, 0.1566006435`}, {2101.88232`, 
    0.16299250649999997`}, {2328.14758`, 0.1693843695`}, {2552.5177`, 
    0.174710922`}, {2782.07678`, 0.1821680955`}, {3004.18726`, 
    0.1864293375`}, {3233.97974`, 0.19069057949999998`}, {3446.79785`,
     0.19282120049999998`}, {3662.18005`, 
    0.1970824425`}, {3874.87915`, 0.20134368449999998`}};

ListPlot[secondHalf]



modelPositive[Hfield_, MS_, Hc_, HT_, 
  XX_] := (2*MS)*ArcTan[(Hfield + Hc)/HT] + XX*Hfield


modelNegative[Hfield_, MS_, Hc_, HT_, 
  XX_] := (2*MS)*ArcTan[(Hfield - Hc)/HT] + XX*Hfield


fitPositive = 
 FindFit[firstHalf, 
  modelPositive[Hfield, MS, Hc, HT, 
   XX], {{MS, 1}, {Hc, 1}, {HT, 1}, {XX, 1}}, Hfield]


fitNegative = 
 FindFit[secondHalf, 
  modelNegative[Hfield, MS, Hc, HT, 
   XX], {{MS, 1}, {Hc, 1}, {HT, 1}, {XX, 1}}, Hfield]

Print["MS = ", MS /. fitPositive]
Print["Hc = ", Hc /. fitPositive]
Print["HT = ", HT /. fitPositive]
Print["XX = ", XX /. fitPositive]
Print["MS = ", MS /. fitNegative]
Print["Hc = ", Hc /. fitNegative]
Print["HT = ", HT /. fitNegative]
Print["XX = ", XX /. fitPositive]

Show[ListPlot[{firstHalf, secondHalf}, PlotStyle -> Red, 
  AxesLabel -> {"Hfield", "Value"}, PlotRange -> All], 
 Plot[modelPositive[Hfield, MS, Hc, HT, XX] /. fitPositive, {Hfield, 
   Min[firstHalf[[All, 1]]], Max[firstHalf[[All, 1]]]}, 
  PlotStyle -> Blue], 
 Plot[modelNegative[Hfield, MS, Hc, HT, XX] /. fitNegative, {Hfield, 
   Min[secondHalf[[All, 1]]], Max[secondHalf[[All, 1]]]}, 
  PlotStyle -> Green]]

I would like to merge the data and fit both experimental data at the same time to get the same parameters. How can this be done?

fit = FindFit[{firstHalf, secondHalf}, {modelPositive, 
   modelNegative}, {MS, Hc, HT, XX}, {Hfield}]
$\endgroup$
2
  • 1
    $\begingroup$ Try ResourceFunction["MultiNonlinearModelFit"] $\endgroup$ Commented Feb 6 at 14:42
  • $\begingroup$ If you "know" that the parameters are the same (including the error variance), then the answer and comment by @UlrichNeumann is what you want. If you don't know for sure that the parameters are identical, then performing two different fits using NonlinearModelFit rather than FindFit gives you the summary statistics that allow comparisons of the two sets of parameters. $\endgroup$ Commented Feb 6 at 15:42

3 Answers 3

2
$\begingroup$

We may expand the data with an additional independent variable: pred, that indicates if the data belongs to firstHalf or secondHalf. E.g. we may set pred=0 for the firstHalf and pred=1 for the second Half. And then we join both data sets.

Further, we define a new model, that calls fitPositive if pred=0 and fitNegative if pred=1;

dat = Join[Prepend[#, 0] & /@ firstHalf, Prepend[#, 1] & /@ secondHalf];
model[pred_, Hfield_, MS_, Hc_, HT_, XX_] := 
 If[pred == 0, modelPositive[Hfield, MS, Hc, HT, XX], modelNegative[Hfield, MS, Hc, HT, XX]]

fit= FindFit[dat, model[pred, Hfield, MS, Hc, HT, XX], {{MS, 1}, {Hc, 1}, {HT, 1}, {XX, 1}}, {pred, Hfield}]

Plot[model[0, Hfield, MS, Hc, HT, XX] /. fit, {Hfield, 
  Min[firstHalf[[All, 1]]], Max[firstHalf[[All, 1]]]}, 
 Epilog -> Point[firstHalf]]

Plot[model[1, Hfield, MS, Hc, HT, XX] /. fit, {Hfield, 
  Min[secondHalf[[All, 1]]], Max[secondHalf[[All, 1]]]}, 
 Epilog -> Point[secondHalf]]

enter image description here

$\endgroup$
4
$\begingroup$

Have a look at ResourceFunction["MultiNonlinearModelFit"] (Thanks @SjoerdSmit)

fit = ResourceFunction["MultiNonlinearModelFit"][{firstHalf, 
   secondHalf}, {modelPositive[Hfield, MS, Hc, HT, XX], 
   modelNegative[Hfield , MS, Hc, HT, XX]}, {{MS, 1}, {Hc, 1}, {HT, 
    1}, {XX, 1}}, Hfield]
fit["BestFitParameters"]
(*{MS -> 0.0554761, Hc -> 132.126, HT -> 695.287, XX -> 0.0000125054}*)

Show[{Graphics[{Blue, Point[firstHalf], Red, Point[secondHalf]}],
  Plot[{fit[1, H], fit[2, H]}, {H, -4000, 4000}, 
   PlotStyle -> {Blue, Red}]}, AspectRatio -> 1, Axes -> True]

enter image description here

Hope it helps!

$\endgroup$
3
$\begingroup$

Just a variant of @DanielHuber using NonlinearModelFit

Using the data provided in firstHalf and secondHalf:

model[Hfield_, s_, MS_, Hc_, HT_, 
  XX_] := (2*MS)*ArcTan[(Hfield + s Hc)/HT] + XX*Hfield
d = Join[Prepend[#, 1] & /@ firstHalf, Prepend[#, -1] & /@ secondHalf];
m = NonlinearModelFit[d, model[x, s, p, q, r, t], {p, q, r, t}, {s, x}];
f = m["BestFitParameters"];
Grid[{{Row[{" MS: ", p}], Row[{" Hc:", q}], Row[{" HT:", r}], 
    Row[{" XX:", t}]}}, Frame -> All] /. f

Plot[{model[x, 1, p, q, r, t] /. f, 
  model[x, -1, p, q, r, t] /. f}, {x, -4000, 4000}, 
 Epilog -> Point[d[[All, {2, 3}]]]]

enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.