$\begingroup$
So, Wolfram Alpha is capable of plotting two functions in one figure.
But I am trying to express a piecewise function that has a different form on two (touching) domains.
In C code:
x < pi ? cos(pi/2 + x/2) : cos(x) // for x 0..2piI tried things like:
plot cos(pi/2 + x/2) and cos(y) for x=0 to pi and y=pi to 6.28...but whatever I try, I can't make it plot piecewise?
I also tried the mathematical notation for conditions using the | notation, to no avail.
1 Answer
$\begingroup$f[x_] := Piecewise[{{Cos[\[Pi]/2 + x/2], x < \[Pi]}, {Cos[x], x >= \[Pi]}}]
Plot[f[x], {x, 0, 2 \[Pi]}, Frame -> True, Axes -> False,
FrameTicks -> {{{-1, 0, 1}, None}, {{0, Pi/2, Pi, (3 Pi)/2, 2 Pi}, None}},
GridLines -> {{0, Pi/2, Pi, (3 Pi)/2, 2 Pi}, {-1, -1/2, 0, 1/2, 1}}]The function plotted is
$$ f(x) = \left\{ \begin{array}{c} \cos (x/2+\pi/2), \quad &(0<x<\pi)\\ \cos(x), \quad &(\pi \le x < 2\pi). \end{array} \right.$$
$\endgroup$ 3