Sinus Function in Squeak

advertisement
Red used for footnotes.
Sine Function in Squeak
Discussion and Detailed instructions for creating on screen (for any
given angle alfa)*:
1. An image of the angle in unit circle
2. Plot the sine function from zero to alfa
3. Compute the (approximate) value of sin alfa
*(Please note: we don’t use “alpha” because in squeak alpha is a term kept for
something else)
Squeak has no ready-made tiles for Trigonometric functions. But squeak has all the
features we need to create them from their basic definitions. Plotting the functions on the
screen and computing approximations for the functions values gives a concrete meaning
to the definitions. Incorporate different activities and stronger visual element into class
routine.
Sine function definition used in this project:
Sin A is the value of y that the moving hand of the angle draws on a unit circle.
Prerequisite:
In Squeak
Creating objects; “basic”,” Geometry”, and “Pen use” menu items; Numerical variables;
Simple tests for comparing numbers; Familiarity with “center of rotation”; Scripting a
“stop” for a script.
In geometry
(Can do without) When an object executes the script: Forward by A; Turn by B, with
“pen down”, all the edges of the segments of its trail are on one circle. Each segment
defines an arc of B degrees on this circle. (In other words: The central angle opposite
each segment is of size B.) See picture 1.
. These statements can be proved by using congruent triangles rules. They explain how
the script: Forward by A; Turn by 90, creates a square with sides length A, and Forward
by A, Turn by 120; creates a triangle with sides length A. *(For detailed discussion and
proofs see “Squeak: Geometry of Forward and Turn”)
Picture1
Using this fact we can draw an approximation of a circle by using small segments with
small angles. The smaller the angle is, the closer we get to a “real” circle.
We strongly suggest working in a playfield. This let us use numeric values without the
fear of distortions that different screen sizes might cause.
Instructions start here
Grab a playfield and size its length to be 350; width 450.
The angle’s elements:
A segment (angleline) for the non moving hand of the angle. A needle (angleneedle)
for the moving hand of the angle. We trace the moving edge of the needle by adding a
“dot” (angletracer) with “pen down” to the edge of needle. This “dot” y-coordinate is
the same as the y-coordinate of the edge of the needle.
The angletracer script is: forward by 1; turn by -1. (The negative measure of turning
is in order to keep with the counter clockwise way we measure angles on a unit
circle.) Doing this script the angletracer draws a circle with radius 57.7943. See pictures
Elements of angle and Sine plotter: unassembled (left), assembled (right)
Creating the angle elements:
Create angleneedle by drawing a segment. Better use the smallest pen size.
Using the geometry menu, adjust the size of its “width” (common language: length)
to 62 .
Move its rotating center (Drag it holding the “shift” key down) to its left edge.
Create angleline by painting a segment, a little longer then the angleneedle. Use
different color to distinguish it from the other segment.
Create angletracer by painting a small dot.
Assembling the angle elements:
Create a script to assemble, and reset, angle’s elements in the playfield..
Name the script: “resetangle”.
Bring in the following tiles:
x
Set angletracer
160
Set angleneedle
100
Set angleline
100+ half its width
y
230
230
230
heading
0
0
0
Get a button to fire the resetangle script.
Create angle alfa
Create a numeric variable, name it “currentangle”. Add to the reset button script a tile
that set the “currenangle” value to 0.
Create another numeric variable, name it “alfa”. Create a detailed watcher for it. Here
you put (type) the value of the angle you wish to create.
Assemble the following script (for angletracer) and name it “anglecreation”:
Angletracer forward by1
Angletracer turn by -1
Angleneedle turn by -1
Currentangle increase by 1
Test: currentangle>= alfa
Yes angletracer stopscript anglecreation
No
Plot a Sine Graph
Sine plotter elements:
Paint a horizontal segment for the line “sin alfa = 0”. (Choose different color from those
of the previous 2 segments). Keep.
Using second bigger size pen, paint a dot. We use it as plotting dot (sinplotter). Make
sure you can distinguish it from angletracer, by color or by design.
Open viewer for sinplotter and make pen down true.
Assemble Sine plotter:
Create, assemble and reset script for the sine graph, name it “resetsingraph”
The script as follows:
x
y
sinplotter
80
100
singraphline
80
100
Get a button to fire the resetsingraph script.
Script for Sine plotter:
Assemble the following script, name it “sinusgraph”
Sinplotter yangletracer y + singraphline y – angleline y) *
Sinplotter x increase by 1
If you want it to stop ploting the graph when angle creation stops, add this test:
Test: currentangle>= alfa
Yes sinplotter stop singraph
No
*
It makes more sense to write it like this:
Sinplotter y(angletracer y – angleline y) + singraphline y
But in Squeak computing is done from right to left, and it has no parenthesis. Therefore
in order to subtract only the angleline y, we have to change the order of the script to:
Sinplotter yangletracer y + singraphline y – angleline y)
.
(In our example: sinplotter y(angleline y -230) +100 which should be rewrite as
sinplotter yangleline y +100 -230 consolidated to : angleplotter y-130)
Numeric value of Sin alfa
Insert value for alfa into its detailed watcher.
Take the value you used for plotting the sine function at the point alfa, and
“Normalize” it to the size of the unit circle. That means divide the difference between
angletracer y and angleline y by the length of the circle’s radius. (In our example divide
by 57.7943)
For the script:
Open viewer for sinplotter.
Create new numeric variable for value of sin alfa . Name it “sinalfa” Set it to take 4
decimal digits. Bring detailed watcher to make reading of the results easier.
Create new numeric variable that will hold the difference.
Name the variable “sinYneto”.
Assemble the following script:
sinYneto sintracer y – sinline y
sinalfa sinYneto / 57.7943
To execute the 3 scripts (“createangle”, ”sinplotter”, and “sinalfa”) simultaneously, grab
a “go; step; stop” button from supply and use it to manipulate the scripts.
The end







Challenge for students:
Add a script that will plot the cosine graph up to given angle (alfa).
Make a script that will compute the value of cosine of given angle (alfa).
What parts of the projects can we give up if all we want is to compute the
numerical value of sine alfa. Make a project that does just that (Gets a value for
alfa and compute its sine with minimal graphing on the screen)
Make a script that when given a numeric value between -1 and 1 will compute the
arcsine of this value.
Activities using the above Squeak program:*
Computes the radius of circles that conscribe the polygon created by the scripts:
(1) Forward by 200; turn by 60, (2) forward by 200; turn by 100. Compare to
results of computing by calculator.
Compute the inner radius (the inscribed circle) for the scripts: forward by 200;
turn by 62, forward by 200; turn by 102.
Find beta (s) when arcsine beta is 0.5555.


Computes the alfa needed for a polygon with segments’ length 10, and radius of
the conscribed circle is 100.
Compute the radius of the circle inscribed by the polygon made of 7 equal
segments of length 100 each.
* For advanced students: the general solution for the general form of the above
questions.
Associated Squeak Project: Math Sine Function
Download