11
$\begingroup$

Is it possible to plot a function over a interval with a gap in the middle; e.g., $f(x)=x^2$ over $-5<x<-1\cup1<x<5$?

I am using Mathematica 7 and most of the options in the answers don't seem to work for me.

$\endgroup$
0

4 Answers 4

11
$\begingroup$

In V8 and up, one can use ConditionalExpression:

Plot[ConditionalExpression[x^2, -5 < x < -1 || 1 < x < 5], {x, -5, 5}]

Mathematica graphics


Response to updated question

This might work in all versions of Mathematica:

Show[
 Plot[x^2, {x, -5, -1}],
 Plot[x^2, {x, 1, 5}],
 PlotRange -> All]
$\endgroup$
4
  • $\begingroup$ Piecewise[] with Indeterminate as the default case works here as well. $\endgroup$ Dec 9, 2016 at 21:11
  • $\begingroup$ @J.M. Nice to hear from you, again! $\endgroup$
    – Michael E2
    Dec 9, 2016 at 22:45
  • $\begingroup$ It's nice to be around again, for sure. :) I was ill for most of my hiatus... $\endgroup$ Dec 9, 2016 at 22:48
  • $\begingroup$ @J.M. I'm sorry to hear that. I wish you better health in the coming year! $\endgroup$
    – Michael E2
    Dec 9, 2016 at 22:49
13
$\begingroup$

You can also define f conditionally:

f[x_] := x^2 /; (-5 < x < -1 || 1 < x < 5)
$\endgroup$
1
  • 7
    $\begingroup$ This is good, but f[x_ /; (-5 < x < -1 || 1 < x < 5)] := x^2 is better $\endgroup$
    – m_goldberg
    Nov 20, 2016 at 22:22
12
+100
$\begingroup$

With

reg = ImplicitRegion[-5 < x < -1 || 1 < x < 5, x]

or

reg = Interval[{-5, -1}, {1, 5}]

do

Plot[x^2, {x} ∈ reg]

enter image description here


EDIT

This might work for v7:

Plot[If[-5 < x < -1 || 1 < x < 5, x^2], {x, -5, 5}]
$\endgroup$
4
  • $\begingroup$ I am using Mathematica 7 and these options don't seem to work. $\endgroup$
    – gbd
    Nov 20, 2016 at 22:17
  • $\begingroup$ This is a nice V10+ solution to the problem: +50 just to draw attention to it for future users with an up-to-date Mathematica. Plot[x^2, {x} ∈ Interval[{-5, -1}, {1, 5}]] seems particularly nice to code. $\endgroup$
    – Michael E2
    Nov 20, 2016 at 22:48
  • $\begingroup$ @MichaelE2 Thanks for the kind words :) $\endgroup$
    – corey979
    Nov 20, 2016 at 23:03
  • $\begingroup$ Hmm, when I started the bounty, the least award on the popup menu was 100. Well, I've seen +50 bounties, so I probably messed up somehow. But what the heck, think of it as a holiday bonus. Don't spend it all in one place. :) -- As I said, I like the fact that one just specifies the desired domain, and you don't have to define a new, temporary, auxiliary function or other such nonsense. $\endgroup$
    – Michael E2
    Nov 26, 2016 at 19:28
11
$\begingroup$

Another way is with the option RegionFunction:

Plot[x^2, {x, -5, 5}, RegionFunction -> Function[x, -5 < x < -1 || 1 < x < 5]]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ I guess this should work in V6 and higher. $\endgroup$
    – Michael E2
    Nov 21, 2016 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.