How to know when Findroot fails to find a solution

My problem is very simple. Assume you have this piece of code :

FindRoot[{x^2 + 1 == 0}, {x, -1, 1}] 

Now, this code returns

FindRoot::cvmit: Failed to converge to the requested accuracy or precision within 100 iterations. {x -> -1.} 

Which is as expected since the equation has no real root. Now my question is, how can I get the fact that FindRoot failed in a variable ? Say I want to display a message "Failed search", when FindRoot fails for whatever reason, how do I collect the fact that the function failed ? I was looking into Catch[], but it seems that no exception is thrown.

Nested NIntegrate with FindRoot

I am trying to numerically integrate a function using nested NIntegrate:

$ $ F(N,x,s)=\int_{-\infty}^s \int_{-\infty}^{+\infty} K(N,z’,x,x’) g_{x’,s’} dx’ds’ $ $

where the kernel of the integration, $ K(N,z’x,x’)$ , is a messy expression defined in the mathematica code below, and $ g_{x’,s’}$ is a bi-variate gaussian defined by:

$ $ g_{x,s’}=\frac{n}{2\pi\sigma_{x’}\sigma_{s’}}\exp\left({ -\frac{x’^2}{2\sigma_{x’}^2} }\right)\exp\left({ -\frac{s’^2}{2\sigma_{s’}^2} }\right).$ $

The tricky part(s) is that:

  1. $ z’$ in the $ K(N,z’,x,x’)$ needs to be solved for numerically using FindRoot and will have a $ s’$ dependence.
  2. The integration upper limit over $ ds’$ is a variable $ s$ .
  3. I suspect the kernel is oscillatory with $ N$ (denoted "Kernel" in the code below) so maybe an averaging of the kernel over $ N$ can be done to simplify the kernel and eliminate $ N$ if the integrations prove to be too time consuming.

