Understanding Errors in Mathematica: How to Locate and Solve Them

In summary: Maybe you want to use the Gamma function instead of Factorial[]Thanks for providing the code snippet. In summary, the error in the code is due to taking the factorial of a negative number, which evaluates to ComplexInfinity. The solution may be to use the Gamma function instead of Factorial or to ensure that the argument for Factorial is always positive.
  • #1
EngWiPy
1,368
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
  • #2
It tells you that the errors were generated by input In[359]. You should be able to find this in the notebook.

- Warren
 
  • #3
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.
 
  • #4
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
 
  • #5
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.
 
  • #6
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 [tex]x\ge 1[/tex].

Thank you very much DaleSpam, I will try this and I hope it will solve the problem.
Best regards
 
  • #7
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...
 

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

What is Mathematica?

Mathematica is a computational software program used for mathematical and scientific calculations. It provides a user-friendly interface for solving complex mathematical problems and creating visualizations.

Why do I encounter errors in Mathematica?

Errors in Mathematica can occur due to a variety of reasons, including incorrect syntax, mismatched data types, or insufficient memory. It is important to carefully check the code and data inputs to identify the source of the error.

How do I troubleshoot errors in Mathematica?

To troubleshoot errors in Mathematica, you can use the built-in debugger tool, which allows you to step through your code and identify the specific line where the error occurs. Additionally, you can refer to the Mathematica documentation or seek help from online forums and communities.

Can I prevent errors in Mathematica?

While it is not possible to completely prevent errors in Mathematica, you can minimize their occurrence by writing well-structured and error-free code. This includes using proper syntax, checking for data type compatibility, and using appropriate debugging techniques.

What resources are available for solving errors in Mathematica?

There are various resources available for solving errors in Mathematica, including the official Mathematica documentation, online forums and communities, and tutorials and guides. You can also seek help from experienced Mathematica users or consult with a professional Mathematica consultant.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
297
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
34
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top