# What is the expression for the periodic rectangular function?

Posted 6 years ago
8908 Views
|
9 Replies
|
7 Total Likes
|
 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!
9 Replies
Sort By:
Posted 6 years ago
 You might find the Math World reference http://mathworld.wolfram.com/SquareWave.html of use. Mathematica code is included there
Posted 6 years ago
 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!
Posted 6 years ago
 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 SawtoothWaveThis spends 3/4ths of it's time under 0: SawtoothWave[x] - 3/4This spends half of it's time under 0: SawtoothWave[x] - 1/2This spends a quarter of it's time under 0: SawtoothWave[x] - 1/4So 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.
Posted 6 years ago
 To illustrate Sean's idea: Manipulate[Plot[UnitStep[SawtoothWave[x] - p], {x, -1, 2}, Filling -> 0], {{p, .2}, .1, .9}] 
Posted 6 years ago
 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!
Posted 6 years ago
 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.
Posted 6 years ago
 This also works: pulseTrain[x_, h_, a_, b_] := If[Mod[x, a + b] < a, h, 0] Plot[pulseTrain[x, 1, 3, 2], {x, 0, 20}]