# How to limit domain of Plot dynamically

I have some function f[x,a] and I would like to plot it, on a subdomain of {x,0,1} that is dependent on x and a in some way, g[x,f,a]<0. At the moment I have, for example

Plot[Evaluate[Table[f[x,a], {a,3}]], {x,0,1}]


But f isn't well-defined beyond this subdomain so the output is nonsense and I would like it clipped. But the subdomain is different for each plot, etc. How can I add some clipping that enforces that domain constraint?

(This may not be easy in the general case, but it is sufficient here to assume that the constraint is satisfied at x=0 and there will be some minimal nonzero x from which it will be violated and clipping from there is enough)

A minimal example can be really simple: f[x_,a_]:=a x; and g[x_,f_,a_]:=f+a/10-1;

• please post a complete self contained example. Including definition of f. You can limit the plot range using the PlotRange option Nov 5 '17 at 19:38
• @Nasser but it seems that PlotRange is constant, and I do not have an analytical expression for the limiting x since in reality f and g are solutions to equations, so they're interpolating functions, say. Nov 5 '17 at 19:54

Might be kind of makeshift, but one possibility is to use the Piecewise function to limit the domain (setting the "default" value to Undefined or Indeterminate):

f[x_, a_] := a*x;
g[x_, f_, a_] := f[x, a] + a/10 - 1;

fplot[x_, a_] := Piecewise[
{{f[x, a], g[x, f, a] < 0}},
Undefined
]

Plot[
Evaluate[Table[fplot[x, a], {a, 3}]], {x, -1, 1},
PlotRange -> {{-1, 1}, {-1, 1}},
PlotLegends -> (("a = " <> ToString@#) & /@ Range),
AspectRatio -> 1
] Hopefully this is somewhat close to what you had in mind!

• That's an excellent idea Nov 5 '17 at 20:23
• Instead of None, the more conventional symbols used for this are either Undefined or Indeterminate. Nov 5 '17 at 20:28
• Good to know, thanks!
– Anne
Nov 5 '17 at 20:33

You can use RegionFunction to restrict the region that is displayed, although the pure function can only depend on the x and y coordinates. For your example, you could use:

g[x_, y_] := y + y/(10 x) - 1 < 0


Then:

Plot[
Evaluate @ Table[f[x, a], {a, 3}], {x, -1, 1},
RegionFunction -> g,
PlotRange->{{-1, 1}, {-1, 1}}
] 