# Can I confine the codomain when drawing plot?

I want to confine the codomain of my plot. For example, I want to draw y=f(x) only when 3x<y<7x+3. If not, want to draw y=0.

Is it possible in Mathematica plot?

• Just as suggested in your previous question, use Piecewise. – xzczd Jul 5 at 6:50
• Thanks, but how can I use Piecewise in this case? In my previous question, I confined the domain, not codomain. – Chris Jul 5 at 6:55
• do you have an example of f(x)? – Nasser Jul 5 at 6:58
• Just confine the codomain as you confined the domain. – xzczd Jul 5 at 6:59
• It's a bit complex, but let me give a simplified example. y = (1/2) * (2H-2L+2*sqrt(H(H-L)(1-a))). And want to draw a graph only when y1< y <y2 (y1 and y2 are function of H, L, and a). – Chris Jul 5 at 7:03

May be

Clear["Global*"]
f[x_?NumericQ] := x + x^2; (*made one up, since none was given *)
g[x_?NumericQ] := Piecewise[{{f[x], 3 x < f[x] < 7 x + 3}, {0, True}}];
Plot[g[x], {x, 0, 8}, PlotRange -> All]


This is f[x]

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


• It works very well even for the very complex multi-variate functions. Thank you so much! – Chris Jul 5 at 7:23
• BTW, what ?NumericQ stands for? – Chris Jul 5 at 7:24
• @Chris it is to insure these functions are called only for numerical input, by the Plot command that is. Since these only make sense with x is numerical, due to the comparison being made. – Nasser Jul 5 at 7:25
• One can also use ConditionalExpression[f[x], 3 x < f[x] < 7 x + 3] to avoid the extension by zero that one gets with Piecewise. – Michael E2 Jul 5 at 11:50
• @Nasser Thank you so much! – Chris Jul 5 at 13:59

PlotRange -> {{<domain>}, {<codomain>}} is the way to set the codomain in its normal meaning.

But the OP has in mind restricting the region that is plotted, and that task can be handled with RegionFunction.

Plot[10 (x - 10) Sin[x], {x, -2, 80},
RegionFunction -> Function[{x, y}, 3 x < y < 7 x + 3]]
`