I have edited this to include integration:

This is a more general case of this question.

I want to create a function that gives the integral of a series of single pulses of the form Cos[Pi*(x/2)]^2 for all values of x that satisfy some Boolean test.

For example, say I wanted to create a curve that summed pulses for all instances of x on the Fibonacci sequence. Manually, I would do something like

Plot[{With[{n = 1}, Cos[Pi*(Clip[x - n]/2)]^2] + 
With[{n = 2}, Cos[Pi*(Clip[x - n]/2)]^2] + 
With[{n = 3}, Cos[Pi*(Clip[x - n]/2)]^2] + 
With[{n = 5}, Cos[Pi*(Clip[x - n]/2)]^2] + 
With[{n = 8}, Cos[Pi*(Clip[x - n]/2)]^2] + 
With[{n = 13}, Cos[Pi*(Clip[x - n]/2)]^2]}, {x, 0, 15}]

enter image description here

Integrating (how?) would create a continuous curve.

That's just an example. I want a function in the form Integral[booleantest*pulse,x] that gives the cumulative area under a series of this style of pulse, where each pulse is centred on any Boolean test on integer values of x.

So it seems to me that my question has three parts:

  1. How do I create a function pulse that gives this pulse-form for all integer x? Clearly using With[{n=...,] isn't going to work. It requires me to manually specify all instances of pulse.

  2. In principle, it's easy enough to create a Boolean test: booleantest=Boole[EvenQ[x/3]], booleantest=Boole[PrimeQ[x]], etc.

    So, how do I multiply the two together? booleantest*pulse should yield pulse for all instances of booleantest=1, and 0 else. But it doesn't work. For example: pulse = Cos[Pi*(Clip[x - n]/2)]^2;booleantest = Boole[EvenQ[n/3]];Plot[pulse*booleantest, {n, 0, 15}]

enter image description here

  1. How do I create the integral?

I'm clearly doing lots of things wrong... So, sticking to the idea of pulse*booleantest (if possible), how do I do this?


Many thanks to @kglrfor the answer below. I have now tried to expand this to a more general Boolean choice, as per original post. I'm ashamed to say that I can't figure out how do it. @kglr, your suggestion works great for the Fibonacci sequence. But how would it work for, for example, with EvenQ[x/n]=True? Or 'FreeQ[{x},n]? Or *any* of a host of other 'filters' of this generalQ` nature? It's the general approach that I'm after.


1 Answer 1


Update: A function that takes an arbitrary list of integers as the second argument:

f2[x_?NumericQ, m : {__Integer}] := Total[Cos[Pi*(Clip[x - #]/2)]^2 & /@ m]
Grid[Partition[Plot[f2[x, #], {x, -1 + Min@#, 1 + Max@#}, PlotRange -> {0, 2}, 
    ImageSize -> 300, PlotLabel -> Style["m = " <> ToString[#], 16]] & /@ 
  {Fibonacci[Range@7], Select[Range[-5, 5], EvenQ], 
   Select[Range[-5, 5], OddQ], Select[Range[-15, 15], Divisible[#, 6] &], 
   {-11, -10, -9, -2,  1,  5, 6}, RandomSample[Range[-15, 15], 6]}, 3], 
 Dividers -> All]

enter image description here

Original answer:

f1[x_?NumericQ, n_Integer] := Total[Cos[Pi*(Clip[x - #]/2)]^2 & /@ Fibonacci[Range[n]]]
Grid[Partition[Plot[f1[x, #] , {x, 0, 20}, PlotStyle -> ColorData[97][#], 
     PlotLabel -> Style["n = " <> ToString[#], 16], 
     PlotRange -> {0, 2}, ImageSize -> 200] & /@ Range[9], 3], 
 Dividers -> All]

enter image description here

Plot[Evaluate[f1[x, #] & /@ Range[9]], {x, 0, 20}, 
  PlotRange -> {0, 2}, PlotLegends -> Range[9], ImageSize -> 500]

enter image description here

f2[x_?NumericQ, n_Integer] := NIntegrate[f1[s, n], {s, 0, x}]
ListLinePlot[Table[{x, f2[x, 7]}, {x, 0, 15, 1/100}]]

enter image description here

  • $\begingroup$ Fantastic. Thanks @kglr :-) $\endgroup$ Oct 23, 2018 at 8:31
  • $\begingroup$ Hi all (especially @kglr). Please see clarification in my edit to the original post above. The solution offered works for the Fibonacci series, but not in a more general context... So, I'm very grateful but would also appreciate further input. $\endgroup$ Oct 24, 2018 at 11:38
  • $\begingroup$ Many thanks for the updated answer. Perfect! $\endgroup$ Oct 24, 2018 at 12:17

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.