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
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

$A=\pi r^{2}+\pi rl$ where $l={\sqrt {h^{2}+r^{2}}}$ (((pi) * ((r) * (r))) + ((pi) * ((r) * (l)))) // when the slant height is known

(((pi) * ((r) * (r))) + ((pi) * ((r) * ([sqrt v] of (((r) * (r)) + ((h) * (h))))))) // when the slant height is not known


### Cylinder

$A=2\pi r^{2}+2\pi rh$ ((((2) * (pi)) * ((r) * (r))) + ((2) * ((pi) * ((r) * (h)))))


### Ellipse

$A=\pi ab$ ((pi) * ((a) * (b)))


#### Circle

$A=\pi r^{2}$ 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).

#### 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={\frac {1}{2}}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

${\frac {a}{\sin A}}={\frac {b}{\sin B}}={\frac {c}{\sin C}}$ ### 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

$c^{2}=a^{2}+b^{2}-2ab\cos(C)$ ### 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:

• sinhx (the hyperbolic sine)
• coshx (the hyperbolic cosine)
• tanhx (the hyperbolic tangent)
• cothx (the hyperbolic cotangent)
• sechx (the hyperbolic secant)
• cschx (the hyperbolic cosecant), also, cosechx

### 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/cosx and 1/sinx, respectively. The hyperbolic secant and cosecant can be expressed in the same way: sechx = 1/coshx, or:

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

and cschx = 1/sinhx:

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

### tanh and coth

The tangent function is expressed by sinx/cosx. Similarly, the hyperbolic tangent is expressed by sinhx/coshx. 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/tanx, 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

$a^{2}+b^{2}=c^{2}$ 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:

$d={\sqrt {(x_{2}-x_{1})^{2}+(y_{2}-y_{1})^{2}}}$ 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:

$d={\sqrt {(x_{2}-x_{1})^{2}+(y_{2}-y_{1})^{2}+(z_{2}-z_{1})^{2}}}$ 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:

