0
$\begingroup$

I have a function intSL[a,zl,zh] where I want to compute its derivative with respect to zl given some values of a and zh. I also want to plot it wrt to zl with the range 0 < zl < 1.

However, there are some zl where my function displays an error message and produces a complex value. When I compare results that are complex valued to the result that are not, it seems like their corresponding real parts has a trend and so I think I could just eliminate the imaginary part (I think???), but I do not know how to do it.

Here is my code,

d = 3;
torootL[a_?NumericQ, t_?NumericQ, zl_?NumericQ, zh_?NumericQ] := a - NIntegrate[(zl y^d)/Sqrt[(1 - (zl/zh)^(d + 1) y^(d + 1)) (1 + t^2 (1 - (zl/zh)^(d + 1))^-1 - y^(2 d))], {y, 0, 1}]
tz[a_?NumericQ, zl_?NumericQ, zh_?NumericQ] := t /. FindRoot[torootL[a, t, zl, zh], {t, 1, -1000, 10000}]
intSL[a_?NumericQ, zl_?NumericQ, zh_?NumericQ] := NIntegrate[With[{b = zl/zh}, (((-1)/(d - 1)) (zl^(2 d) (1 + tz[a, zl, zh]^2 (1 - (zl/zh)^(d + 1))^(-1)))^(-1) zl^(2 d)) x^d ((1 - (b x)^(d + 1))/(1 - (zl^(2 d) (1 + tz[a, zl, zh]^2 (1 - (zl/zh)^(d + 1))^(-1)))^(-1) (zl x)^(2 d)))^(1/2) - ((b^(d + 1) (d + 1))/(2 (d - 1))) x ((1 - (zl^(2 d) (1 + tz[a, zl, zh]^2 (1 - (zl/zh)^(d + 1))^(-1)))^(-1) (zl x)^(2 d))/(1 - (b x)^(d + 1)))^(1/2) + (b^(d + 1)x)/((1 - (b x)^(d + 1)) (1 - (zl^(2 d) (1 + tz[a, zl, zh]^2 (1 - (zl/zh)^(d + 1))^(-1)))^(-1) (zl x)^(2 d)))^(1/2)], {x, 0, 0.98, 0.99, 1}, MaxRecursion -> 20, PrecisionGoal -> 6]

Here is an example of the evaluation of the derivative,

intSLL[zl_] := Evaluate[D[intSL[0.01, zl, 1], zl]]

In[58]:= intSLL[0.3]

During evaluation of In[58]:= FindRoot::jsing: Encountered a singular Jacobian at the point {t} = {-5.99038*10^-9}. Try perturbing the initial point(s).

During evaluation of In[58]:= FindRoot::jsing: Encountered a singular Jacobian at the point {t} = {-5.99038*10^-9}. Try perturbing the initial point(s).

During evaluation of In[58]:= FindRoot::jsing: Encountered a singular Jacobian at the point {t} = {-5.99038*10^-9}. Try perturbing the initial point(s).

During evaluation of In[58]:= General::stop: Further output of FindRoot::jsing will be suppressed during this calculation.

During evaluation of In[58]:= FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances.

During evaluation of In[58]:= FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances.

During evaluation of In[58]:= FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances.

During evaluation of In[58]:= General::stop: Further output of FindRoot::lstol will be suppressed during this calculation.

During evaluation of In[58]:= FindRoot::reged: The point {10000.} is at the edge of the search region {-1000.,10000.} in coordinate 1 and the computed search direction points outside the region.

During evaluation of In[58]:= FindRoot::reged: The point {10000.} is at the edge of the search region {-1000.,10000.} in coordinate 1 and the computed search direction points outside the region.

During evaluation of In[58]:= FindRoot::reged: The point {10000.} is at the edge of the search region {-1000.,10000.} in coordinate 1 and the computed search direction points outside the region.

During evaluation of In[58]:= General::stop: Further output of FindRoot::reged will be suppressed during this calculation.

Out[58]= 0.0149288