At the end, I would like a function, F(N,x,s), that would be able to plot across $ s$ for a given $ (N,x)$ values i.e. Plot[F[a,b,s,{s,-1e-5,1e-5}].

(*Constants*) e = -1.60217733*10^-19; m = 9.109389699999999*10^-31; epsilon = 8.854187817620391*10^-12; re = 2.81794092*10^-15; c = 2.99792458*10^8; n = -10^-10/e; KK = 1; lw = 0.026; kw = (2 Pi)/lw; gamma = 4000/0.511; beta = Sqrt[1 - 1/gamma^2]; sigmaS = 10^-5; sigmaX = 30*10^-6; coeff = n/(2 Pi*sigmaS*sigmaX) Exp[-(xprime^2/(2 sigmaX^2))]* Exp[-(sprime^2/(2 sigmaS^2))];  (*Preliminary Equations*) rs2 = {zprime, xprime + KK/(gamma*kw) Sin[kw*zprime], 0}; ro2 = {(NN + 10000)*lw, x + KK/(gamma*kw) Sin[kw*(NN + 10000)*lw], 0};  betas = {beta - KK^2/(2 gamma^2) Cos[kw*zprime]^2,KK/gamma Sin[kw*zprime], 0}; betao = {beta - KK^2/(2 gamma^2) Cos[kw*(NN + 10000)*lw]^2,KK/gamma Sin[kw*(NN + 10000)*lw], 0};  betaDot = {(c*KK^2*kw)/(2 gamma^2)Sin[2 kw*zprime], -((KK*c*kw)/gamma) Sin[kw*zprime], 0};  deltar2 = ro2 - rs2; Rgam2 = Sqrt[deltar2[[1]]^2 + deltar2[[2]]^2];  Ec2 = (e/(4 Pi*epsilon)) (deltar2/Rgam2 - betas)/(gamma^2 Rgam2^2 (1 - (deltar2/Rgam2).betas)^3); Erad2 = (e/(4 Pi*epsilon)) Cross[deltar2/Rgam2,Cross[deltar2/Rgam2 - betas, betaDot]]/(c*Rgam2*(1 - (deltar2/Rgam2).betas)^3);  sumElong = (Ec2[[1]] + Erad2[[1]]); sumEtran = (Ec2[[2]] + Erad2[[2]]);  (*Numerical Functions*)  ZPRIME[NN_?NumericQ, x_?NumericQ, xprime_?NumericQ, s_?NumericQ, sprime_?NumericQ] := zprime /.FindRoot[s - sprime == (Sqrt[gamma^2 + KK^2] (EllipticE[kw*(NN + 10000)*lw,KK^2/(gamma^2 + KK^2)] - EllipticE[kw zprime, KK^2/(gamma^2 + KK^2)]))/(gamma kw) -beta Sqrt[((NN + 10000)*lw - zprime)^2 + (x - xprime + (KK Sin[kw *(NN + 10000)*lw])/(gamma kw) - (KK Sin[kw zprime])/(gamma kw))^2], {zprime, 0}]   Kernel = coeff re/gamma (sumElong*betao[[1]] + sumEtran*betao[[2]])/.{zprime -> ZPRIME[NN, x, xprime, s, sprime]};  FNxprimesprime[NN_?NumericQ, x_?NumericQ, xprime_?NumericQ, s_?NumericQ, sprime_?NumericQ]:= Kernel  FNsprime[NN_?NumericQ, x_?NumericQ, s_?NumericQ, sprime_?NumericQ] :=NIntegrate[FNxprimesprime[NN, x, xprime, s, sprime], {xprime, -300/10^6, 300/10^6}]  FN[NN_?NumericQ,x_?NumericQ, s_?NumericQ] := NIntegrate[FNsprime[NN,x, s, sprime], {sprime,-10^-4, s}]  lst1 = Table[{ss, FN[0,0, ss], PrecisionGoal -> 5] // Quiet}, {ss, -10^-5, 10^-5, 10^-6}] ListPlot[lst1] 

FindRoot blocked by Jacobian in multi-valued function

Clear[t, r, z] c = 1; gam = 1.; tm = 16.; r[t_] = 2 c ArcTanh[Tan[t/(2 Sqrt[1 - Cot[gam]^2])]];  z[t_] = r[t] Cot[gam]; plr = PolarPlot[r[t], {t, 0, tm}, GridLines -> Automatic,    PlotStyle -> {Blue, Thick}] rad = Plot[r[t], {t, 0, tm}, GridLines -> Automatic,    PlotStyle -> {Red, Thick}] pp3 = ParametricPlot3D[{r[t] Cos[t], z[t], r[t] Sin[t]}, {t, -tm, tm},    PlotStyle -> {Magenta, Thick}] FindRoot[r[t] = 1.25, {t, 1.2345}] 

The last line FindRoot does not work due to Jacobian singularity. Can there be some work around? Thanks for help.

FindRoot: unstable solution

Writing:

a0 = 0;  A = ImplicitRegion[{-175 < x < 175, yc - 450 < y < yc, Cos[a] y - Sin[a] x > 0}, {x, y}];  B = {{-20, yc - 410, 16}, {0, yc - 410, 16}, {20, yc - 410, 16},         {-20, yc - 40, 16}, {20, yc - 40, 16}};  eqn = {Integrate[y, {x, y} \[Element] A] + 15 Pi/4 Total[B[[All, 2]] B[[All, 3]]^2],         Integrate[x y, {x, y} \[Element] A] + 15 Pi/4 Total[B[[All, 1]] B[[All, 2]] B[[All, 3]]^2],         Integrate[y^2, {x, y} \[Element] A] + 15 Pi/4 Total[B[[All, 2]]^2 B[[All, 3]]^2] - 100 10^6 yc/sc};  N[FindRoot[eqn == {0, 0, 0}, {{a, a0}, {yc, 225}, {sc, 15}}, WorkingPrecision -> 100], 10] 

I get:

{a -> 0, yc -> 113.2248453, sc -> 11.33863961}

which is what I expected (being an engineering problem, I know the solution).

Unfortunately, writing a0 = 10^-10 I get:

{a -> 1132.470809, yc -> 225.0000000, sc -> 12.19507508}

which is obviously incorrect. I suppose this is a problem of stability in the system of equations. How can it be solved in MMA? Thanks!

FindRoot returns the initial guess (Having large magnitude)

In my program (Big Enough, no need to see, pasted here : https://pastebin.com/HqnskRdn), I am trying to find the root of equation by FindRoot in range of order of $ 10^{12}$ or so. but FindRoot returns the initial guess,

Now if I change the coefficients of equation slightly then also, It returns the initial guess.

Ex- Initial Guess: $ 1.06154*10^{12}$ , Answer: $ 1.06154*10^{12}$

Any way out please ?

Instability of FindRoot solution

Using FindRoot on a smooth function of a parameter and a variable, the resulting solution should be a smooth function of the parameter, by the Implicit Function Theorem. However, Mathematica returns a very spiky unstable function. One way to fix the problem seems to avoid Newton’s method. Are there others, in particular that work with more than one variable?

MWE:

Clear[cdf, s0, s, sol] cdf[x_] := -2 (x - Sinh[1/2] +       Sinh[1/2 - x])/(-2 (1 - Sinh[1/2] + Sinh[1/2 - 1])) s0[a_] :=   s0[a] = x /. FindRoot[D[x*(cdf[a - x] - cdf[x]), x] == 0, {x, 0.5}] Plot[s0[a], {a, 0, 1}] (*A different root finding method does not cause instability.*) s[a_] := s[a] =    x /. FindRoot[D[x*(cdf[a - x] - cdf[x]), x] == 0, {x, 0.01, 0.99}] Plot[s[a], {a, 0, 1}] (*The problem is worse with two variables*) sol[c_] :=   sol[c] = {p1, p2} /.     FindRoot[{D[p1*(cdf[p2 - p1] - cdf[p1]), p1] == 0,   D[(p2 - c)*(1 - cdf[p2 - p1]), p2] == 0}, {{p1, 0.2}, {p2, 0.5}}] Plot[{sol[c][[1]], sol[c][[2]]}, {c, 0, 1}] (*Using non-Newton methods of root finding does not help with two variables.*) sol[c_] :=   sol[c] = {p1, p2} /.     FindRoot[{D[p1*(cdf[p2 - p1] - cdf[p1]), p1] == 0,       D[(p2 - c)*(1 - cdf[p2 - p1]), p2] == 0}, {{p1, 0.1, 0.8}, {p2,        0.2, 0.9}}] Plot[{sol[c][[1]], sol[c][[2]]}, {c, 0, 1}] 

FindRoot with NIntegrate: either recursion depth exceeded or numerical integration converging too slowly

Trying to numerically solve an equation involving an integral, either the error Recursion depth exceeded appears (when AccuracyGoal->10 or smaller) or the error Numerical integration converging too slowly appears. In the MWE below, the functions involved are smooth, bounded and the solution for the equation is 1 (as confirmed by Plot[{hdif[z], 0}, {z, smin, 2}]), so there should not be difficulty numerically integrating or solving.

How to find a solution to similar equations which cannot be solved by hand analytically?

MWE

Clear[smin, smax, h, hdif] smin = -1; h[z_] := Exp[-z/2] hdif[zmax_?NumericQ] :=   NIntegrate[(Exp[z] - 1)*h[z], {z, smin, zmax}, AccuracyGoal -> 5] smax = FindRoot[{hdif[smax] == 0}, {smax, 0, smin, Max[-smin, 1000]}] 

Other solutions recommend increasing AccuracyGoal or WorkingPrecision (both of which I tried), cancelling periodic oscillations, integrating piecewise around points where the derivative is discontinuous (these do not apply to the function in question). Replacing NIntegrate with Integrate works for h[z_] := Exp[-z/2], but not for functions without a closed form integral.