# Error in Mathematica

Tags:
1. Apr 18, 2016

### S_David

Hello,

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);
Print[yQ];
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. Apr 18, 2016

### 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.

3. Apr 18, 2016

### S_David

No actually it's not for every value of yQdB. What do you mean by "Note that the precision is close to the target."?

4. Apr 18, 2016

### Staff: Mentor

That is a relative uncertainty of about 3*10-12.

5. Apr 18, 2016

### S_David

So, do I need to increase the precision?

6. Apr 18, 2016

### S_David

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?

7. Apr 18, 2016

### 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?

8. Apr 18, 2016

### S_David

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.

9. Apr 18, 2016

### 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.

10. Apr 18, 2016

### JorisL

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.

11. Apr 18, 2016

### S_David

I think the mathematical derivations were accurate. I'm not sure about the limits though. The integral is the following

$$\int_0^{\infty}\frac{1}{x^2}\exp\left(\frac{A}{x}-x\right)E_1\left(B+\frac{A}{x}\right)\,dx$$

More details are found here.

12. Apr 18, 2016

### JorisL

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.

Edit;
I also think your expression in mathematica has $A=A(x)$ meaning you have an extra subtlety.

Last edited: Apr 18, 2016
13. Apr 18, 2016

### S_David

Really? Do you mean I should use square brackets all the time in Mathematica?

14. Apr 19, 2016

### JorisL

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

$$\frac{1}{x^2}\exp\left[\frac{A^\prime(x)}{x}-x\right]E_1\left[\frac{G_{SS}}{y_p}+\frac{A}{x}\right]$$

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

Edit;
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
15. Apr 19, 2016

### Tom.G

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!

16. Apr 19, 2016

### JorisL

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.

17. Apr 21, 2016

### S_David

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.

18. Apr 21, 2016

### JorisL

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?

@Tom.G
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.

19. Apr 21, 2016

### S_David

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*
NIntegrate[
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];
Print[A1]
]
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?

20. Apr 21, 2016

### 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?