Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Error in Mathematica

  1. Apr 18, 2016 #1

    I have the following code in Mathematica, and it gives the following error:

    Code (Text):
    NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 12 recursive bisections in x near {x} = {0.000156769}. NIntegrate obtained 0.21447008480474927` and 5.637666621985554`*^-13 for the integral and error estimates. >>
    How to solve this error?

    Code (Text):
    yp = 10^(5/10);
    GSS = 10;

    For[yQdB = -10, yQdB <= 10, yQdB++;
    yQ = 10^(yQdB/10);
    A1 = NIntegrate[
       1/x^2*Exp[(GSS*(x + GSS*yQ))/(x*yp) - x]*
        ExpIntegralE[1, (GSS*(x + GSS*yQ))/(x*yp)], {x, LL, Infinity},
       PrecisionGoal -> 12, MaxRecursion -> 12];
  2. jcsd
  3. Apr 18, 2016 #2


    User Avatar
    2017 Award

    Staff: Mentor

    You have a loop, so a natural step to narrow down the error would be to test individual iterations of the loop. Does every value of yQdB produce the problem? Did you try a lower precision goal or a deeper recursion? Note that the precision is close to the target.
  4. Apr 18, 2016 #3
    No actually it's not for every value of yQdB. What do you mean by "Note that the precision is close to the target."?
  5. Apr 18, 2016 #4


    User Avatar
    2017 Award

    Staff: Mentor

    That is a relative uncertainty of about 3*10-12.
  6. Apr 18, 2016 #5
    So, do I need to increase the precision?
  7. Apr 18, 2016 #6
    Another thing, which is that the lower limit of the integral is theoretically zero. However, since the program doesn't compute it by saying that the integral has evaluated to overflow, indeterminate, or infinity, I replaced it but ##LL=10^{-5}##. However, when I compare the results with Monte-Carlo simulations (the above formula is the numerical result), there is some discrepancy. How to make the numerical evaluation as accurate as possible?
  8. Apr 18, 2016 #7


    User Avatar
    2017 Award

    Staff: Mentor

    The opposite. Mathematica is unable to require the precision you ask for with the number of iterations you allow. Allow more iterations or reduce the precision you ask for and it should work.
    Why do you think the problem is with Mathematica?
  9. Apr 18, 2016 #8
    Fair point. I'm not sure if the problem is with Mathematica. I posed a question in the calculus forums to double check if there is something fundamentally wrong with the math, but I haven't gotten a conclusive answer.
  10. Apr 18, 2016 #9


    User Avatar
    2017 Award

    Staff: Mentor

    Well, we can't tell. You wrote down an integral. Mathematica evaluates it. Is this the right integral you want to calculate? No idea.
  11. Apr 18, 2016 #10
    Also it helps to give the integral you are trying to evaluate in latex with proper symbols.
    That makes it easier to check for typos in the code (don't ask me how many times I made that kind of mistake :S) or whether there is some mathematical reason for the error message.
  12. Apr 18, 2016 #11
    I think the mathematical derivations were accurate. I'm not sure about the limits though. The integral is the following


    More details are found here.
  13. Apr 18, 2016 #12
    You mentioned in post #3 that the error doesn't happen for every yQdB, could you narrow it down some more?
    With these kinds of expressions you sometimes overlook undefined cases.

    I also think your expression in mathematica has ##A=A(x)## meaning you have an extra subtlety.
    Last edited: Apr 18, 2016
  14. Apr 18, 2016 #13
    Really? Do you mean I should use square brackets all the time in Mathematica?
  15. Apr 19, 2016 #14
    I have rewritten the expression in your code to make it more clear what happens.
    \frac{1}{x^2}\exp\left[ \frac{G_{SS}\left(x+G_{SS} \cdot y_Q\right)}{xy_p}-x\right]
    E_1\left[\frac{G_{SS}\left(x+G_{SS}y_Q\right)}{xy_p}\right]=\frac{1}{x^2}\exp\left[ \frac{G_{SS}}{y_p}+\frac{G_{SS}^2 \cdot y_Q}{xy_p}-x\right]E_1\left[\frac{G_{SS}}{y_p}+\frac{G_{SS}^2y_Q}{y_px}\right]$$

    If you now define ##A=\frac{G_{SS}^2y_Q}{y_p}##, ##B=\frac{G_{SS}}{y_p}## and ##C=\frac{G_{SS}}{y_p}## this results in

    \frac{1}{x^2}\exp\left[ \frac{G_{SS}\left(x+G_{SS} \cdot y_Q\right)}{xy_p}-x\right]
    E_1\left[\frac{G_{SS}\left(x+G_{SS}y_Q\right)}{xy_p}\right] = \frac{1}{x^2}\exp\left[C +\frac{A}{y_p}-x\right]E_1\left[\frac{G_{SS}}{y_p}+\frac{A}{x}\right]

    So either you have different A's as you can get the right form in the exponential by setting ##A^\prime(x)=\frac{G_{SS}\left(x+G_{SS} \cdot y_Q\right)}{y_p}## leading to


    So you have to check that if really want this or the integral in post #11.

    I used square brackets and parentheses for clarity but these expressions are not code, I don't like reading the code directly most of the time as it can obscure things which seem obvious in retrospect.

    Edit 2;
    As Tom pointed out I made a mistake, it's now fixed
    Last edited: Apr 19, 2016
  16. Apr 19, 2016 #15


    User Avatar
    Science Advisor

    1x2exp&#x2061;[GSS(x+GSS&#x22C5;yQ)xyp&#x2212;x]E1[GSS(x+GSSyQ)xyp]=1x2exp&#x2061;[C+Ayp&#x2212;x]E1[GSSyp+Ax]" style="font-size: 106%; position: relative;" tabindex="0" class="mjx-chtml MathJax_CHTML" id="MathJax-Element-8-Frame">1x2exp[C+Ayp−x]E1[GSSyp+Ax]

    As you you Defined A, above, should the exp term in the rewrite have A/x rather than A/yp ?

    EDIT: well, that didn't copy thru as intended!
  17. Apr 19, 2016 #16
    You're absolutely right, thanks for pointing that out.

    Best way to copy formulas is to use quote or reply since that copies the LaTeX codes.
  18. Apr 21, 2016 #17
    Yes, you are right. But if you saw the Mathematica code, it's detailed. I used the constants A and B in the mathematical integral for convenience.
  19. Apr 21, 2016 #18
    So my question is, does the variable A in the mathematical integral really represent a constant?

    Because that is not reflected in your mathematica code as I've shown.

    Is there a unique value of yQdB for which the evaluation fails? Or are there several values that cause trouble?

    Copying equations works best through reply or quote. They are typeset using LaTeX through MathJax.
    You seem to have copied the html-code representing the equation which is quite odd.
  20. Apr 21, 2016 #19
    In the Mathematica code there is no A. As I said, I used A and B for mathematical convenience, and your expression is more accurate mathematically. But again Mathematica code is still correct as I wrote it detailed; I didn't write it in terms of A and B.

    The problem occurs at almost all values of yQdB where the lower limit of the integral is 0. But when I use a lower limit that is close to zero (like in the following code), I will have the error occurring at certain values.

    Code (Text):
    yp = 10^(5/10);
    GSS = 1;
    Cons = 10^-7;

    For[yQdB = -10, yQdB <= 15, yQdB++;
    yQ = 10^(yQdB/10);
    A1 = 0.5*((GSS^2)*yQ )/yp*
        1/x^2*Exp[(GSS*(x + GSS*yQ))/(x*yp) - x]*
         ExpIntegralE[1, (GSS*(x + GSS*yQ))/(x*yp)], {x, Cons, Infinity},
        PrecisionGoal -> 5, MaxRecursion -> 12];
    The above code is working fine. But when I change GSS to 100, the error appears again, but at certain values. I'm not sure why?
  21. Apr 21, 2016 #20


    Staff: Mentor

    Does Mathematica evaluate this as the square root of 10? Many programming languages would evaluate 5/10 as 0, using integer division rather than floating point division, thereby setting yp to 1.

    Out of curiosity, why are you raising 10 to the 5/10 power?
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted