1
$\begingroup$

I am confused as to why the two functions below yield different results (as vs ss)? One function assigns the Piecewise function directly, while the other reads the same function from a list of lists. Why doesn't the second function repeat?

piecewiseLists =
  {
   {
    Piecewise[{{signal, t < 2700.}, {0., 2700. <= t < 5400.}}]
    },
   {
    Piecewise[{{signal, t < 15000.}, {0., 15000. <= t < 30000.}}]
    }
   };

signal = 1;
a[t_] := Piecewise[{{signal, t < 2700.}, {0., 2700. <= t < 5400.}}]
as[y_, repeat_] := a[Mod[y, repeat]];
Plot[as[t, 5400.], {t, 0, 36000.}]
s[t_] := piecewiseLists[[1, 1]]
ss[y_, repeat_] := s[Mod[y, repeat]];
Plot[ss[t, 5400.], {t, 0, 36000.}]

After implementing @Pirx's solution, I noticed that symbolic assignment of the piecewise function fails if placed within an actual function. My suspicion is that either there is something strange happening with the independent variable being hidden in the symbolic function. Or there is some other weird Context issues that at this point is beyond my understanding.

piecewiseLists = {{Piecewise[{{signal, t < 2700.}, {0., 
   2700. <= t < 5400.}}]}, {Piecewise[{{signal, t < 15000.}, {0., 
   15000. <= t < 30000.}}]}};
generatePeriodicity[piecewiseLists_] :=
Module[{s, ss},
 s[t_] = piecewiseLists[[1, 1]];
 ss[y_, repeat_] := s[Mod[y, repeat]];
 Plot[ss[t, 5400.], {t, 0, 36000.}]];
 generatePeriodicity[piecewiseLists]
$\endgroup$

1 Answer 1

1
$\begingroup$

The issue is with the delayed evaluation in the second case. Try this:

signal = 1; 
a[t_] := Piecewise[{{signal, t < 2700.}, {0., 2700. <= t < 5400.}}] 
as[y_, repeat_] := a[Mod[y, repeat]]; 
Plot[as[t, 5400.], {t, 0, 36000.}] 
piecewiseLists = {{Piecewise[{{signal, t < 2700.}, 
   {0., 2700. <= t < 5400.}}]}, {Piecewise[{{signal, t < 15000.}, 
   {0., 15000. <= t < 30000.}}]}};
s[t_] = piecewiseLists[[1, 1]];
ss[y_, repeat_] := s[Mod[y, repeat]]; 
Plot[ss[t, 5400.], {t, 0, 36000.}]
$\endgroup$
2
  • $\begingroup$ So this solution works fine for a dynamic environment. However it fails when I try to define the solution in a function. Any idea why this is true? See the newest edit to the question. $\endgroup$
    – tarhawk
    Commented Aug 13, 2016 at 12:11
  • $\begingroup$ After reaching out to wolfram through premiere service, the issue I was having is a bug that is fixed in v.11 $\endgroup$
    – tarhawk
    Commented Aug 18, 2016 at 0:06

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.