I have a function that produces a single impulse spanning the range [-1,1]:

func1 = Cos[Pi*(Clip[x]/2)]^2

enter image description here

I want to create a second function that produces a series of such pulses, with each pulse centred on an instance of EvenQ[x/3]=True. It feels like it should be simple, but I can't figure it out. This is what I tried:

eventest = If[EvenQ[x/3] = True, 1, 0]; 
func2 = eventest*func1; 
Plot[func2, {x, 0, 20}]

...but as you can see, it doesn't work.

What am I doing wrong?

  • $\begingroup$ Hi @kglr, sadly this doesn't seem to work. it produces an inverted square wave in the range [-1,1] and nothing else. I'm after a series of pulses like the one I have now added to the original post, with each pulse centred on an instance of x/3 is even. $\endgroup$ Oct 21, 2018 at 13:45
  • $\begingroup$ thank you @Richard $\endgroup$
    – kglr
    Oct 21, 2018 at 14:24

2 Answers 2


Using the method from this answer (see this as well):

pulse[x_] := Cos[π Clip[Mod[x, 6, -3]]/2]^2
  • $\begingroup$ (will add a plot when I am not using gedanken Mathematica) $\endgroup$ Oct 21, 2018 at 14:31
  • 1
    $\begingroup$ Thanks @J.M. - this works, so will mark as answered; but I do wonder if there is a more general version that would work with more general sequences. For example, what if I wanted a sequence that gave pulses at all x that satisfy TrueQ[truetest] for some arbitrary test on x? Just trying to learn how to drive the machine better! $\endgroup$ Oct 21, 2018 at 16:03
  • $\begingroup$ I like the idea of gedanken Mathematica, BTW! Hopefully you'll get the machine back soon! :-) $\endgroup$ Oct 21, 2018 at 16:04
  • $\begingroup$ Boole[] (i.e. the Iverson bracket) is a useful tool for constructing general conditional functions; but, constructing the right conditions to put in it is as much art as science. $\endgroup$ Oct 21, 2018 at 16:09
  • $\begingroup$ Interesting (and again thank you). I just tried Plot[Boole[OddQ[x]]*Cos[Pi*(Clip[x]/2)]^2, {x, -5, 5}], but it just gives a flat-line zero... This could be tricky! $\endgroup$ Oct 21, 2018 at 16:16
Plot[Boole[Or @@ (Divisible[{Floor[x + 1], Ceiling[x - 1]}, 6])] 
  Cos[Pi x / 2]^2, {x, -15, 15}, PlotRange -> {0, 1}]

enter image description here


Plot[Boole[And @@ EvenQ[Quotient[Through[{Floor, Ceiling}[x + 1]], 3]]] 
 Cos[Pi x/2]^2, {x, -15, 15}, PlotRange -> {0, 1}]

same picture


Your Answer

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

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