3
$\begingroup$

I'm plotting a function that I get after numerically integrating over another function. Something like:

f[x_,y_]:=NIntegrate[g[x,y,z],{z,0,1}]

I know that above some threshold value for $x$, the integrand in the expression diverges and the function is not defined anymore. I also know that this threshold value varies depending on the value of $y$. However, I don't know exactly what this threshold value is (i.e. I don't have an analytic expression for it).

Now I want to plot this function as a function of $x$ for several different $y$. However, for a given plotting range (for example, $x \in [0,1]$), the plot becomes ugly if this threshold point happens to be in this region since NIntegrate produces just gibberish after this point.

How do I tell Mathematica to stop plotting (and, for example, set the value of function to zero) when this point is reached? In other words, I'm looking for some way to tell the plotting function: "plot this function in this parameter region, but if the value of this function is outside this range of accepted values, then stop plotting."

$\endgroup$
1
  • 1
    $\begingroup$ "stop plotting" could be implemented with "PlotRange", but "stop calculating" is more involved since you need first to find out where to stop $\endgroup$ Commented Nov 21, 2014 at 13:40

2 Answers 2

2
$\begingroup$

I'll use a simpler form for an example. One can keep track of the least value that has given an error/warning message in a variable. It can be set whenever a message is generated using Check. The use of Quiet is optional. You may want to limit the messages that trigger a Check or that are suppressed by Quiet. See their documentation for more.

I also made the undefined value Undefined; change it to 0 or whatever, if that is appropriate to your use-case.

ClearAll[f];
x0 = Infinity;
f[x_?NumericQ] /; x >= x0 := Undefined;
f[x_?NumericQ] := Quiet@Check[NIntegrate[Sqrt[1 + y - x], {y, 0, 1}],
   x0 = x; Undefined]

Plot[f[x], {x, 0, 2}]

Mathematica graphics

To see how it works, put a Print statement in the function.

ClearAll[f];
x0 = Infinity;
f[x_?NumericQ] /; x >= x0 := Undefined;
f[x_?NumericQ] := Quiet@Check[NIntegrate[Sqrt[1 + y - x], {y, 0, 1}],
   Print[x];
   x0 = x; Undefined]

Since Plot does not evaluate f at points in the domain in order, we see that the limit where f becomes undefined changes.

Plot[f[x], {x, 0, 2}];
(*
1.019
1.01414
1.01049
1.00563
1.00052
*)

Caveat: I often use a special context, e.g., f`x0, instead of x0 (which is really Global`x0, unless you've set the context to something else), for such variables that have side-effects on functions. Certainly, x0 is probably a bad choice, since among variable names it is somewhat likely to be used. It would be bad if you set x0 equal to something by hand, forgetting that it determines the behavior of f.

$\endgroup$
2
  • $\begingroup$ Nice idea +1 :) $\endgroup$ Commented Nov 21, 2014 at 15:42
  • $\begingroup$ Thanks. I finally got around to actually trying this and it works nicely! $\endgroup$
    – Echows
    Commented Nov 24, 2014 at 10:31
3
$\begingroup$

Try this idea:

    Plot[If[x < 0, Integrate[Exp[x*z^2], {z, -\[Infinity], \[Infinity]}], 
  None], {x, -1, 1}]

Within this example you will get the following plot:

enter image description here

Have fun!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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