Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a notebook which takes in a user-defined function and starting value and tracks the orbit of the particle under iteration of the function. The problem occurs when the user enters a starting value with an orbit that goes to infinity.

f[x_]:=x^2;
x=1.1;

I get the error message:

General::ovfl:

How can I catch this?

share|improve this question
1  
Look up Check[]. –  J. M. Jun 17 '13 at 18:28
    
It would be nice to have more details about what you are doing. If you're iterating a polynomial, for example, there's a simple bound on the absolute value of the iterate, beyond which, you are guaranteed the orbit will diverge to Infinity. It's easy to incorporate this into a loop or a NestWhileList. One issue with Check is that the Overflow[] won't be detected until after it happens. That's way too late, if you're working with compiled code. –  Mark McClure Jun 17 '13 at 18:41

1 Answer 1

It depends on how you are doing the iteration. For example, if you were using Nest, then it could be replaced by NestWhile. If you are willing to modify the function, the you can explicitly test for divergence in the function, for instance

  f[x_] := Clip[x^2, {-1000, 1000}]

will bound f. If it is a user defined function, then you can wrap it in the Clip or define it using Piecewise:

f[x_] := Piecewise[{{x^2, Abs[x] < 1000}, {1000, x >= 1000}, {-1000, x <= 1000}}]

Again, you might place the user-defined function in the first position and have the upper and lower limits always be fixed to catch the divergence. Perhaps more elegant is to use a conditional function definition:

f[x_] := x^2;
f[x_ /; x > 1000] := 1000
f[x_ /; x < -1000] := -1000

In this case, you can have the user define the function as before, but you also have the explicit divergence criteria predefined. Of course you might use more sensible numbers than "+/-1000"!

share|improve this answer
    
Thank-you! I've got some code to clean up. –  twhoward99 Jun 17 '13 at 18:52
    
+1, especially for the conditional function definition approach. –  Jagra Jun 17 '13 at 20:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.