From Sine to Square

How to make a square wave from sine wave. If sine is applied to sine repeatedly, it gradually gets shaped into a Square wave. I have used the Nesting function in Mathematica to apply sine to Itself.  For example, level-2 nesting means that the plotted function will be Sin[Sin[x]], level-3 will be Sin[Sin[Sin[x]]]. For various levels of Nesting one can observe the gradual transition from sinusoidal to a square. My computer was able to do that till level 20,000. For any higher numbers, the kernel crashed. A faster multicore processor should be able to do it for  much higher levels. But it is apparent that at Level-infinity it will be a perfect square wave. And unlike a perfect square-wave which is continuous but not differentiable at certain points, this function is continuous and differentiable everywhere.

The gradual transition has been shown below with the level of Nesting Indicated.

Nest Level 1

Nest Level 2Nest Level 3Nest Level 4

Nest Level 5Nest Level 10Nest Level 20Nest Level 100Nest Level 200Nest Level 1000Nest Level 10000

A comparison plot of all the above in one graph is done below which gives an idea about the transition.

Sine Nest all

It should be mentioned that the application of Sine repeatedly to itself decreases the amplitude to a very low value and hence all the plots have been normalized. An unnormalized function is plotted below. Please notice the amplitude, it is really very low.

Nest Level 10000 Unnormalized

Here is an animation. It might be a be a bit fast (and slightly Cheesy) but it will give a good idea of the transition.


 Improved Animations

Nest from 1 to 100 (Step 1)


Nest from 1 to 1000 (Step 20)


Nest from 1 to 10000 (Step 50)


Reason why this happens:

Sin[x] is a non linear function which means that the rate of its variation is different for different values of x. Mathematica takes the given input as  radian value unless specified. So for every level of nesting, the range of the input value becomes smaller and smaller and hence it slowly starts emerging as a square wave. Also since the function is asymmetric, the positive values and the negative values remain with their signs. When the same is tried with Cosine, the result will be a constant value between 0.7 and 0.8. It is understandable why this happens because, it converts all the negatives into positive in the very first nesting.


You can get the Pdf of the program and Images here – Sine Nest Program

This PDF will be better in one way that you can zoom in and see the closeup of the plots without losing their quality.(There will be no pixellation like for .jpg or .png)



















4 thoughts on “From Sine to Square

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s