0

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..2pi

I 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.

Bram
  • 115
  • 1
    What exactly is it you want to plot? I cannot quite make out the "computer code" Is that code (what language, C?) or pseudocode? – mjw Jul 04 '21 at 18:35
  • 2
    Do you want to plot a piecewise function? $f = \cos(\pi/2+x/2)$ when $(0<x<\pi)$ and $f=\cos(x),$ whe $ (\pi \le x < 2\pi) $ – mjw Jul 04 '21 at 18:37
  • @mjw Thanks. Yes, exactly that. I've added the piecewise term to my question. I think I confused partial with piecewise. – Bram Jul 04 '21 at 18:41
  • I advise you to post this question on https://mathematica.stackexchange.com – Jean Marie Jul 04 '21 at 18:41

1 Answers1

0
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}}]

enter image description here

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.$$

mjw
  • 8,647
  • 1
  • 8
  • 23
  • Awesome, thank you. So the piecewise keyword is what matters here. Note that your notation is not accepted by Wolfram Alpha for being too long. I guess you need a PRO account to see it. – Bram Jul 04 '21 at 18:52
  • You are welcome! Sorry about that, in Mathematica, there is plenty of room to get fancy. The main point is to produce a piecewise plot. This works in Wolfram $\alpha$: Plot[Piecewise[{{Cos[[Pi]/2 + x/2], x < [Pi]}, {Cos[x], x >= [Pi]}}],{x,0,2Pi}] – mjw Jul 04 '21 at 18:55
  • https://www.wolframalpha.com/input/?i=Plot%5BPiecewise%5B%7B%7BCos%5B%5C%5BPi%5D%2F2+%2B+x%2F2%5D%2C+x+%3C+%5C%5BPi%5D%7D%2C+%7BCos%5Bx%5D%2C+x+%3E%3D+%5C%5BPi%5D%7D%7D%5D%2C%7Bx%2C0%2C2Pi%7D%5D – mjw Jul 04 '21 at 19:02