# Fitting integral of complicated function into a dataset

So, I'm trying to adjust a model to an experiment, this model, more specifically:

$\dpi{120}&space;B=\frac{\mu&space;_{0}I}{2\pi\sqrt{d^{2}+\left&space;(&space;R^{'}&space;+&space;r\right&space;)^2}}&space;\left&space;(&space;\frac{R^{'2}-d^{2}-r^{2}}{d^{2}+(r-R^{'})^{2}}&space;E&space;_{2}(k)+&space;E&space;_{1}(k)&space;\right&space;)$

While:

$\dpi{120}&space;k&space;=&space;\sqrt{4r{R}'(d^{2}+({R}'+r)^2)^{-1}}$

The only parameter to be adjusted is I, the problem here is, I not only need to plot this with all the errors and so, as I need to fit the integral of this in respect of "r" from 0 to 2*pi, with "d" being the x-axis value.

I'm extremely new to Mathematica, I've been barely able to just plot the data points with errors, without any fitting whatsoever, how should I proceed? And, is it feasible at all?

Oh,and E_1/E_2 are complete elliptic integrals of first and second kind, respectively…

Edit: This is my current code, however it doesn't wields me more than a bunch of warnings and errors, and, this is the actual model implemented: $\dpi{100}&space;I&space;=&space;\int_{0}^{2\pi}&space;\int_{0}^{r}&space;\frac{\mu&space;_{0}if\sqrt{2}}{2\pi&space;R&space;\sqrt{d^{2}+\left&space;(&space;R^{'}&space;+&space;r\right&space;)^2}}&space;\left&space;(&space;\frac{R^{'2}-d^{2}-r^{2}}{d^{2}+(r-R^{'})^{2}}&space;E&space;_{2}(k)+&space;E&space;_{1}(k)&space;\right&space;)&space;\mathrm{dr}\mathrm{d\theta&space;}$

• Can you post the data as well? Dec 1, 2017 at 11:29
• Of course!.And, this is the code I've been (trying to) working in, in the last hours. Dec 1, 2017 at 11:31
• In your code Sqrt[_] (like all functions) must use square brackets rather than parentheses. And the call to the function field must include its arguments. Dec 1, 2017 at 13:50
• @BobHanlon Oh, Thank you! Such a simple problem… C++ Habits… However, I have a new problem, here's my current code, now it gives me "FindFit::fitm: Unable to solve for the fit parameters; the design matrix is nonrectangular, non-numerical, or could not be inverted.", and I don't really know what it means. Dec 1, 2017 at 17:04
• New updated current version…, I'm still not sure what the problem may be, I've tried all the suggestions I could find, and, yet, nothing… Dec 1, 2017 at 18:11

Download your data file and delete the .txt extension before importing.

data = Import["/Users/roberthanlon/Downloads/coil_datawu.csv"];


The data then consists of lists of pairs of numbers rather than a string.

Union[{Head[#], Length[#], And @@ (NumberQ /@ #)} & /@ data]

(* {{List, 2, True}} *)

(* mu=4 e-7*Pi *)

mu = 4*10^-7 * Pi; (* Assuming you intended this *)
frequency = 8313 // Rationalize;


There is no need to restrict the arguments of field to be numeric since it does not use numeric techniques.

field[r_, x_, mu_, Radius_, frequency_] :=
mu*frequency*
Sqrt[2]/(2*Pi*


However, define the model using NIntegrate which then necessitates that the arguments to model be numeric. Note that i must also be an argument and the integrand should not be enclosed in List brackets.

Clear[model]

frequency_?NumericQ] :=

solution = FindFit[data, model[i, x, mu, Radius, frequency], {{i, 0.7}}, x]

(* {i -> 12.7577} *)

{xmin, xmax} = MinMax[data[[All, 1]]];


However, the model does not appear to fit the data.

Plot[model[i, x, mu, Radius, frequency] /. solution,
{x, xmin, xmax},
Epilog -> {Red, AbsolutePointSize[3], Point[data]}]


NonlinearModelFit produces the same results.

nlm = NonlinearModelFit[data, model[i, x, mu, Radius, frequency], {{i, 0.7}},
x];

nlm["BestFitParameters"]

(* {i -> 12.7577} *)

Plot[nlm[x], {x, xmin, xmax},
Epilog -> {Red, AbsolutePointSize[3], Point[data]}]