This page lists ways in which **important mathematical functions and formulas can be expressed using the Scratch Operators Blocks**.

## Conventional Measurement Abbreviations

Abbreviation | Measurement |
---|---|

b | Length of a side of the base, plus the area of the base |

h | Height |

r | Radius. In a torus, the radius of the tube. |

l | Slant Height |

a, b, c | Side lengths of a triangle |

A, B, C | Angle measures of a triangle (angle A is opposite side a) |

R | The radius of the hole, plus the radius of the tube |

m | Slope of a line segment |

### Common Constants

Note: | Since pi is an irrational number, it is up to the user to decide the number of decimal places it is rounded to. 3.14, 3.142 or 3.1416 are some conventional roundings. 355/113 is a common estimation of pi provided as a fraction. |

Constant | Approximate Value |
---|---|

pi (π) |
3.141592654 |

Golden Ratio (φ) |
1.618033989 |

## Area and Surface Area

### Cone

(((pi) * ((r) * (r))) + ((pi) * ((r) * (l))))

### Cylinder

((((2) * (pi)) * ((r) * (r))) + ((2) * ((pi) * ((r) * (h)))))

### Ellipse

((pi) * ((a) * (b)))

#### Circle

Also known as a degenerate ellipse, this is the area of a circle:

((pi) * ((r) * (r)))

### Frustum

