The function I am referring to is as shown below, the height of the boxes being h, the length of each of them a, and the length of each gap b.
How can I write an expression for it in Mathematica (preferably most efficiently)?
Thanks a lot!
The function I am referring to is as shown below, the height of the boxes being h, the length of each of them a, and the length of each gap b.
How can I write an expression for it in Mathematica (preferably most efficiently)?
Thanks a lot!
You might find the Math World reference Square Wave -- from Wolfram MathWorld of use. Mathematica code is included there
There are a lot of different representations possible for a lot of different purposes. So the answer might really depend. A clean way of doing it is to use SawtoothWave
This spends 3/4ths of it’s time under 0:
SawtoothWave - 3/4
This spends half of it’s time under 0:
SawtoothWave - 1/2
This spends a quarter of it’s time under 0:
SawtoothWave - 1/4
So you can use UnitStep[SawtoothWave[x] - p] where p is the proportion of the function that should be 0. If you want it expanded you can always divide x by how much you want the function stretched out by.
To illustrate Sean’s idea:
Manipulate[Plot[UnitStep[SawtoothWave[x] - p], {x, -1, 2}, Filling -> 0], {{p, .2}, .1, .9}]

Replying to Sean Clarke:
Thank you very much! Your idea is a hundred times better than what I originally thought, which was to sum an infinite number of HeavisidePi (square pulse) functions with different offsets.
In reply to Vitaliy Kaurov:
Thank you very much for the code and demonstration! I am new to Mathematica, so the code is really of great help to me!
The SquareWave function has equal up- and down-periods, which is not precisely what I had in mind, but thank you for the reply, since I’ve learned a new function!
That’s concise indeed, and so it is conceptually. It is a pleasure reading it. Thanks a lot!
Very clever and useful hack. Thank you for the code.