In[57]:= intSLL[0.4]

During evaluation of In[57]:= FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances.

During evaluation of In[57]:= FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances.

During evaluation of In[57]:= FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances.

During evaluation of In[57]:= General::stop: Further output of FindRoot::lstol will be suppressed during this calculation.

During evaluation of In[57]:= FindRoot::jsing: Encountered a singular Jacobian at the point {t} = {-6.30218*10^-9}. Try perturbing the initial point(s).

During evaluation of In[57]:= FindRoot::jsing: Encountered a singular Jacobian at the point {t} = {-6.30218*10^-9}. Try perturbing the initial point(s).

During evaluation of In[57]:= FindRoot::jsing: Encountered a singular Jacobian at the point {t} = {-6.30218*10^-9}. Try perturbing the initial point(s).

During evaluation of In[57]:= General::stop: Further output of FindRoot::jsing will be suppressed during this calculation.

During evaluation of In[57]:= FindRoot::reged: The point {10000.} is at the edge of the search region {-1000.,10000.} in coordinate 1 and the computed search direction points outside the region.

During evaluation of In[57]:= FindRoot::reged: The point {10000.} is at the edge of the search region {-1000.,10000.} in coordinate 1 and the computed search direction points outside the region.

During evaluation of In[57]:= FindRoot::reged: The point {10000.} is at the edge of the search region {-1000.,10000.} in coordinate 1 and the computed search direction points outside the region.

During evaluation of In[57]:= General::stop: Further output of FindRoot::reged will be suppressed during this calculation.

Out[57]= 0.00650273

In[55]:= intSLL[0.5]

Out[55]= 0.00353467

In[56]:= intSLL[0.6]

During evaluation of In[56]:= FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances.

During evaluation of In[56]:= FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances.

During evaluation of In[56]:= FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances.

During evaluation of In[56]:= General::stop: Further output of FindRoot::lstol will be suppressed during this calculation.

During evaluation of In[56]:= NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in y near {y} = {0.984824086012257}. NIntegrate obtained 0.0293841 -0.119444 I and 0.0017267736029825806` for the integral and error estimates.

During evaluation of In[56]:= NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in y near {y} = {0.984824086012257}. NIntegrate obtained 0.0293841 -0.119444 I and 0.0017267736029825806` for the integral and error estimates.

During evaluation of In[56]:= NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in y near {y} = {0.984824086012257}. NIntegrate obtained 0.0293841 -0.119444 I and 0.001726773577114529` for the integral and error estimates.

During evaluation of In[56]:= General::stop: Further output of NIntegrate::ncvb will be suppressed during this calculation.

During evaluation of In[56]:= FindRoot::reged: The point {1.} is at the edge of the search region {-1000.,10000.} in coordinate 1 and the computed search direction points outside the region.

During evaluation of In[56]:= FindRoot::reged: The point {1.} is at the edge of the search region {-1000.,10000.} in coordinate 1 and the computed search direction points outside the region.

During evaluation of In[56]:= FindRoot::reged: The point {1.} is at the edge of the search region {-1000.,10000.} in coordinate 1 and the computed search direction points outside the region.

During evaluation of In[56]:= General::stop: Further output of FindRoot::reged will be suppressed during this calculation.

During evaluation of In[56]:= NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small.

During evaluation of In[56]:= NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small.

During evaluation of In[56]:= NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small.

During evaluation of In[56]:= General::stop: Further output of NIntegrate::slwcon will be suppressed during this calculation.

Out[56]= 0.00225041 + 8.62966*10^-11 I

In[59]:= intSLL[0.7]

During evaluation of In[59]:= NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small.

During evaluation of In[59]:= NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in y near {y} = {0.99762205831973036103399454788132061366923153400421142578125000000}. NIntegrate obtained 0.00330503 -0.0437457 I and 0.0008224862881109875` for the integral and error estimates.

During evaluation of In[59]:= NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small.

