# How to fit experimental data using NonLinearModelFit

I am trying to fit the experimental data with the below equation but I am failing to get a good fit This is the data

dataInput = Import["https://pastebin.com/raw/cLxyZbdy","TSV"]; (* Pastebin *)
(*dataInput = Import["C:\\Users\\Lenovo\\Documents\\VSM\\CR2O3\\fitting\\cr2o3-900-5k.DAT"]; *)


Or alternatively, here is a Google Drive link to my data file cr2o3-900-5k.DAT Below is the code I am using

data = dataInput[[All, {1, 2}]]
P1 = ListLinePlot[data, PlotStyle -> Blue]
fm2 = NonlinearModelFit[
data, {(m*2)/π}*
ArcTan[(H + c)/c] Tan[(π (r/m))/2] + ϰ*
H, {{m, .00010, 1}, {c, 1*10^-5, 1*10^-3}, {r,
0.00001, .0001}, {ϰ, 0.0001, 1}}, H];
fm2@"BestFitParameters"
P2 = Plot[fm2[H], {H, -7, 7},
PlotStyle -> {Brown, AbsoluteThickness}]
fm1 = NonlinearModelFit[
data, {(m*2)/π}*
ArcTan[(H - c)/c] Tan[(π (r/m))/2] + ϰ*
H, {{m, .00010, .1}, {c, 1*10^-5, 1*10^-7}, {r, 0.001,
0.0001}, {ϰ, 0.0001, 1}}, H];
fm1@"BestFitParameters"
P3 = Plot[fm1[H], {H, -7, 7}, PlotStyle -> {Red, AbsoluteThickness}]
Show[P1, P2]
Show[P1, P3]
Show[P1, P2, P3]
fm1["RSquared"]

• Mariam, your data is almost featureless linear trend on which a superimposed very small-amplitude "waves". That will be hard to fit to a complicated model, given that the noise is sometimes the same order of magnitude as your "features". Your fits are reasonably good too, so what exactly do you not like in them? You show two attempts, which one is "better"? What would you consider an improvement here? Jul 27, 2022 at 17:43
• Consider also that some of your parameters are highly correlated. Look at the result of TableForm[fm1@"CorrelationMatrix", TableHeadings -> {{m, c, r, \[CurlyKappa]}, {m, c, r, \[CurlyKappa]}}] to see that m and r are perfectly correlated, and m and chi are strongly correlated as well. Jul 27, 2022 at 17:51
• Finally, the formula for your model and the code don't seem to completely agree: your tan term in the code contains r/m, but the formula does not contain m. Jul 27, 2022 at 17:52
• Thank you for the reply... Sorry i made mistake in the equation.. I updated the correct equation Jul 27, 2022 at 20:22
• Just to amplify @MarcoB 's comment: Your data does not support such a complicated model. Take a look at fm1["CorrelationMatrix"] // MatrixForm and fm2["CorrelationMatrix"] // MatrixForm. You'll see several perfect correlations among the parameter estimators.
– JimB
Jul 27, 2022 at 21:17

data = Import["https://pastebin.com/raw/cLxyZbdy","TSV"];


First of all, as pointed out by @MarcoB and JimB in the comments, your parameters are highly correlated. You need to remove the dependent parameters in your model. Here I propose the following model

model = a h + b ArcTan[(c+h)/c]


where $$b = \frac{2 m \tan \left(\frac{\pi r}{2 m}\right)}{\pi }$$. This model is perfectly equivalent but reduces the number of correlated parameters.

Second, you need a well-justified initial guess, the easiest is to just first fit a simpler linear model and extract estimations from there.

fit1= LinearModelFit[
data
, x, x
]


Considering that model /. h-> 0 gives $$b \pi/4$$ we can have an initial estimation for two of the parameters of our new model.

initialguess = {
{a, fit1-fit1},
{b, 4 fit1/Pi },
{c, 0.5}
} I don't have a good estimator for $$c$$, so I did that by hand, but one should always put some thought into how to extract initial guesses directly from the data.

Only now we can try a complete fit. Here the trick is to try many Methods suitable to your problem. If you can afford the computational time, also increase the MaxIterations or any other parameters that allow more intensive computation. You may want to impose constraints on some of the parameters.

fit2= NonlinearModelFit[
data
, model
, initialguess
, h
, MaxIterations->10000
, Method-> "NMinimize"
]


I encourage you to use and share well-indented code so it's easier to understand by humans.

I suspect your model just doesn't fit the data very well. Your data es clearly an odd function, antisymmetric, and your model is not. • "perfectly equivalent" is a good term! I think it's both comforting and discomforting that such overparameterized models get the exact same predictions. It's just the interpretation of the coefficients then becomes suspect. The discomforting part is that there is no warning message produced.
– JimB
Jul 28, 2022 at 15:37
• Thank you very much... I really appreciate all the comments and help given... Aug 14, 2022 at 15:13