# How to plot a function that is not defined after certain point

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."

• "stop plotting" could be implemented with "PlotRange", but "stop calculating" is more involved since you need first to find out where to stop – Dr. belisarius Nov 21 '14 at 13:40

## 2 Answers

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}] 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., fx0, instead of x0 (which is really Globalx0, 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.

• Nice idea +1 :) – Dr. belisarius Nov 21 '14 at 15:42
• Thanks. I finally got around to actually trying this and it works nicely! – Echows Nov 24 '14 at 10:31

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: Have fun!