Understanding Errors in Mathematica: How to Locate and Solve Them

  • Context: Mathematica 
  • Thread starter Thread starter EngWiPy
  • Start date Start date
  • Tags Tags
    Errors Mathematica
Click For Summary

Discussion Overview

The discussion revolves around identifying and resolving errors in Mathematica code, particularly focusing on a specific error message related to indeterminate expressions. Participants share debugging strategies, code snippets, and insights into the behavior of certain functions within Mathematica.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant inquires about locating errors in Mathematica, presenting a specific error message encountered during code execution.
  • Another participant suggests that the error is linked to the input cell In[359] and encourages checking the notebook for the source of the error.
  • A Mathematica expert proposes various debugging strategies, including breaking down computations into smaller parts and using Print[] statements to track variable values.
  • Participants discuss the specific code causing the error, particularly focusing on the function Xe and its evaluation leading to ComplexInfinity.
  • One participant explains that the error arises from attempting to compute the factorial of a negative number, suggesting that the variable x should be constrained to values greater than or equal to 1.
  • Another participant challenges the explanation regarding the behavior of the Factorial function for negative values, noting that it does not return ComplexInfinity in all cases.
  • Further discussion includes the implications of assumptions on the evaluation of the Gamma function and its relationship to factorials.

Areas of Agreement / Disagreement

Participants express differing views on the behavior of the Factorial function for negative inputs and the resulting implications for the error encountered. There is no consensus on the best approach to resolve the issue, and multiple perspectives on debugging strategies are presented.

Contextual Notes

Participants reference specific Mathematica functions and behaviors, which may depend on the context of their use and the assumptions made about variable types. The discussion highlights the complexity of debugging in Mathematica and the nuances of mathematical definitions in programming.

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 doesn'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
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 13 ·
Replies
13
Views
8K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K