# Plotting a function with the domain restricted. Basic question

I'm trying to Plot a simple function on a restricted domain.

I tried ListLinePlot[Table[x^2, {x, x>-3, x<3, 1}]].

This doesn't work. I'm not sure how to take the list for the domain of the function and turn it into an inequality that Mathematica can make sense of.

• How about: ListLinePlot[Table[x^2, {x, -3, 3, 0.1}]] or just Plot[x^2, {x, -3, 3}] – bill s Jul 6 '20 at 17:27
• x cannot be equal to -3 or 3. – Andrew Jul 6 '20 at 17:37
• ListLinePlot[Table[{x, x^2}, {x, -2.99, 2.99, 0.01}]] or Plot[x^2, x \[Element] ImplicitRegion[-3 < x < 3, x]] – Bob Hanlon Jul 6 '20 at 18:02

When using Plot, you could use RegionFunction:

Plot[
x^2,
{x, -3, 3},
RegionFunction -> Function[{x, y}, -3 < x < 3]
]


Or ConditionalExpression:

Plot[
ConditionalExpression[x^2, -3 < x && x < 3],
{x, -3, 3}
]


When using ListLinePlot, I would consider using Select to filter out points I don't want:

pts = Select[
Table[{x, x^2}, {x, -3, 3, 0.1}],
-3 < First[#] < 3 &
];
ListLinePlot[pts]


If I'm generating a list and don't even want to generate them, I could do something like:

pts = Table[
If[-3 < x < 3, {x, x^2}, Nothing],
{x, -3, 3, 0.1}
];
ListLinePlot[pts]


Unless I'm missing something, these solutions should work fine... please let me know if there's a problem.

Out of curiosity, I have tried to use Region[] as a domain in the command Plot[].

It seems to work fine.

Examples :

domain00 = Region[MeshRegion[{{-3}, {3}}, Line[{1, 2}]]];
Plot[x^2, x ∈ domain00, PlotRange -> {{-5, 5}, Automatic}]

domain01 = Line[{{-3}, {3}}];
domain01 // RegionQ
Plot[x^2, x ∈ domain01, PlotRange -> {{-5, 5}, Automatic}]

domain02 = Interval[{-3, 3}];
domain02 // RegionQ
Plot[x^2, x ∈ domain02, PlotRange -> {{-5, 5}, Automatic}]


The three blocks of code give :

Mathematica version 12.1

It works too with Plot3D and complicated domains.

• OP says in a comment that -3 and 3 cannot be included, yet this is the case in all your solutions I think. – C. E. Jul 7 '20 at 1:05
• I didn't see that the OP wanted to exclude -3 and +3. I can't interpret this requirement (since 3 and -3 are Infinite precison number and the Plot family uses machine precision number by default). – andre314 Jul 7 '20 at 7:30
• Yeah, the requirements are not clear. OP was oddly explicit with the sampling in his Table command though, and that led me to believe that perhaps there is a jump between the values at $\pm 3$ and the other values, hence leading me to recommend Select to filter out values not belonging to the (endpoint exclusive) interval. In any case, if your answer is not useful to OP then it will still be useful to others who come here in the future, and will most definitely not notice these nuances. – C. E. Jul 7 '20 at 7:34

Another basic way of plotting a graph with restricted domain is stated below:

Plot[x^3,{x,-5,10}]


The Domain is restricted from -5 to 10

• You can apply this to your own graphs as well – Krishiv SURESH Jul 7 '20 at 13:36