I would like to create a graph of the piecewise function
$f(x) = \begin{cases} x + 2, & x < -1 \\ x^2, & -1 \leq x \leq 2 \\ 3, & x > 2\end{cases}$

where the changes in the definition of $f(x)$ are clearly demonstrated. Specifically, I would like to:

a) have each part of the definition graphed in a different colour;

b) have the appropriate open/closed disks (any simpler method than my tedious solution of plotting each disk?)

Here is what I have so far:

Plot[Piecewise[{{2+x,x<-1},{x^2,-1<x&&x<=2},{3,x>2}},0],{x,-5,6}, PlotStyle ->Thick, Epilog->{PointSize[0.03],Blue,Point[{-1,1}],PointSize[0.03],Blue,Point[{2,3}],PointSize[0.02],White,Point[{2,3}]}]

Which yields this:

enter image description here

Again, although this has the dots, it's a very tedious solution, and I would like to be able to set the colour of the three parts, separately (ideally have Mathematica do it automatically).

I am looking for a solution that students using a sandboxed version of Mathematica (provided through WolframAlpha Pro) could implement.

  • 1
    $\begingroup$ Please check if the linked threads address your needs. If they don't, edit your question to explain why, and we can reopen this question. $\endgroup$ Commented Aug 28, 2017 at 21:45
  • $\begingroup$ Have edited question to reflect the nuance between the questions (viz. sufficiently simple to be appropriate for sandboxed, student-version). Also, in >1H of searching and looking at over a dozen "related questions" and even composing this question - neither of those links were proposed. Does not really encourage users to do the research to avoid repeat questions.. I have tried to improve tagging on the other two, but perhaps s/o with more experience can have a look as well. $\endgroup$
    – Rax Adaam
    Commented Aug 28, 2017 at 21:58
  • $\begingroup$ If you students actually have Mathematica (as opposed to just Wolfram Alpha), they should be able to use the routines in the linked threads (probably after some suitable repackaging). If they only have access to Alpha, I don't think they can do any custom plots like this at all. $\endgroup$ Commented Aug 28, 2017 at 21:59
  • $\begingroup$ They have access to the "Wolfram Development Platform" which comes with Alpha Pro, which seems to be in between the two. The routines might work, but a) I wouldn't know where to begin, personally, and b) the routines are lightyears beyond students ability to implement. $\endgroup$
    – Rax Adaam
    Commented Aug 28, 2017 at 22:02
  • $\begingroup$ The Wolfram Development Platform is version of Mathematica Online with a variety of licenses which vary by the amount of disk space and cpu time allowed. All of them, however, include the full Wolfram Language. I doubt that plotting anything that students are likely to type in will hit the CPU limits. $\endgroup$ Commented Aug 28, 2017 at 22:53

1 Answer 1

Plot[Piecewise[{#}, None] & /@ {{2 + x, 
     x < -1}, {x^2, -1 < x && x <= 2}, {3, x > 2}} // 
  Evaluate, {x, -5, 6}, PlotRange -> All,
 PlotStyle -> Thick, 
 Epilog -> {PointSize[0.03], Blue, Point[{-1, 1}], PointSize[0.03], 
   Blue, Point[{2, 3}], PointSize[0.02], White, Point[{2, 3}]}]


enter image description here

Update: Demonstration of manual color manipulation. Also some minimalist conditioning around piecewise point graphics.

Specify conditions, e.g.:

conds = {{2 + x, x < -1}, {x^2, -1 < x && x <= 2}, {3, x > 2}};

and plot

  Piecewise[{#}, None] & /@ conds // 
   Evaluate, {x, -5, 6}, PlotRange -> All,
  PlotStyle -> {{Thick, Red}, {Thick, Blue}, {Thick, 
     RGBColor[0.5, 0.19, 0.5]}}, 
 Epilog->Flatten[{PointSize[0.03], joinPiece[conds[[{1, 2}]], x], 
    joinPiece[conds[[{2, 3}]], x]}]]


enter image description here

using the following minimal auto-formatting based on continuity (assuming region a < region b):

joinPiece[{a_, b_}, x_] := Module[{myVal},
   If[ (a[[1]] /. FindMaximum[{x, a[[2]]}, x][[2, 1]]) === (
      b[[1]] /. (myVal = FindMinimum[{x, b[[2]]}, x])[[2, 1]]
      ), {Blue, 
     Point[{myVal[[2, 1, 2]], a[[1]] /. myVal[[2, 1]]}]}, {White, 
     EdgeForm[{Thick, Blue}], 
     Disk[{myVal[[2, 1, 2]], b[[1]] /. myVal[[2, 1]]}, .15]}]];
  • $\begingroup$ Brilliant, perfect - thank you. This appears to be so much simpler than the routines suggested on the linked pages, you may wish to also post your solution there to receive proper credit. Cheers! $\endgroup$
    – Rax Adaam
    Commented Aug 28, 2017 at 22:04
  • $\begingroup$ John - is it possible to specify the colours using this approach? $\endgroup$
    – Rax Adaam
    Commented Aug 28, 2017 at 22:06
  • 1
    $\begingroup$ Yes, with PlotStyle, (e.g. PlotStyle->{ Red, Blue, RGBColor[0.5, 0.19, 0.5]}). I'll update my answer to include. $\endgroup$ Commented Aug 28, 2017 at 22:12
  • $\begingroup$ thank you kindly for the elaborations and your time. Am astounded it is this complicated to create such common graphs, but am glad to have found a relatively straight-forward implementation. $\endgroup$
    – Rax Adaam
    Commented Aug 29, 2017 at 19:20

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