6
$\begingroup$

I have a piecewise function represented by two lists,

x= {0, 30, 60, 120, 150, 180};
y= {0, 1, 0, -1, 0};

and the plot of this function is

enter image description here.

Now I would like to shift this function horizontally by an arbitrary angle and then add the shifted version to the original one. The original one can be thought of as periodic with a fundamental period of 180 degrees. For simplicity, shift it 15 degrees to the right and the sum should be something like this

enter image description here

By my previous convention, this result should be represented by x1= {30, 45, 60, 75, 120, 135, 150, 165}, y1= {0, 1, 2, 1, 0, -1, -2, -1, 0}. My question is, what can be one of the least complicated ways to obtain this result, particularly the vertical value list y1 above, by using list manipulations?

$\endgroup$
7
$\begingroup$
x = {0, 30, 60, 120, 150, 180};
y = {0, 1, 0, -1, 0};

f[z_] := Piecewise @ Transpose @ {y, Less @@@ Insert[z, 2] /@ Partition[x, 2, 1]}
f[z]

enter image description here

Plot[f[z], {z, 0, 180}, Exclusions -> None]

enter image description here

Plot[f[z] + f[z - 15], {z, 0, 180}, Exclusions -> None]

enter image description here


g[z_] := f[z] + f[z - 15]

PiecewiseExpand @ g[z]

enter image description here

Building on this answer:

args = Join[{0, 180}, 
    z /. (Reduce[Not@Reduce[D[g[z], z] ∈ Reals, z], z] // 
        ToRules // List)] // Sort // DeleteDuplicates

{0, 30, 45, 60, 75, 120, 135, 150, 165, 180}

g[#] & /@ (RandomReal[#] & /@ Partition[args, 2, 1])

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


There's also an undocumented function converting Piecewise to UnitStep (thanks to xzczd):

Simplify`PWToUnitStep @ g[z]

enter image description here

$\endgroup$
  • $\begingroup$ I'm interesting about how do you dig to get that? $\endgroup$ – yode Dec 14 '16 at 13:41
  • $\begingroup$ Yep,I have.Thanks. :) $\endgroup$ – yode Dec 14 '16 at 14:14
6
$\begingroup$

You can also use UnitBox, e.g.

f[x_] := UnitBox[(x - 30)/30 - 1/2] - UnitBox[(x - 120)/30 - 1/2]
Plot[f[x] + f[x - 15], {x, 0, 200}]

enter image description here

Update

I add this to address Mr. Wizard's comment. There are many ways and probably better but to get corners:

g[x_] := f[x] + f[x - 15]
pts = {u, 
    g[u]} /. {ToRules@Reduce[Not[FunctionDomain[D[g[u], u], u]], u]};
{xs, ys} = Transpose[pts]
Plot[g[x], {x, 0, 200}, 
 Epilog -> {Blue, PointSize[0.02], Point[Thread[{xs, 0}]]}, 
 Exclusions -> None]

enter image description here

Correction

To deal with the dfference between the definition of UnitBox and the desired outcome a small offset to get what is aimed for:

ptscorr = {u, 
   g[u - 0.01]} /. {ToRules@
    Reduce[Not[FunctionDomain[D[g[u], u], u]], u]};
{xscorr, yscorr} = Transpose[ptscorr]

yields:

{30, 45, 60, 75, 120, 135, 150, 165}, {0, 1, 2, 1, 0, -1, -2, -1}}
$\endgroup$
  • $\begingroup$ How would you convert the UnitBox expression into the x1/y1 output shown? $\endgroup$ – Mr.Wizard Dec 8 '16 at 6:50
  • $\begingroup$ @Mr.Wizard I have updated to address your comment ( I hope). It could be done a few ways but something different. This spider built an elaborate web in my garden and made me think of you. I hope you pass "happy holidays". On a selfish note: I have always wanted to post on the "i've passed 5000" but have never found an interesting enough number and am too inept to game. 40000 was a nice milestone in an otherwise terrible year :) $\endgroup$ – ubpdqn Dec 8 '16 at 7:11
  • 1
    $\begingroup$ You already had my vote but now the answer is even better. I am happy that a wonder of nature makes you think of me. I am not happy to hear that you have had a terrible year. I hope that things have turned around now, whatever the circumstance. $\endgroup$ – Mr.Wizard Dec 8 '16 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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