Message Boards Message Boards

Plot a function of x with different terms on R- and on R+?



I want to Plot a function of x with different terms on R-, 0 and on R+ as one function rather than as a set of functions. How can I achieve that, there is no such example in the doc for Plot[]?

Thank you.

1 year ago

Can you give a more concrete example of what you are looking for? I'm not sure I understand the question.

POSTED BY: Sean Clarke
1 year ago

You can choose among different approaches. One is to combine different formulas for the negative and positive using Piecewise:

f[x_] = Piecewise[{{1 - x, x > 0}, {x - 1, x < 0}}];
Plot[f[x], {x, -2, 2}, Epilog -> {PointSize[Large], Point[{0, 1/2}]}]

Another way is to combine separate plots with Show:

Show[Plot[1 - x, {x, 0, 2}], Plot[x - 1, {x, -2, 0}], 
 Graphics[{PointSize[Large], Point[{0, 1/2}]}], PlotRange -> All]

An isolated point of the graph requires a separate treatment, as Plot is meant for curves only.

POSTED BY: Gianluca Gorni
1 year ago

Group Abstract Group Abstract