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}]
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:
How do I create a function
pulse
that gives this pulse-form for all integerx
? Clearly usingWith[{n=...,]
isn't going to work. It requires me to manually specify all instances ofpulse
.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 yieldpulse
for all instances ofbooleantest=1
, and0
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}]
- 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?
UPDATE
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 general
Q` nature? It's the general approach that I'm after.