(((pi) * ((r of bottom circle) + (r of top circle))) * ( [sqrt v] of ((((r of bottom circle) - (r of top circle)) * ((r of bottom circle) - (r of top circle))) + ((h) * (h)))

### Möbius Strip

(((4) * (pi)) * ((r) * (w)))

w is the distance between the edges, and r is half the distance between two points on opposite sides of the strip

This formula derives from the fact that the Möbius strip is one-sided (non-orientable).

### Quadrilateral

#### Kite

Here, "d1" and "d2" are the lengths of the two diagonals of the kite:

(((d1) * (d2)) / (2))

#### Parallelogram

((base) * (height))

#### Trapezoid/Trapezium

Here, "a" and "b" are the two parallel sides of the trapezoid:

((((a) + (b)) * (h)) / (2))

### Regular N-gon

Here, "n" is the number of sides in the polygon:

(((n) * ((side) * ((1) / ([tan v] of ((pi) / (n)))))) / (4))

### Sphere

((pi) * ((4) * ((r) * (r))))

### Sector of a Circle

This will find the answer in degrees:

(((central angle) / (360)) * ((pi) * ((r) * (r))))

### Square-based Pyramid

(((2) * ((b) * (l))) + ((b) * (l)))

### Torus

(((4) * ((pi) * (R))) * ((pi) * (r)))

Note that this formula is just the product of the circumferences of two circles with radii R and r.

### Triangle

There are numerous ways to calculate the area of a triangle:

The most common formula is **Area = (base/2)*height** (see below) where **base** is any side and **height** is the length of a line segment perpendicular to the base, ending at the vertex opposite the base. In Scratch, this translates to:

(((base) / (2)) * (height))

If two side lengths are known (a and b) and one angle (C), the following formula may be used to find the area:

(((a) * (b)) * (([sin v] of (C)) / (2)))

If one side length is known (c) and two angles (A and B), the following formula may be used to find the area:

((((c) * (c)) * (([sin v] of (A)) * ([sin v] of (B)))) / ((-2) * ([sin v] of ((A) + (B)))))

If 3 side lengths are known (a, b, c), the following formula, known as Heron's formula, may be used to find the area:

set [s v] to ((((a) + (b)) + (c)) / (2)) //s is called the semiperimeter; it is half the perimeter set [area v] to ([sqrt v] of ((((s) * ((s) - (a))) * ((s) - (b))) * ((s) - (c))))

If the triangle's vertices have integer coordinates, it is best to use Pick's theorem or Heron's formula (above) (using the distance formula to find the lengths of the triangle's sides).

If one knows the radius of the triangle's incircle and its semiperimeter (half the perimeter), we can use this formula:

((incircle radius) * (semiperimeter))

### Pick's Theorem

Pick's theorem says that the area of a polygon with integer coordinates can be determined by:

*A=½a+b-1*

In the equation, a is the number of integer coordinates on the perimeter of the shape, and b is the number of coordinates inside the figure. In Scratch this translates to:

((((a) / (2)) + (b)) - (1))

## Volume

### Cone

(((pi) * (((r) * (r)) * (h))) / (3))

### Cylinder

((pi) * ((r) * ((r) * (h))))

### Frustum

((((pi) * (h)) / (3)) * (((r of bottom circle) * (r of bottom circle)) + (((r of bottom circle) * (r of top circle)) + ((r of top circle) * (r of top circle)))))

### Pyramid

(((b) * (h)) / (3))

### Regular Tetrahedron

Here, "b" is the length of each edge:

((([sqrt v] of (2)) / (12)) * (((b) * (b)) * (b)))

### Sphere

(((4) / (3)) * ((pi) * ((r) * ((r) * (r)))))

### Torus

(((2) * ((pi) * (R))) * ((pi) * ((r) * (r))))

Note that this formula is just the product of the area of a circle with radius r and the circumference of a circle with radius R. It is only correct when R ≥ r.

## Law of Sines

### Sides

This will give an answer for side "a":

(((b) * ( [sin v] of (A))) / ( [sin v] of (B)))

### Angles

This will give an answer for angle "A":

( [asin v] of (((a) * ( [sin v] of (B))) / (b)))

## Law of Cosines

### Sides

This will give an answer for side "c":

( [sqrt v] of ((((a) * (a)) + ((b) * (b))) - ((2) * ((a) * ((b) * ( [cos v] of (C)))))))

### Angles

This will give an answer for angle "C":

( [acos v] of ((((a) * (a)) + (((b) * (b)) - ((c) * (c)))) / ((2) * ((a) * (b)))))

## Hyperbolic Functions

There are six main hyperbolic functions:

- sinh
*x*(the hyperbolic sine) - cosh
*x*(the hyperbolic cosine) - tanh
*x*(the hyperbolic tangent) - coth
*x*(the hyperbolic cotangent) - sech
*x*(the hyperbolic secant) - csch
*x*(the hyperbolic cosecant), also, cosech*x*

### sinh and cosh

Just as in normal trigonometry, the sinh and cosh functions are the fundamental units. They can be replicated with

((([e^ v] of (x)) / (2)) - ((1) / ((2) * ([e^ v] of ((0) - (x))))))

and

((([e^ v] of (x)) / (2)) + ((1) / ((2) * ([e^ v] of ((0) - (x))))))

respectively.

### sech and csch

In normal trigonometry, the secant and cosecant are given by 1/cos*x* and 1/sin*x*, respectively. The hyperbolic secant and cosecant can be expressed in the same way: sech*x* = 1/cosh*x*, or:

((2) / (([e^ v] of (x)) + ([e^ v] of ((0) - (x)))))

and csch*x* = 1/sinh*x*:

((2) / (([e^ v] of (x)) - ([e^ v] of ((0) - (x)))))

### tanh and coth

The tangent function is expressed by sin*x*/cos*x*. Similarly, the hyperbolic tangent is expressed by sinh*x*/cosh*x*. In Scratch, that is:

((([e^ v] of (x)) - ([e^ v] of ((0) - (x)))) / (([e^ v] of (x)) + ([e^ v] of ((0) - (x)))))

Similarly, the cotangent is 1/tan*x*, so the hyperbolic cotangent is:

((([e^ v] of (x)) + ([e^ v] of ((0) - (x)))) / (([e^ v] of (x)) - ([e^ v] of ((0) - (x)))))

## Pythagorean Theorem

This will give the length of the hypotenuse:

( [sqrt v] of (((a) * (a)) + ((b) * (b))))

And these will give the length of sides "a" and "b" (assuming the hypotenuse is "c"):

( [sqrt v] of (((c) * (c)) - ((a) * (a)))) ( [sqrt v] of (((c) * (c)) - ((b) * (b))))

### Distance between two Points

The distance between two points can be calculated by using the Distance Formula which is derived from the Pythagorean Theorem, The following is a ScratchBlocks representation of the distance formula. It has been broken into three lines. The distance formula is as follows:

set [a v] to ( ((x2) - (x1) ) * ( (x2) - (x1) )) set [b v] to (((y2) - (y1)) * ((y2) - (y1))) set [distance v] to ([sqrt v] of ((a) + (b)))

### Distance between two Points in Any Dimension

The distance between two points on a 2D coordinate plane is caluclated using the formula above. However, this formula can be further expanded to any dimension. For example, the distance between any two 3D points is as follows:

set [a v] to (((x2) - (x1)) * ((x2) - (x1))) set [b v] to (((y2) - (y1)) * ((y2) - (y1))) set [c v] to (((z2) - (z1)) * ((z2) - (z1))) set [distance v] to ([sqrt v] of (((a) + (b)) + (c)))

Simply add further squares of delta coordinates for each dimension.

### Inverse Pythagorean Formula

If a,b are the lengths of the two shortest legs of a right triangle and c is its altitude then:

To find c (the altitude):

( [sqrt v] of ((1)/(((1)/((a)*(a)))+((1)/((b)*(b))))))

### Midpoint between Two Points

The coordinate of the midpoint of a line is found using the midpoint formula, which is as follows (the equation gives a *(x,y)* paired coordinate):

*midpointcoord = ((x2+x1/2), (y2+y1/2))*

set [x v] to (((x2) + (x1)) / (2)) set [y v] to (((y2) + (y1)) / (2))

## Roots and Linear Equations

### Linear Equations

A linear equation is an algebraic equation in which each term is either a constant or the product of a constant and a single variable.

Here is an example of a linear equation: 2x+4=14, for which in standard form would look like this:

<(((a) * (x)) + (b)) = (c)>

In scratch, we could solve for x using the following script:

(((c) - (b)) / (a))

Inputting the values of a, b and c scratch could solve the equation with ease.

#### System of 2 Linear Equations

For two equations in the system we have:

Therefore:

x1:

((((c2,2) * (y1)) - ((c2,1) * (y2))) / (((c1,1) * (c2,2)) - ((c2,1) * (c1,2))))

x2:

((((c1,1) * (y2)) - ((c1,2) * (y1))) / (((c1,1) * (c2,2)) - ((c2,1) * (c1,2))))

### Quadratic Formula

These will give the two possible values of "x":

((((b) * (-1)) + ( [sqrt v] of (((b) * (b)) - ((4) * ((a) * (c)))))) / ((2) * (a))) ((((b) * (-1)) - ( [sqrt v] of (((b) * (b)) - ((4) * ((a) * (c)))))) / ((2) * (a)))

Caution: | If b^{2}-4ac is negative, a script error will happen. Here's how to avoid the error: |

In this case these will give you the two possible values of "x":

(join (((b) * (-1)) / ( (2) * (a))) (join (join [+] (([sqrt v] of ((((b) * (b)) - ((4) * ((a) * (c)))) * (-1))) / ((2) * (a)))) [i])) (join (((b) * (-1)) / ( (2) * (a))) (join (join [-] (([sqrt v] of ((((b) * (b)) - ((4) * ((a) * (c)))) * (-1))) / ((2) * (a)))) [i]))

...where i^{2} is given to be -1.

**Warning**: a≠0. If a=0 is plugged in, an invalid solution will be recieved. As a rule in math, the "a" coefficient in Quadratic equations should not have a value of 0.

### Cubic Formula

Note: | This formula may deal with complex numbers that cannot be handled by Scratch. If it were to be worked out properly, the complex numbers would cancel out, but unfortunately here a lot of problems will be unsolvable because Scratch does not support complex roots. |

This will give the answer to the equation:

The analogous formula for this is:

In Scratch this translates to this (scroll the equation to see more):

((([10 ^ v] of (((1) / (3)) * ([log v] of ((((((0) - (((b) * (b)) * (b))) / ((27) * (((a) * (a)) * (a)))) + (((b) * (c)) / ((6) * ((a) * (a))))) - ((d) / ((2) * (a)))) + ([sqrt v] of (((((((0) - (((b) * (b)) * (b))) / ((27) * (((a) * (a)) * (a)))) + (((b) * (c)) / ((6) * ((a) * (a))))) - ((d) / ((2) * (a)))) * (((((0) - (((b) * (b)) * (b))) / ((27) * (((a) * (a)) * (a)))) + (((b) * (c)) / ((6) * ((a) * (a))))) - ((d) / ((2) * (a))))) + ((((c) / ((3) * (a))) - (((b) * (b)) / ((9) * ((a) * (a))))) * ((((c) / ((3) * (a))) - (((b) * (b)) / ((9) * ((a) * (a))))) * (((c) / ((3) * (a))) - (((b) * (b)) / ((9) * ((a) * (a))))))))))))) + ([10 ^ v] of (((1) / (3)) * ([log v] of ((((((0) - (((b) * (b)) * (b))) / ((27) * (((a) * (a)) * (a)))) + (((b) * (c)) / ((6) * ((a) * (a))))) - ((d) / ((2) * (a)))) - ([sqrt v] of (((((((0) - (((b) * (b)) * (b))) / ((27) * (((a) * (a)) * (a)))) + (((b) * (c)) / ((6) * ((a) * (a))))) - ((d) / ((2) * (a)))) * (((((0) - (((b) * (b)) * (b))) / ((27) * (((a) * (a)) * (a)))) + (((b) * (c)) / ((6) * ((a) * (a))))) - ((d) / ((2) * (a))))) + ((((c) / ((3) * (a))) - (((b) * (b)) / ((9) * ((a) * (a))))) * ((((c) / ((3) * (a))) - (((b) * (b)) / ((9) * ((a) * (a))))) * (((c) / ((3) * (a))) - (((b) * (b)) / ((9) * ((a) * (a)))))))))))))) - ((b) / ((3) * (a)))

There are ways to work around Scratch's incapability of calculating complex roots with the aid of the Trigonometric Form of Solutions.

## Powers and Roots

### Tetration

Tetration is iterated exponentiation. It is written as ^{b}*a*, *a*^^*b*, or *a*[4]*b*, where *a* is the base and *b* is the height. Tetration is computed by the following procedure: ^{b}*a*=*a*^{a⋰a}, where there are *b* *a*s So ^{4}2 is 2^{222}, which is 65536. It can be written in Scratch as:

repeat ((b) - (1)) repeat (a) set [a^^b v] to ((a^^b) * (a)) end end

Here, "a^^b" is your answer. This method only works for whole heights, however, but there is no known algorithm for calculating the value for non-integral heights.

### xth root

The *x*th root of a number *n* is number that must be multiplied *x* times to get *n*. For example, the cube root (3rd root) of 2 (written ∛2) is about 1.2599, because 1.2599*1.2599*1.2599≈2.

A universal formula for finding the * x*th root of a number can be found using logarithms. To find the

*root of*

**x***, use the following formula:*

**n**([e ^ v] of (([ln v] of (n)) / (x)))

This is based on a method of using exponents and logarithms, which Scratch supports, to find powers, which Scratch does not directly support (see solving exponents using the logarithmic method).

#### Fourth root, Eighth root, etc.

The fourth root of a number is the number that, when squared twice, gives the original number. The fourth root of 81, for example, is 3. Therefore, it can be replicated in Scratch with this script:

([sqrt v] of ([sqrt v] of (a)))//a is the input number

Similarly, the eighth root of a number is the number, when squared three times, gives the original number. The eighth root of 256, for example, is 2. It can be replicated with this script:

([sqrt v] of ([sqrt v] of ([sqrt v] of (a))))

In general, to get the 2^{n}th root of a number, nest *n* () of () blocks together, and then enter the desired value, or insert a variable.

## Derivatives and Integrals

Derivative and integral estimation is important in advanced simulation projects.

The derivative of a function is defined to be:

Center finite difference can be used to estimate the derivative of a function.

when green flag clicked set [h v] to [0.001] //h must be small enough but not too small to introduce roundoff errors. set [x v] to [2] set [f(x-h) v] to (([e ^ v] of ((x) - (h))) / ([sqrt v] of ((x) - (h)))) // Your function here and below. set [f(x+h) v] to (([e ^ v] of ((x) + (h))) / ([sqrt v] of ((x) + (h)))) set [derivative v] to (((f\(x+h\)) - (f\(x-h\))) / ((2) * (h)))

Integrals can be evaluated using trapezoidal approximation:

when green flag clicked set [n v] to [500] //n (# subintervals) must be large enough but not too large to introduce roundoff errors. set [b v] to [2] //Upper bound of integral set [a v] to [-2] //Lower bound of integral set [i v] to [0] set [integral v] to [0] repeat ((n) + (1)) set [x v] to (((((b) - (a)) / (n)) * (i)) + (a)) if <<(i) = [0]> or <(i) = (n)>> then change [integral v] by ((((b) - (a)) / ((2) * (n))) * ([e ^ v] of ((-1) * ((x) * (x))))) //Function appears here and below. else change [integral v] by ((2) * ((((b) - (a)) / ((2) * (n))) * ([e ^ v] of ((-1) * ((x) * (x)))))) end change [i v] by (1) end