# Integrating FindRoot

I have a certain function F[a_] := FindRoot[f[a,x],{x,0.5}] and I want to compute NIntegrate[F[t],{t,0,1}]. However, F returns the list {x -> N} for some number $$N$$. I am not sure how to tweak my definition of F so that Mathematica does the root-finding and takes out the value from the list in one go, so that I can integrate the function.

## Edit

The function I am considering is the inverse of the elliptic integral of the second kind, EllipticE. Because there is no inverse in Mathematica, I have defined my own:

EInvrs[a_,k_] := FindRoot[EllipticE[x, a/(a-1)] - k/a^2, {x, 0.5}]


and I am trying to evaluate the following integral:

NIntegrate[a*Sqrt[1 - 2 a]/4 * Cos[EInvrs[2 k/(1 - a), a^2/(a - 1)^2]],{k,0,f[a]/4}]


(This is the second integral in a double integral, where a is the second variable to be integrated over.) However, this is not working, as EInvrs[a_,k_] returns a list, not a number.

• The problem you state would be solved by x /. FindRoot[...] in the definition. But I am not sure about your definition because FindRoot[f[x], {x, 0.5}] should always return the same value, i.e. the root around 0.5. You may want to give us context about the larger problem you are trying to solve. Commented Apr 15, 2020 at 18:42
• how could this work? you are passing a number to F[t]? so F[x] becomes like FindRoot[f[0.94],{0.94,0.5}]] Also better to post a fully working MWE with all the definitions Commented Apr 15, 2020 at 18:42
• The edit you added is still missing definitions of f[a] you are using. Please start all over and just post all the code in one segment fully as is without text in between the code. i.e. code which one can copy all at once. Not bits and pieces here and there that one has to stitch together. Commented Apr 15, 2020 at 19:04
• Redefine EInvrs[a_, k_] := x /. FindRoot[EllipticE[x, a/(a - 1)] - k/a^2, {x, 0.5}] // Chop and show f[a] Commented Apr 15, 2020 at 19:08
• @UlrichNeumann It's an unrelated function (not the one I mentioned at the start of my post). I should have made that clear. Commented Apr 15, 2020 at 19:29

Here is a working version of your code (note the messages):

Clear[EInvrs]
EInvrs[a_,  k_] := (x /. Flatten[FindRoot[EllipticE[x, a/(a - 1)] - k/a^2, {x, 0.5}]]);

EInvrs[20, 1]

(* 0.0025 + 1.60478*10^-17 I*)

Block[{a = 50, f},
f[a_] := a;
NIntegrate[a*Sqrt[1 - 2 a]/4*Cos[EInvrs[2 k/(1 - a), a^2/(a - 1)^2]], {k, 0, f[a]/4}]
]

(* During evaluation of In[145]:= FindRoot::nlnum: The function value {-(625./k^2)+EllipticE[0.5,-((0.0408163 k)/(-1.-0.0408163 k))]} is not a list of numbers with dimensions {1} at {x} = {0.5}. *)

(* During evaluation of In[145]:= NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in k near {k} = {0.976368}. NIntegrate obtained 0. +148.94 I and 18.261278096131498 for the integral and error estimates. *)

(*  0. + 148.94 I *)

• Somehow, I think the numerical result is suspicious. Letting EInvrs[a_?NumericQ, k_?NumericQ] := Block[{x}, x /. First[FindRoot[EllipticE[x, a/(a - 1)] - k/a^2, {x, 1/2}]]], and evaluating the NIntegrate[] for various settings of WorkingPrecision, I get varying results. The OP will probably need to investigate more deeply. Commented Apr 17, 2020 at 14:04
• Plotting the integrand shows that it oscillates wildly near the origin. It's not terribly surprising that NIntegrate` is complaining. Commented Apr 17, 2020 at 14:24