During evaluation of In[59]:= NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in y near {y} = {0.99762205831973036103399454788132061366923153400421142578125000000}. NIntegrate obtained 0.00330503 -0.0437457 I and 0.0008224862881109875` for the integral and error estimates.

During evaluation of In[59]:= NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small.

During evaluation of In[59]:= General::stop: Further output of NIntegrate::slwcon will be suppressed during this calculation.

During evaluation of In[59]:= NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in y near {y} = {0.99762205831973036103399454788132061366923153400421142578125000000}. NIntegrate obtained 0.00330503 -0.0437457 I and 0.0008224862758539473` for the integral and error estimates.

During evaluation of In[59]:= General::stop: Further output of NIntegrate::ncvb will be suppressed during this calculation.

During evaluation of In[59]:= FindRoot::reged: The point {1.} is at the edge of the search region {-1000.,10000.} in coordinate 1 and the computed search direction points outside the region.

During evaluation of In[59]:= FindRoot::reged: The point {1.} is at the edge of the search region {-1000.,10000.} in coordinate 1 and the computed search direction points outside the region.

During evaluation of In[59]:= FindRoot::reged: The point {1.} is at the edge of the search region {-1000.,10000.} in coordinate 1 and the computed search direction points outside the region.

During evaluation of In[59]:= General::stop: Further output of FindRoot::reged will be suppressed during this calculation.

Out[59]= 0.0016139 + 2.5821*10^-6 I

To summarize the sample result, intSLL[zl] produces the values for the corresponding zl

0.0149288                         zl=0.3   (with error)
0.00650273                        zl=0.4   (with error)
0.00353467                        zl=0.5
0.00225041 + 8.62966*10^-11 I     zl=0.6   (with error)
0.0016139 + 2.5821*10^-6 I        zl=0.7   (with error)

You can see the trend of the values of the real part.

$\endgroup$

1 Answer 1

1
$\begingroup$

Use Derivative instead of D:

{#, intSL[.01, #, 1], Derivative[0, 1, 0][intSL][0.01, # , 1]} &[.3]
(*{0.3, -0.00218296, 0.0149288}*)

{#, intSL[.01, #, 1], Derivative[0, 1, 0][intSL][0.01, # , 1]} &[.6];
% //Chop
(*{0.6, -0.000430944, 0.00225041}*)

The evaluation takes some time and shows several messages "FindRoot::jsing: Encountered a singular Jacobian at the point {t} = {-5.99038*10^-9}. Try perturbing the initial point(s)." but works in principle.

$\endgroup$
7
  • $\begingroup$ I tried your code for zl=0.6, it still displays the imaginary part. Also how does it work when you enclose those functions in a list {} then evaluate at .3 using &? $\endgroup$
    – mathemania
    Commented Nov 23, 2020 at 8:31
  • 2
    $\begingroup$ For zl=.6 Mathematica v12 evaluates {0.6, -0.000430944, 0.00225041 + 8.62966*10^-11 I}, the imaginary part might be removed with Chop $\endgroup$ Commented Nov 23, 2020 at 8:36
  • $\begingroup$ The code I used, intSLL[zl_] := Evaluate[Derivative[0, 1, 0][intSL][0.01, zl, 1]]. For 0.6 it worked In[81]:= intSLL[0.6] // Chop // Quiet Out[81]= 0.00225041 but for 0.7 it did not, In[84]:= intSLL[0.7] // Chop // Quiet Out[84]= 0.0016139 + 2.58281*10^-6 I $\endgroup$
    – mathemania
    Commented Nov 23, 2020 at 9:18
  • 1
    $\begingroup$ Usually Chop works with a tolerance of 10^-10, that's why imaginary part ` 2.58281*10^-6 I ` still apperars. Try //Chop[#,10^-6]& $\endgroup$ Commented Nov 23, 2020 at 10:17
  • 1
    $\begingroup$ Chop[intSLL[0.7], 10^-5] and intSLL[0.7] //Chop[#,10^-5]& are the same! $\endgroup$ Commented Nov 24, 2020 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.