${\frac {1}{a^{2}}}+{\frac {1}{b^{2}}}={\frac {1}{c^{2}}}$ 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): ${\textrm {midpoint}}=({\frac {x_{1}+x_{2}}{2}},{\frac {y_{1}+y_{2}}{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))


After inputting the values of a, b and c; Scratch could solve the equation with ease.

#### System of Two Linear Equations

A system of two linear equations may follow the form below where variables $a$ , $b$ and $c$ are constants:

${\begin{cases}a_{1}x+b_{1}y=c_{1}\\a_{2}x+b_{2}y=c_{2}\end{cases}}$ In this case, the solution of this system is:

$x={\frac {c_{1}b_{2}-c_{2}b_{1}}{a_{1}b_{2}-a_{2}b_{1}}},y={\frac {a_{1}c_{2}-a_{2}c_{1}}{a_{1}b_{2}-a_{2}b_{1}}}$ $x$ :

((((c1) * (b2)) - ((c2) * (b1))) / (((a1) * (b2)) - ((a2) * (b1))))

$y$ :

((((a1) * (c2)) - ((a2) * (c1))) / (((a1) * (b2)) - ((a2) * (b1)))) Note: There are cases where infinite or no solutions exist to the system of equations. The above functions will evaluate to undefined values when this occurs!

For the equation $ax^{2}+bx+c=0$ , the solutions to $x$ are described in the quadratic formula below:

$x={\frac {-b\pm {\sqrt {b^{2}-4ac}}}{2a}}$ The equation above 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\neq 0$ . If $a=0$ is plugged in, an invalid solution will be received. 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:

$ax^{3}+bx^{2}+cx+d=0$ 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.

### Higher Order Polynomials

To solve for the roots of other polynomials of higher degree, explicit equations for the solutions are extremily complex, and the incorporation of such equations into scratch are infeasible. It is recommended to solve polynomials of order three and higher with root-finding algorithms such as bracketing or iterative methods.

## Powers and Roots

### Tetration

Tetration is iterated exponentiation. It is written as ba, a^^b, or ab, where a is the base and b is the height. Tetration is computed by the following procedure: ba=aaa, where there are b as So 42 is 2222, 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 xth 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 xth root of a number can be found using logarithms. To find the x root of n, use the following formula:

([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).

## Derivatives and Integrals

Derivative and integral estimation is important in advanced simulation projects. The derivative of a function is defined below.

$f'(x)=\lim _{h\to 0}{\frac {f(x+h)-f(x)}{h}}$ The two-point center finite difference equation described below is a simple method that can be used to estimate the derivative of a function.

$f'(x)\approx {\frac {f(x+h)-f(x-h)}{2h}},h\approx 0$ For some applications, setting h to a carefully selected small value and evaluating the above equation once is adequate.

If greater control of the solution's precision is required, an iterative method can be employed. An example of such a method is produced below. It first requires h to be set to a carefully selected small number (notated as h0 below). The derivative is calculated through the center finite difference equation. h is subsequently halved and the derivative is recalculated. This halving process is repeated until consecutive derivative calculations stabilize towards a single number. This stabilization, or convergence, is achieved when the relative error between the most recent and the second most-recent calculated derivative is lower than a set tolerance (in the script below, it is set at 0.01, or 1%). Caution: Convergence does not guarantee accuracy! If h0 is not carefully selected based on the nature of the function, this method may converge towards an incorrect answer! Always consider the nature of the function before selecting h0 (by visualizing the function, for example).

when green flag clicked
delete all of [f'(x) values v]
set [h v] to (1) //This is h0, the starting h size used to evaluate the derivative.
set [x v] to (2) //x value to evaluate the derivative.
set [i v] to (1)
forever
delete all of [f(x) values v]
evaluate function at ((x)-(h)):: custom // This and the following block evaluates the function at x-h and x+h, then stores the two values in the "f(x) values" list.
evaluate function at ((x)+(h)):: custom
add (((item (2) of [f(x) values v]) - (item (1) of [f(x) values v])) / ((2)*(h))) to [f'(x) values v] // The center finite difference used here calculates the derivative and stores it in the "f'(x) values" list.
if <(length of [f'(x) values v]) > (1)> then
set [relative error v] to ([abs v] of (((item (length of [f'(x) values v]) of [f'(x) values v]) - (item ((length of [f'(x) values v]) - (1)) of [f'(x) values v])) / (item (length of [f'(x) values v]) of [f'(x) values v])))
if <(relative error) < (0.01)> then // The lower the relative error tolerance (here at 0.01) is set, the more precise your answer will be.
say (join [Derivative at x = ](join (x)(join [is](item (length of [f'(x) values v]) of [f'(x) values v])))) for (2) seconds
stop [all v]
end
end
if <(i) > (1000)> then
say [Iteration stopped due to nonconvergence.] for (2) seconds // After 1000 iterations, if the  relative error still does not achieve our tolerance value, the program will stop.
stop [all v]
end
set [h v] to ((h) / (2)) // If another iteration is needed, halve h and try again.
change [i v] by (1)
end

define evaluate function at (value)
add (([e ^ v] of (value::custom))/(value::custom)) to [f(x) values v] // This is the function of which you want to evaluate the derivative.


Integrals can be evaluated using trapezoidal approximation:

$\int _{a}^{b}f(x)\,dx\approx {\frac {b-a}{2n}}\left(f(x_{0})+f(x_{n})+2\sum _{i=1}^{n-1}f(x_{i})\right)$ when green flag clicked
set [n v] to  //n (# subintervals) must be large enough but not too large to introduce roundoff errors.
set [b v] to  //Upper bound of integral
set [a v] to [-2] //Lower bound of integral
set [i v] to 
set [integral v] to 
repeat ((n) + (1))
set [x v] to (((((b) - (a)) / (n)) * (i)) + (a))
if <<(i) = > 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

Cookies help us deliver our services. By using our services, you agree to our use of cookies.