Message Boards Message Boards


What is the expression for the periodic rectangular function?

Posted 6 years ago
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!

enter image description here

9 Replies

You might find the Math World reference 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!

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[x] - 3/4

This spends half of it's time under 0: SawtoothWave[x] - 1/2

This spends a quarter of it's time under 0: SawtoothWave[x] - 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}]

enter image description here

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}]

enter image description here

Posted 6 years ago

That's concise indeed, and so it is conceptually. It is a pleasure reading it. Thanks a lot!

Posted 2 months ago

Very clever and useful hack. Thank you for the code.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract