Mathematica Understanding Errors in Mathematica: How to Locate and Solve Them

Click For Summary
In Mathematica, errors can often be traced back to specific input lines, as indicated by error messages. Debugging strategies include breaking down complex computations into smaller parts and using Print[] statements to check variable values. More advanced tools like Trace[] and Monitor[] can help track intermediate expressions and variable values during calculations. In a specific case, an error arose from attempting to compute the factorial of a negative number, leading to an indeterminate form when multiplied by zero. It is advised to ensure that variables like x are constrained to valid ranges to avoid such issues.
EngWiPy
Messages
1,361
Reaction score
61
Hello,

Is there any way to know where exactly are the errors occurred in Mathematica? For example, I have the following error in my long code:

Code:
During evaluation of In[359]:= \[Infinity]::indet: Indeterminate \
expression 0 ComplexInfinity encountered. >>

During evaluation of In[359]:= \[Infinity]::indet: Indeterminate \
expression 0 ComplexInfinity encountered. >>

During evaluation of In[359]:= \[Infinity]::indet: Indeterminate \
expression 0 ComplexInfinity encountered. >>

During evaluation of In[359]:= General::stop: Further output of \
\[Infinity]::indet will be suppressed during this calculation. >>

Out[381]= $Aborted

but I don't know where exactly the error is.

Thanks in advance.
 
Physics news on Phys.org
It tells you that the errors were generated by input In[359]. You should be able to find this in the notebook.

- Warren
 
Is there any way to know where exactly are the errors occurred in Mathematica?

The short answer is yes, there are a large number of tools and strategies for debugging Mathematica code.

The most simple method, which is also the one I use 99% of the time and recommend most highly as a Mathematica expert, is to take advantage of the interpreted nature of the language by building a larger computation out of smaller pieces that you have already debugged.

Another primitive but effective debugging strategy is to include Print[] statements in the middle of your calculation, to see that your variables have the values you expect.

Some more formal debugging tools are Trace[] and Monitor[]. Trace[expr] will output a list of the intermediate expressions used in the computation of expr. Monitor[n] will output a list of the values taken by the variable n.

If you want, post your code and I will be happy to find and fix the error and/or suggest some tips. Nowadays I use Mathematica all day long without generating any errors, it's truly beautiful --- but I remember the old days when even the simplest expressions would take me hours of debugging.
 
Civilized said:
...
If you want, post your code and I will be happy to find and fix the error and/or suggest some tips. Nowadays I use Mathematica all day long without generating any errors, it's truly beautiful --- but I remember the old days when even the simplest expressions would take me hours of debugging.

Actually, I am new on Mathematica, and as you said about yours old days, it takes me a considerable amount of time to find a small error, if I found it. Anyway, after some effort, I narrowed the piece of code that contains the error in the following:

Code:
f1[ze_, al_] := 1/2*(ze - s - Sqrt[(ze - s)^2 - 4*al^2]);
f2[ze_, al_] := 1/2*(ze - s + Sqrt[(ze - s)^2 - 4*al^2]);
G[b_, f1_, f2_] := (
  HypergeometricU[1, 1 - b, f1] - HypergeometricU[1, 1 - b, f2])/(
  2*(f2 - f1));
Xe[ze_, al_, E_, x_] := ((2*(x)!)/al^x \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(z = 0\), \(x\)]Binomial[x, 
        z]*\((E*D[G[x, f1[ze, al], f2[ze, al]], {s, E - 1 + z}] - 
         ze*D[G[x, f1[ze, al], f2[ze, al]], {s, E + z}])\)\)) - ((
     2*al*(x - 1)!)/al^(x - 1) \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(z = 0\), \(x - 1\)]Binomial[x - 1, 
        z]*\((2*D[G[x - 1, f1[ze, al], f2[ze, al]], {s, E + z + 1}] + 
         D[G[x - 1, f1[ze, al], f2[ze, al]], {s, E + z}])\)\));

In[12]:= Xe[0.05, 0.1, 0, 0]

During evaluation of In[12]:= \[Infinity]::indet: Indeterminate \
expression 0 ComplexInfinity encountered. >>

Out[12]= Indeterminate

Please, copy the code from here, and paste it at your Mathematica editor, because some symbols, powers and square roots are translated into other forms.

Thank you for your replying Civilized and chroot.

Best regards
 
In your function Xe, in the second term the first part:
( 2*al*(x - 1)!)/al^(x - 1)
evaluates to ComplexInfinity

Meanwhile the second part:
\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(z = 0\), \(x - 1\)]\(Binomial[
x - 1, z]*\((2*
D[G[x - 1, f1[ze, al], f2[ze, al]], {s, E + z + 1}] +
D[G[x - 1, f1[ze, al], f2[ze, al]], {s, E + z}])\)\)\)
evaluates to 0.

Infinity times 0 is undefined, so you get the error you are having.

By the way, the reason the first part evaluates to ComplexInfinity is that your argument x is 0 and so (x-1)! means that you are taking the factorial of a negative number. Probably you don't want to do that, so most likely x should always be greater than or equal to 1.
 
DaleSpam said:
In your function Xe, in the second term the first part:
( 2*al*(x - 1)!)/al^(x - 1)
evaluates to ComplexInfinity

Meanwhile the second part:
\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(z = 0\), \(x - 1\)]\(Binomial[
x - 1, z]*\((2*
D[G[x - 1, f1[ze, al], f2[ze, al]], {s, E + z + 1}] +
D[G[x - 1, f1[ze, al], f2[ze, al]], {s, E + z}])\)\)\)
evaluates to 0.

Infinity times 0 is undefined, so you get the error you are having.

By the way, the reason the first part evaluates to ComplexInfinity is that your argument x is 0 and so (x-1)! means that you are taking the factorial of a negative number. Probably you don't want to do that, so most likely x should always be greater than or equal to 1.

Really? but when you write at a Mathemtica editor Factorial[-ve] it dosen't give you ComplexInfinity, but 0 as I know,so , I left it as is! Although your observation may be in its place about that x\ge 1.

Thank you very much DaleSpam, I will try this and I hope it will solve the problem.
Best regards
 
because Factorial[-ve] IS defined for non-integer values of "ve".

Plot[n!, {n, -2.5, 3}]But what I don't like is that even if you make the assumption that ve belongs to integers, it won't take it as complex infinity. BUT the gamma will:

$Assumptions =
ve \[Element] Reals && ve > 1 && ve \[Element] Integers;
Refine[Gamma[ve + 1]]
Refine[Gamma[-ve + 1]]
Refine[Factorial[ve]]
Refine[Factorial[-ve]]
OUTPUT:
Gamma[1 + ve]
ComplexInfinity
ve!
(-ve)!Even though its the definition...
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 13 ·
Replies
13
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K