# Problem in using FindFit

I have the following set of data

data={{0,0,0},{0,2,1},{0,4,2.247},{0,6,3.627},{0,8,5.031},{1,0,3.346}};


where the values are {n, L,$$\varepsilon$$} and satisfy the following equations

$$E(n,L) = 2n+1 + \sqrt{L(L+1)-\frac{3}{4}(L)^2 + 1 + \beta_0^4}$$

e[n_, L_] = 2n + 1 + Sqrt[L(L + 1) - 3/4 L^2 + 1 + b0^4]


$$\varepsilon = \frac{E(n,L)-E(0,0)}{E(0,2)-E(0,0)}$$,

where $$\beta_0$$ should be determined. I don't know how I can use FindFit command of Mathematica to find the best value of $$\beta_0$$ to have the best fit for $$\varepsilon$$.

e[n_, L_] = 2n + 1 + Sqrt[L(L + 1) - 3/4 L^2 + 1 + b0^4]

FindFit[data, (e[n, L] - e[0, 0])/(e[0, 2] - e[0, 0]), b0, {n, L}]


{b0 -> 1.3514967}

Which seems reasonable in view of the residuals:

Plot[Evaluate[(e[#, #2] - e[0, 0])/(e[0, 2] - e[0, 0]) - #3 & @@@ data], {b0, 0, 3}]


The brown and purple residual has bigger slope around the roots in the plots. Hence for Mathematica to minimize the sum of squares in the y-dimension, the mean of the 2 data points that correspond to the big slopes are cared more about than the others. It is purpose specific whether this is appropriate. If it isn't you can add the NormFunction-option to FindFit.

• Thank you dear @Coolwater. How does Mathematica recognize {n,L} for each data? Nov 12, 2018 at 7:25
• Also, how does it recognize that the expr is the 3rd value of every data element? Nov 12, 2018 at 7:34
• FindFit assumes its first argument has the form {{var1, var2, ..., varN, expr}, ... , {var1, var2, ..., varN, expr}} where {var1, var2, ..., varN} is the 4th argument of FindFit Nov 12, 2018 at 7:36
• Ok! Given b -> {1.27225, 1.29505, 1.28573, 1.40411} having Mean=1.31428 and Medean=1.29039 do you think Mathematica did a good job? Anyway +1 from me Nov 12, 2018 at 7:52
• @J42161217 It uses least squares, see edit Nov 12, 2018 at 8:05

You can also use NMinimize. First we need to write cost function, i.e. residual.

data = {{0, 0, 0}, {0, 2, 1}, {0, 4, 2.247}, {0, 6, 3.627}, {0, 8,
5.031}, {1, 0, 3.346}};
e[n_, L_] := 2 n + 1 + Sqrt[L (L + 1) - 3/4 L^2 + 1 + b0^4]
cost[b0_] =Sum[(e @@data[[i, 1 ;; 2]] - (data[[i, 3]] (e[0, 2] - e[0, 0]) +
e[0, 0]))^2, {i, 6}];
(*or Total[(e[#1, #2] - (#3 (e[0, 2] - e[0, 0]) + e[0, 0]))^2 & @@@ data]*)

fit = NMinimize[cost[b0] , b0]


{0.0196376, {b0 -> 1.35462}}

Since your cost function has only one variable you can also use grid search.

Ordering[val,1] gives position of min value.

b0Val = Range[0, 10, 0.0001];
val = cost[b0Val];
b0Val[[Ordering[val, 1]]]


{1.3546}

Note that there is another min at b0=-1.3546

b0Val = Range[-1000, 1000, 0.001];
val = cost[b0Val];
b0Val[[Ordering[val, 2]]]


{-1.3546, 1.3546}

We can plot cost function

$$\text{cost}(b0)=\left(-5.031 \left(\sqrt{\text{b0}^4+4}-\sqrt{\text{b0}^4+1}\right)-\sqrt{\text{b0}^4+1}+\sqrt{\text{b0}^4+25}\right)^2\\+\left(-3.627 \left(\sqrt{\text{b0}^4+4}-\sqrt{\text{b0}^4+1}\right)-\sqrt{\text{b0}^4+1}+ \sqrt{\text{b0}^4+16}\right)^2\\+\left(2-3.346 \left(\sqrt{\text{b0}^4+4}-\sqrt{\text{b0}^4+1}\right)\right)^2+\left(-2.247 \left(\sqrt{\text{b0}^4+4}-\sqrt{\text{b0}^4+1}\right)-\sqrt{\text{b0}^4+1}+\sqrt{\text{b0}^4+9}\right)^2$$

Plot[cost[b0], {b0, -10, 10}]