# How to speed up FindRoot?

How can I speed up my FindRoot time, for the below code? It takes 25 seconds on my machine to find one single root for a set of $$g$$, $$mx$$, $$m\psi$$ values, I need to take around $$10^5$$ such points; I am stuck. Can I reduce the timing? And the function sigv is a numerical integration here.

xff[g_, mx_, mψ_?NumericQ] := Re[x /. FindRoot[
x - Log[(0.038*2*1.22*10^19*mψ*sigv[x, g, mx, mψ])/Sqrt[107*x]],
{x, 100}]]


**Where sigv[x,g,mx,$m\psi$] is **

sigv[x_, g_, mx_, m\[Psi]_?NumericQ] = x/(8 m\[Psi]^5*BesselK[2, x]*
BesselK[2, x]) (NIntegrate[
sig[x, g, mx, m\[Psi]]*s^(1/2)*BesselK[1, s^(1/2)*x/m\[Psi]], {s,
4 m\[Psi]^2, Infinity}, WorkingPrecision -> 15,
AccuracyGoal -> 20, PrecisionGoal -> 20, MaxRecursion -> 20]);


And sig[x,g,mx,$m\psi$]

sig[x,g,mx,m\[Psi]] = 1/(2 + 4 Sqrt[2] E^-x)^4 16 (1 + 25.0038 E^(-2 x) +
3.98149 E^-x)^2 ((Sqrt[(1 -
0.00002116/s)/(1 - (4 m\[Psi]^2)/s)] (0. +
0.459003 g^4 (0.00001058 + s) (-4 m\[Psi]^2 +
s)))/(4 \[Pi] s (0.0334177 g^4 mx^4 + (-mx^2 +
s)^2)) + (Sqrt[(1 -
0.00009216/s)/(1 - (4 m\[Psi]^2)/s)] (0. +
0.293596 g^4 (0.00004608 + s) (-4 m\[Psi]^2 +
s)))/(2 \[Pi] s (0.0334177 g^4 mx^4 + (-mx^2 +
s)^2)) + (Sqrt[(1 -
0.0361/s)/(1 - (4 m\[Psi]^2)/s)] (0. +
0.293596 g^4 (0.01805 + s) (-4 m\[Psi]^2 +
s)))/(4 \[Pi] s (0.0334177 g^4 mx^4 + (-mx^2 +
s)^2)) + (Sqrt[(1 -
6.5025/s)/(1 - (4 m\[Psi]^2)/s)] (0. +
0.459003 g^4 (3.25125 + s) (-4 m\[Psi]^2 +
s)))/(4 \[Pi] s (0.0334177 g^4 mx^4 + (-mx^2 +
s)^2)) + (Sqrt[(1 -
120007./s)/(1 - (4 m\[Psi]^2)/s)] (0. +
0.459003 g^4 (60003.4 + s) (-4 m\[Psi]^2 +
s)))/(4 \[Pi] s (0.0334177 g^4 mx^4 + (-mx^2 +
s)^2)) + (Sqrt[(1 -
1.04448*10^-6/s)/(1 - (4 m\[Psi]^2)/s)] (0. +
0.293596 g^4 ((-1.04448*10^-6 + s) s +

4 m\[Psi]^2 (-s +
2.61121*10^-7 (7 - (6 s)/mx^2 + (3 s^2)/
mx^4)))))/(4 \[Pi] s (0.0334177 g^4 mx^4 + (-mx^2 +
s)^2)) + (Sqrt[(1 -
0.04469/s)/(1 - (4 m\[Psi]^2)/s)] (0. +
0.293596 g^4 ((-0.04469 + s) s +
4 m\[Psi]^2 (-s +
0.0111725 (7 - (6 s)/mx^2 + (3 s^2)/
mx^4)))))/(4 \[Pi] s (0.0334177 g^4 mx^4 + (-mx^2 +
s)^2)) + (Sqrt[(1 -
12.6309/s)/(1 - (4 m\[Psi]^2)/s)] (0. +
0.293596 g^4 ((-12.6309 + s) s +
4 m\[Psi]^2 (-s +
3.15773 (7 - (6 s)/mx^2 + (3 s^2)/
mx^4)))))/(4 \[Pi] s (0.0334177 g^4 mx^4 + (-mx^2 +
s)^2)))

• I don't think we can recommend anything unless you show us what sigv is, as the bottleneck is probably there. – Roman Apr 22 '19 at 6:51
• If your 10^5 points are close to one another, you could use the preceding solution as a starting point for FindRoot. Another possible problem: you call NIntegrate with options AccuracyGoal and PrecisionGoal of 20, though the integrand involves hardware floating-point numbers: that precision can't be achieved. You might also try different integration methods. – user64074 Apr 22 '19 at 10:37
• Can you give an example of typical g, mx, mψ values? – Chris K Apr 22 '19 at 10:44
• g varies from 0.01 to 0.75, mx from 1000 to 5000 and $m\psi$ 800-2500. Although the solution will depend on the step size given for these ranges. – Immy Salam Apr 22 '19 at 13:58
• I don't see your definition of the function sig[x_,g_,mx_,m\[Psi]_] := ... You only have an expression sig = ... and yet you also have NIntegrate[sig[x, g, mx, m\[Psi]] ... Please explain. – Somos Apr 22 '19 at 16:20