2
$\begingroup$

I defined a periodic piece wise function y[x_] with some discrete steps with Piecewise command and two lists xv and yv, which are the end points and values of the pieces, respectively. y[x_] is shown below.

y[x_]

Then I do a shifted and linearly combined version from y[x_], e.g., ys = y[x] - 1/2 y[x - 120] - 1/2 y[x + 120]. ys[x_] is shown below.

ys[x_]

My question is, how to "read" from the synthesized function ys the information of the end points and values of all pieces? For the example given, I would like two lists to be produced in the form of xsv = {-180, -150, -90, -30, 30, 90, 150, 180} and ysv = {-2, -1, 1, 2, 1, -1, -2}.

The code for this process is

xv = {-180, -90, 90, 180};
yv = {-1, 1, -1};
y[x_] := Piecewise[Table[{yv[[i]], xv[[i]] < Mod[x, 360, -180] < xv[[i + 1]]}, {i, Length[yv]}]];
Plot[y[x], {x, -180, 180}, Exclusions -> None]
ys = y[x] - 1/2 y[x - 120] - 1/2 y[x + 120];
Plot[ys, {x, -180, 180}, Exclusions -> None]
$\endgroup$

1 Answer 1

1
$\begingroup$
pw[x_] = PiecewiseExpand[ys, -180 < x < 180]

$$\begin{cases} -2 & x>150\lor x<-150 \\ -\frac{3}{2} & x=-150\lor x=150 \\ -1 & -150<x<-90\lor 90<x<150 \\ \frac{1}{2} & x=-60\lor x=60 \\ 1 & -90<x<-60\lor -60<x<-30\lor 30<x<60\lor 60<x<90 \\ \frac{3}{2} & x=-30\lor x=30 \\ 2 & -30<x<30 \end{cases}$$

Because the function is piecewise constant, we may apply the following trick to extract the discontinuities:

xsv = Join[{-180}, Union@Cases[D[pw[x], x][[1, 1, 2]], _?NumericQ, ∞], {180}]
ysv = Map[pw, MovingAverage[xsv, 2]]

{-180, -150, -90, -60, -30, 30, 60, 90, 150, 180}

{-2, -1, 1, 1, 2, 1, 1, -1, -2}

$\endgroup$
1
  • $\begingroup$ I would like two lists to be produced in the form of xsv = {-180, -150, -90, -30, 30, 90, 150, 180} and ysv = {-2, -1, 1, 2, 1, -1, -2}. The function values at the transition points are also desired to be ignored. $\endgroup$
    – nanjun
    Commented Oct 31, 2018 at 19:34

Your Answer

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

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