## Thursday, 27 November 2014

### Mapping two range of numbers

Math has never been my favourite subject during my school days and I’ve always asked why do we need to learn Math. It would have changed my mind and become more interesting if students have been taught how Math can be applied in the real world.

Take for example a simple linear equation y = mx + C, given two points and we can find an equation of the line.

So what’s the big deal with finding an equation of a line and what can it practically do in the real world ??

Suppose you would like to plot a trigonometric function of “sine”, with an amplitude of 10,  over a horizontal range of 0 to 360 degrees, we would find the graph as tabled below

FIG 1

Note that the value on the X-axis is given the same value as the angle θ since we can assign the positive range of the X-Axis from 0 to 360. The Y-Axis represents the amplitude of the waveform.

However, plotting similar graph with Scratch is a different game altogether. In Scratch, the screen has a resolution of 480x360 pixels (FIG.2), such that the X-Axis range from -240 to 240 instead of 0 to 360 as seen from FIG.1

FIG. 2 Scratch screen

How do you plot the points on the graph across the screen where the X-Axis range from -240 to 240 with a complete cycle of 0 to 360 deg ??? In other words, we need to translate the whole range of 0-360 degrees proportionately to a range of -240 to 240 on the X-Axis as shown below,

FIG.3

FIG.3 denotes that if the value of the angle is 0, it is translated to -240 on the X-Axis, 90 deg to -120, 180 deg to 0 etc.

The translation of one range of numbers to another is often called “mapping” or “scalling”.

Let’s revisit the linear equation,

Note that the x and y in eqn(1) are not the points on the X-Y coordinates of the screen. It’s just an expression of two range of numbers where y represents a set of number and x represents another set.

In our case, we would like to map the angle (0-360 deg) to the full range on the X-Axis (-240 to 240). Hence y, being the desired output, represents the values that lies on the X-Axis and x represents the value of the angles (0 to 360 deg) to be mapped.

Re-writing  eqn(1) with its min and max value, we have,

The min and max values of the two sets of numbers are :

Let’s solve for m and C.

Subtracting eqn(2) from 3, we have

Re-arranging eqn(4) yields

Replacing m from eqn(5) in eqn(3), we can now solve the constant C.

Replacing m and C in eqn(1) :

Re-arranging the above equation,

Let’s rename the above symbols to make our life easier, i.e,

Renaming y in eqn(7) to x (the desired points on the X-Axis) and replace with the new symbols, we have

We can now plot the graph as shown below.

Below is the Scratch script to plot a “sine” function with and amplitude of 150.

A block is also created to map the above discussed two range of numbers.

And the graph on the Stage Area is show below

I’ll cover more on the “mapping” subject in my next post, that will involve Arduino and external sensors. Happy Scratching………