Message Boards Message Boards

6 Replies
6 Total Likes
View groups...
Share this post:

Reaching an error of 1/0 when trying to evaluate an expression

Posted 10 years ago

Hi there,

When trying to calculate the absolute value of a complex expression, I am reaching errors for 1/0 being an infinite expression. I know where in the code this error comes from but there are too many points for me to write each one individually. I am wanting to calculate the absolute value so that I can find the minimum points in an intensity distribution and plot it as a contour map. The function I run takes the input and evaluates it at all x,y,z points in the area I'm interested in. The code for the potential is;

uPWPhase[k_, d0_, l_, r_] := Module[{nk, u0, n0, n90, kLength, phi},
  kLength = Sqrt[k.k]; 
  nk = k/kLength;
  u0 = d0 - d0.nk nk;
  n0 = u0/Sqrt[
  n90 = Cross[n0, nk]; 
  phi = ArcTan[r.n90/r.n0];
  Exp[I (k.r - l*phi)] 

uHP[x_, y_, z_] := Sum[ uPWPhase[
   k[[j]] (*k vectors*),
   k[[nn[[j, 1]]]] - k[[j]] (*d0 vector*),
   {x, y, z} (*r vector*)],
  {j, 1, Length[k]}] 

vHP = uHP[x, y, z]

and gives a sum series of the form (I am only giving 1 term in the series for ease of reading, but there are 8 terms all of a similar form);

E^(I (-(x/2) - y/2 + z/2 - 3 ArcTan[(-(x/Sqrt[2]) + y/Sqrt[2])/(-(x/Sqrt[6]) - y/Sqrt[6] -Sqrt[2/3] z)]))

As you can see, it is the complex exponential of a location and an angle. The 1/0 error comes from the evaluation of the ArcTan for certain values of x,y,z. Is there a way to get Mathematica to evaluate this, or at least approximate it?

Thanks in advance

POSTED BY: Andrew Ross
6 Replies

When defining functions, you mostly want to use SetDelayed (:=) and not Set (=). The difference between these two isn't something often see in other languages, but SetDelayed (:=) behaves more like you expect. When you use Set (=) the right hand side is evaluated first.

This is probably an abuse of error checking, but we can use Check to just return 0 whenever there's an error message.

safeExpression[x_?NumericQ, y_?NumericQ, z_?NumericQ] := Check[Abs[vHP] , 0]

Remember to include the arguments to the function when calling it. I've prepended "Quiet@" to stop the messages from appearing.

intensityTable = Table[If[ x^2 + y^2 + z^2 <= rMax2, Quiet@safeExpression[x, y, z], 0], {x, -a, a, 2 a/(n - 1)}, {y, -a, a, 2 a/(n - 1)}, {z, -a, a, 2 a/(n - 1)}] ;

POSTED BY: Sean Clarke
Posted 10 years ago

Thanks for the suggestion Sean, however, I tried to use the solution that you suggested but to no avail. I have attached a notepad containing the code that is messing up if it helps at all. When I tried wrapping the function I'm calculating, every point was set to zero. It is possible to calculate the function as I have done it for other inputs and I can't understand why it won't work for a cube.

Thanks in advance

POSTED BY: Andrew Ross

This is a full programming language. So you can create conditional statements to handle errors like division by zero. I'm kinda guessing without seeing your code, but lets assume you're still working with this expression:

expression = 
 E^(I (-(x/2) - y/2 + z/2 - 3 ArcTan[(-(x/Sqrt[2]) + y/Sqrt[2])/(-(x/Sqrt[6]) - y/Sqrt[6] - Sqrt[2/3] z)]));

We can wrap a function around it that takes only numeric values. If it produces an Indeterminate value, 0 is returned instead.

safeExpression[x_?NumericQ, y_?NumericQ, z_?NumericQ] = (expression /. {Indeterminate -> 0})
POSTED BY: Sean Clarke
Posted 10 years ago

Thanks for the feedback, replacing ArcTan[y/x] with ArcTan[x,y] just replaces the error of evaluating 1/0 with failure with evaluating ArcTan[0,0]. I have also tried using the Arg[x] function but get yet another error trying to evaluate my expression.

When I plot the expression as a hue on a sphere, there is no problem, problem comes in trying to plot a contour of the Absolute value of the function. At the points that the errors are coming in, the function, in the physical interpretation, spirals to zero from all sides so I'm not sure what sort of limit to use.

Is there a way to tell Mathematica, if you encounter this problem, set the value to zero within the Abs[x] function without writing in every single combination of x,y,z within the expanded sum individually as if --- && --- && etc. that causes a problem?

Sorry if this sounds obvious, I'm fairly new to Mathematica

Thanks in advance

POSTED BY: Andrew Ross

So the function doesn't exist at a value. I assume them you want to use the limit at that point then? Does the limit exist?

To illustrate, let's look at the last expression. The problematic part is

ArcTan[(Sqrt[3] (x - y))/(x + y + 2 z)]

What value does this approach as x,y,and z approach 0? It depends entirely on how you approach 0. The limit doesn't exist.

Using the two argument form of ArcTan is often helpful, but usually where the limit exists. Replacing f/g with f/(g+.0001) is really just the same as choosing a random direction to take the limit from.

The point {0,0,0} isn't an isolated problem. The limit also doesn't exist if we let x and y approach some value "a" and let z approach "-a".

Limit[Limit[Limit[ArcTan[Sqrt[3] (x - y),  x + y + 2 z], {y -> a}], {z -> -a}], {x -> a}]

Limit[Limit[Limit[ArcTan[Sqrt[3] (x - y), x + y + 2 z], {x -> a}], {y -> a}], {z -> -a}]

So if you must evaluate this function at these points, replacing the functions value with the limit isn't going to do the trick. You have to decide what it means to evaluate the function at these points where it is not defined.

POSTED BY: Sean Clarke

Sometimes it helps to input ArcTan[y/x] as ArcTan[x,y]. A little trick for fractions f/g where g might go through 0: replace with something like f/(g+.0001) where the added quantity is too small to affect the numerical value.

POSTED BY: S M Blinder
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract