Monday, November 11, 2024

A STEM Project: Comparing Three Different Methods of Coding a Sine Wave



Coding a Sine Wave in TurtleStitch Using the Sine Wave Equation y = a sin(Fx)



 Sashiko design called Hokusai

After coding the Sashiko design called Hokusai,  Cynthia Solomon challenged me to code a sine wave with just Arcright/Arcleft.  While the sine wave is not a circle, the arc commands can emulate the sine wave in small segments with adjustments made by a turn block.

In the following TurtleStitch program https://www.turtlestitch.org/run#cloud:Username=Elaine&ProjectName=Sine%20Wave%20Equation, I created three routines to make a sine wave. 

I used the following equation to code the sine wave.
  

Sine Wave Equation Code

y = a sin(Fx)

where:

a is the amplitude - makes the wave taller or shorter

F  is the frequency - how close the waves are together

This code creates an exact replica of a sine wave.


Arcright/Arcleft Code

This code uses arcright/arcleft to make the sine wave.

No calculations were made, just visual clues were used to code the sine wave. I coded this by using the original sine wave and then tracking the course of the curve with the turtle.  I used a different pen color to illustrate the arc's path. With trial and error, using different values of arcs and turns I created this sine wave.

The red curve is the calculated sine wave equation and the blue curve is the arcright/arcleft. You will notice the the blue curve is not an exact replica of the original because it is difficult to achieve the exact path with trial and error.


Turns and Moves Code

This code uses Turns and Moves to make the sine wave. I broke up the code into four sections so that you could see the receptive nature of the code.

No calculations were made, just visual clues were used to code this sine wave. I coded this by using the original sine wave and then tracking the course of the curve with the turtle.  I used a different pen color to illustrate its path. With trial and error, using different values of turns and moves, I created this sine wave.

The red curve is the calculated sine wave equation and the green curve is the turn/moves. Notice the green curve is not an exact replica of the coded "Sine Wave" because it is tedious to follow the path of the  original sine wave with trial and error.


Comparing the Three Methods of Coding a Sine Wave
While all three sine waves look similar, only one of these waves is exactly correct.  
The red sine wave is the coded sine wave equation. The blue and green sine waves are an approximation.  

It is also interesting to see the number of blocks that each method has used to create the sine wave.  The first column is the sine wave equation.  The second column is the arcright/arcleft blocks.  The third column onward is the turn and move blocks. 

By comparing the length of each code, you can see that the coded sine wave equation is half the length of the arcright/left code. The arcright/arcleft blocks are one-fourth the length of the turn/move blocks. The coded sine wave equation is one-eighth the length of the turn/move blocks. While the arcright/arcleft blocks simplify the code,  the coded equation is the shortest in length.

In conclusion, I think it is very interesting to compare and contrast the three coding methodologies.  It shows that there are multiple ways to achieve a goal when accuracy doesn't count. When precision is required, coding with equations is the simplest and most accurate method of coding in TurtleStitch.

Saturday, November 2, 2024

A STEM Project: Coding a SIne/Sinusoidal Wave In TurtleStitch

The pattern above is similar to a Sashiko design called Hokusai.
This pattern was created in TurtleStitch using different sine/sinusoidal wave orientations.

 
The difference between a sine wave and a sinusoidal wave is a sine wave oscillates in a smooth repetitive pattern centered at zero across the x-axis, with a regular phase and amplitude.
The sine wave equation is y = sin(x)

A sinusoidal wave has a general shape of a sine wave but it could either follow a sine or cosine function or any shifted version of these functions. The sinusoidal equation is y = a sin(Fx - p) + d

In the following TurtleStitch program, I created a program where you can compare different types of sine/sinusoidal waves.

Using the sinusoidal equation, y = a sin(Fx - p) + d, I created the Sashiko design called Hokusai.

Here is a link to my Hokusai TurtleStitch program.