This article or section is currently undergoing major changes by Ideapad-320 (talk | contribs) and may be incomplete. Please avoid largely modifying this page's contents until this template has been removed. (October 2023) |
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 or the area of the base |
h | Height. Perpendicular to the base. |
r | Radius. In a torus, the radius of the tube. |
R | The radius of the hole, plus the radius of the tube (major radius) |
l | Slant or side Length |
n | Number of sides/faces |
a, b, c | Side lengths of a triangle |
A, B, C | Angle measures of a triangle (angle A is opposite side a) |
a,b,... | Side lengths of a shape |
angle or | Some Angle |
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.141592653589793 |
Golden Ratio (φ) | 1.618033988749895 |
Area and Surface Area
Cone
where
(((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
((((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 of a Cone
(((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
Known length of one side and height (perpendicular to that side)
((base) * (height))
Known length of both sides and any angle
(((a)*(b))*([sin v] of (angle))
Trapezoid/Trapezium
Here, "a" and "b" are the two parallel sides of the trapezoid, and "h" is the distance between them:
((((a) + (b)) / (2)) * (h))
Here, "a" and "b" are the two parallel sides of the trapezoid, "l" is one other side, and "angle" or "θ" is an angle between that side and either of the two parallel sides:
((((a) + (b)) / (2)) * ((l) * ([sin v] of (angle))))
Regular N-gon
Inscribed circle radius(center of polygon to middle of side):r
Circumscribed circle radius(center of polygon to corner):c
Side Length: l
Side Count: n
Known inscribed circle radius, side length, and side count:
(((r)*((l)*(n)))/(2))
Known inscribed circle radius and side count:
(((r)*(r))*([tan v] of ((360)/((2)*(n)))))
Known side length and count:
(((n)*(l))/(([tan v] of ((180) / (n)))*(4)))
Sphere
((pi) * ((4) * ((r) * (r))))
Sector of a Circle
For angle in degrees:
(((central angle) / (360)) * ((pi) * ((r) * (r))))
For angle in radians:
(((central angle) / (2)) * ((r) * (r)))
Square-based Pyramid
b is the base size, l is the height of the triangles on the side.
(((2) * ((b) * (l))) + ((b) * (b)))
Calculating Slant length:
([sqrt v] of (((b/2)*(b/2))+((h)*(h))))
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 (b and c) and one included angle(between the two sides) (a), the following formula may be used to find the area:
(((b) * (c)) * (([sin v] of (a)) / (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:
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)))))
Square-base 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:
- (the hyperbolic sine)
- (the hyperbolic cosine)
- (the hyperbolic tangent)
- (the hyperbolic cotangent)
- (the hyperbolic secant)
- (the hyperbolic cosecant), also,
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
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 calculated 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):
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: , 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 , and are constants:
In this case, the solution of this system is:
:
((((c1) * (b2)) - ((c2) * (b1))) / (((a1) * (b2)) - ((a2) * (b1))))
:
((((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! |
Quadratic Formula
For the equation , the solutions to are described in the quadratic formula below:
The equation above will give the two possible values of "":
((((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 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 "":
(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 is given to be .
Warning: . If is plugged in, an invalid solution will be received. As a rule in math, the "" coefficient in Quadratic equations should not have a value of .
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.
Higher Order Polynomials
To solve for the roots of other polynomials of higher degree, explicit equations for the solutions are extremely complex or do not exist, 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 , a^^b, or a[4]b, where a is the base and b is the height. Tetration is computed by the following procedure: , so for example, , which is 65536. It can be written in Scratch as:
set [result v] to (1) repeat (b) set [temp v] to (1) repeat (result) set [temp v] to ((temp)*(a)) end set [result v] to (temp) end
where, (result)
is your answer.
Note: | This method only works for whole values of b. |
nth root
The nth root of a number x is the number of times which a number has to be multiplied by itself n times to get x. For example, the cube root of 64 (written ) is , because .
A universal formula for finding the nth root of a number can be found using logarithms. To find the n root of x, use the following formula:
([e ^ v] of (([ln v] of (x)) / (n)))
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.
The two-point center finite difference equation described below is a simple method that can be used to estimate the derivative of a function.
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, a looping 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:
If controlled precision in the solution is desired, the number of subintervals can be increased until the solution converges to an acceptable precision tolerance. However in the example below, the integral is evaluated once with a set 500 subintervals which is arbitrarily picked for the integration interval chosen.
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