3
$\begingroup$

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.

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

3 Answers 3

4
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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 :

enter image description here

Mathematica version 12.1

It works too with Plot3D and complicated domains.

$\endgroup$
3
  • $\begingroup$ OP says in a comment that -3 and 3 cannot be included, yet this is the case in all your solutions I think. $\endgroup$
    – C. E.
    Commented Jul 7, 2020 at 1:05
  • $\begingroup$ 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). $\endgroup$
    – andre314
    Commented Jul 7, 2020 at 7:30
  • $\begingroup$ 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. $\endgroup$
    – C. E.
    Commented Jul 7, 2020 at 7:34
2
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ You can apply this to your own graphs as well $\endgroup$ Commented Jul 7, 2020 at 13:36

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.