# Error in Mathematica

• Mathematica

## Main Question or Discussion Point

Hello,

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

Code:
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:
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];
]

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
mfb
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.

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

mfb
Mentor
NIntegrate obtained 0.21447008480474927 and 5.637666621985554*^-13
That is a relative uncertainty of about 3*10-12.

So, do I need to increase the precision?

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?

mfb
Mentor
So, do I need to increase the precision?
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.
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?
Why do you think the problem is with Mathematica?

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.

mfb
Mentor
I posed a question in the calculus forums to double check if there is something fundamentally wrong with the math
Well, we can't tell. You wrote down an integral. Mathematica evaluates it. Is this the right integral you want to calculate? No idea.

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.

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.

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:
Really? Do you mean I should use square brackets all the time in Mathematica?

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:
Tom.G
I have rewritten the expression...
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!

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.

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

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.

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

Mark44
Mentor
yp = 10^(5/10);
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?

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.
Even if you don't have it explicitly, I showed in post #14 that you're mathematica code doesn't compute an integral of the form you gave.

Try putting this in your loop

Code:
Print["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, ")]"]
It will show you which integral is being evaluated (without the domain and options as they don't change).
That way you'll know what the exact integral is that doesn't work.

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?
Because usually we choose the value of yp in dB, while in computation we use it in linear scale. So, yp in dB is 5 while in linear scale it's 10^(5/10).

Even if you don't have it explicitly, I showed in post #14 that you're mathematica code doesn't compute an integral of the form you gave.

Try putting this in your loop

Code:
Print["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, ")]"]
It will show you which integral is being evaluated (without the domain and options as they don't change).
That way you'll know what the exact integral is that doesn't work.
In mathematical form, the integrad is

$$\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]$$

In Mathematica it's

Code:
1/x^2*Exp[(GSS*(x + GSS*yQ))/(x*yp) - x]*
ExpIntegralE[1, (GSS*(x + GSS*yQ))/(x*yp)]
They both look the same to me!! Am I missing something here?

I copied your line, but didn't know how to use, or what I'm looking for. Last edited:
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.
If you compare the two you see that in the code $B=0$ while $A$ explicitly contains $x$ (it's linear in x).
Meaning it isn't the same integral. I'm sure your code is right but the mathematical discussion in the other thread isn't really relevant.

<snip>
I copied your line, but didn't know how to use, or what I'm looking for. You put it inside your For loop above the NIntegrate-function.
It will print your command without executing it i.e. show you what the integrand looks like.

But again you referred to the integral which was written in terms of A and B. I just wrote it this way here to make it easier for the reader to read. The integral as it's in post #23 is the same as the integral in Mathematica. This is the integral in my calculations. Not the one I wrote here which you just quoted.

Your line gave me this for the first value of yQdB

Code:
A1 = 199.054*NIntegrate[  1/x^2*Exp[ ( 100*(x +10 10^(1/10)))/(x*Sqrt) - x]* ExpIntegralE[1, (100*(x + 10 10^(1/10)))/(x*Sqrt)]