# How to calculate square/cubic root by hand?

Today, technologies make everything easy and fast for us. Sometimes, when we see the ancient marvels of engineering, we wonder: “How they designed this without computers”? And the answer is: “with knowledge, inspiration and hard work”.

However, being almost irreplaceable, computers makes us more and more adjective. Our brains become lazy and we are losing some knowledge and skills. Such is the manual calculation of root. It is so easy and elegant, so I will share it here for the sake of the good old math science.

## Square root

Lets calculate x = √a first.

By squaring both sides and moving a to the left side, we obtain the following equation:

x2a = 0

This is an equation of type f(x) = 0 and we can solve numerically it using the Newton’s method:

1. Start with initial guess: x0. It is good to be close to the solution, so you can use the nearest exact root.
2. Calculate the next approximation of the root by the equation: x1 = x0f(x0)/f′(x0)
3. Continue the iterations by using the previous result to estimate a new one:
xn+1 = xnf(xn)/f′(xn).
4. Stop when you reach the desired precision.

In the above formulas, f(x) is the function and f′(x) is the first derivative. In our case:

f(x) = x2a

f′(x) = 2x

If we substitute the above equations into the Newton’s iterative formula, we get:

xn+1 = xn − (xn2a)/2xn

xn+1 = (xn + a/xn)/2

Now, lets use it to calculate √a = ?, for a = 5.

We will start with an initial guess of x0 =√4 = 2.

1st iteration: x1 = (x0 + a/x0)/2 = (2 + 5/2)/2 = 2.25

2nd iteration: x2 = (x1 + a/x1)/2 = (2.25 + 5/2.25)/2 = 2.2361

3rd iteration: x3 = (x2 + a/x2)/2= (2.2361 + 5/2.2361)/2 = 2.2361

After the second iteration, we have 5 correct significant digits, which is quite sufficient for most cases.

## Cubic root

We will use the same approach, as follows:

f(x) = x3a

f′(x) = 3x2

xn+1 = xn − (xn3a)/3xn2

xn+1 = (2xn + a/xn2)/3

Lets see how it works for 3√7. Our initial guess is: x0 = 3√8 = 2.

1st iteration: x1 = (2x0 + a/x02)/3 = (2·2 + 7/22)/3 = 1.9167

2nd iteration: x2 = (2x1 + a/x12)/3 = (2·1.9167 + 7/1.91672)/3 = 1.9129

3rd iteration: x3 = (2x2 + a/x22)/3 = (2·1.9129 + 7/1.91292)/3 = 1.9129

Again, we managed to find it after two iterations. We can also use Calcpad to create a tiny program for that:

It is interesting that just after 4 iterations, the Newton’s method finds 16 correct significant digits which is the limit precision for most computers. We also calculated the root by three different methods which give the same result.

# What’s new in Calcpad, version 5.6.3?

Since version 5.6 was released, we made some little, but nice and useful improvements. They were driven mostly by our small and dedicated GitHub community. Some of the most important changes are listed below:

## 1. Autorun mode

When enabled, the results are refreshed automatically each time you edit the code and leave the current line. You can switch it on and off by the checkbox over the output window. When the “Autorun” is off, the program displays “Quick help” on the right as before. You can run calculations by pressing the respective button. However, new shortcuts were added to make that easier. Press F5 to run the calculations and F4 to compile to input form. Ctrl + Enter calculates the results and scrolls the output to match the current line.

## 2. Scroll enhancements

After recalculation, the scroll position of the output window is preserved and not moved to the beginning as before. However, if you get lost, you can double click on a line in the source code and the output will scroll to match. Alternatively, you can press Ctrl + Enter.

## 3. New symbols

The Symbol toolbar was extended with the full set of Greek letters. You can also change the case by pressing the button on the right.

There is also a shortcut to convert Latin to Greek letters. You can use it to insert Greek letters even faster. Just type the corresponding Latin letter and press Ctrl + G. The following conversion table is used:

Some special symbols were also added: ” ø “, “Ø”, ” ° “, “∡” and primes: ” ′ “, ” ″ “, ” ‴ “, ” ⁗ “. You can now use them in variable and function names. However, you cannot start a name with a number of prime symbol. Although primes look similar to quotes, they are different characters. So, there is no danger for the program to confuse them and start a comment.

## 4. Bugs and errors

There was a problem with recognition of inches (in) units in Complex mode that is now fixed. The “i” character was parsed as the imaginary symbol first and “n” dropped out as invalid unit.

Rewriting of non-metric (Imperial and USCS) units was also improved. For example, in the previous versions, lbf/in^2 was rewritten as kPa by default. Now, ksi is used instead. It is the same with other mechanical units.

Some rare bugs were luckily identified and fixed as well. For example, if you attempted plotting with identical limits, the program fell in infinite loop.

Debugging of Calcpad programs was made easier. Links to the respective lines in the source code were added to the error messages in the output. Also nice background highlighting was implemented.

# How to plot the Julia Set

In the previous post we discussed the Mandelbrot set. There is another beautiful algebraic fractal, called Julia set 𝓙. It is named after the French mathematician Gaston Julia, who has discovered. It is obtained by the same formula as the Mandelbrot Set:

zn+1 = zn2 + c

The difference is that z0 = x + yi is the current point (x, y) in the complex plane and c is a fixed complex number, while for the Mandelbrot set z0 = 0 and c = x + yi.

Both sets are closely related. There are a lot of different Julia sets, corresponding to different values of c. Their shapes and properties depend on where the point is located, related to the Mandelbrot set 𝓜. Inside 𝓜, the corresponding Julia set 𝓙 is connected. Outside 𝓜, it falls apart into a set of isolated points, called Fatou set 𝓕. Further away, it turns into Fatou dust – infinite number of points with zero area, similarly to the Cantor dust.

For simple plotting, you can use the following code:

```"Julia set
'Define the function
JuliaSet(z; c) = \$Repeat{z = z^2 + c @ i = 1 : n}
#hide
'Set the plotting parameters
PlotStep = 1','PlotWidth = 600','PlotHeight = 400
#show
'Plot for'c = -0.4 + 0.59i'and'n = 200'iterations
\$Map{abs(JuliaSet(x + 1i*y; c))) @ x = -1.5 : 1.5 & y = -1 : 1}
```

When you run it inside Calcpad, you will get the following result:

To obtain better and colorful images, we will use different approach this time. We will modify the JuliaSet function in a way to return directly the number of iterations for which the result overpasses 2. The source code is provided bellow:

```"Julia set
J(z; c; n) = abs(\$Repeat{z = if(abs(z) < 2; z^2 + c; 4) @ i = 1 : n})
JuliaSet(z; c) = \$Find{J(z; c; x) - 2 @ x = 1 : n}
#hide
PlotStep = 1','PlotWidth = 600','PlotHeight = 400','Precision = 10^-6
#show
'Plot for'c = -0.4 + 0.6i'and'n = 100'iterations
\$Map{JuliaSet(x + 1i*y; c)) @ x = -1.5 : 1.5 & y = -1 : 1}
```

It runs a little bit slower, but the result is worth the wait:

You can download Calcpad for free and experiment by yourself with different values of c. For example, the lightning image bellow is a special case of Julia set for c = i and is called “dendrite” fractal.

Most people consider math boring, but sometimes it can be really beautiful. Recently I found some unusual application of Calcpad – to draw nice pictures using math formulas. In general, you can plot functions of two variables using the \$Map command:

`\$Map{f(x;y) @ x = a : b & y = c : d}`

I notices that you can get nice effects using the following types of functions:

`f(x;y) = abs(cos(p1(x;y)) + cos(p2(x;y)) ...)`

It creates families of intersecting curves that from canyons with hills in between. If you select “none” for the color scale, it creates nice and smooth pictures with shadows only. Bellow are some examples:

f(x;y) = abs(cos(x^2/10) + cos(y^2/10))

f(x;y) = abs(cos(x + y/3) + cos(x*y/7))

f(x;y) = abs(cos(x – sin(y)) + cos(y – sin(x)))

f(x;y) = abs(cos(r(x;y)*e^(ξ*r(x;y)))*e^(-1.5*ξ*r(x;y)))

f(x;y) = abs(cos(r(x;y)) + cos(y))

f(x;y) = abs(cos(x) + cos((y/4)^2))

f(x;y) = abs(cos(2*r(x;y)) + cos(x) + cos(y))

f(x;y) = abs(cos(2*r(cos(x/2);cos(y/2))))

f(x;y) = abs(r(cos(x/2);cos(y/2)))

In the above equations, r(x;y) = sqr(x^2 + y^2).

# How units work

In this post, we will take a quick look inside Calcpad and see how units actually work. But first, let’s make a brief overview of the theory basis.

## Theory basis

There are only seven base units in the whole universe. They correspond to the seven physical dimensions:

• mass – kilogram (kg)
• length – meter (m)
• time – second (s)
• electric current – ampere (A)
• temperature – Kelvin (K)
• amount of substance – mole (mol)
• luminous intensity – candela (cd)

All other units are derived from the above ones, using the respective laws of physics. For example, forces are measured in Newtons (N). By definition, force is mass × acceleration, so 1 N = 1 kg·m/s2. Such units are called “composite”. They can be always decomposed to their constituent units.

Other method to derive units is by adding prefixes. I this way, we can obtain multiples of both base and composite units. For example,  kilonewtons – kN = 103·N, meganewtons – MN = kN = 106·N and so on. All possible prefixes are listed in the following tables:

Name deca hecto kilo mega giga tera peta exa zetta yotta
Symbol da h k M G T P E Z Y
Factor 101 102 103 106 109 1012 1015 1018 1021 1024
Name deci centi milli micro nano pico femto atto zepto yocto
Symbol d c m μ n p f a z y
Factor 10−1 10−2 10−3 10−6 10−9 10−12 10−15 10−18 10−21 10−24

However, not all prefixes are commonly used for all units, and that is why they are not included in Calcpad. Full list of the available units is provided in the user manual:

## Internal implementation

Any software implementation must allow the units to be compared, converted and scaled. For that purpose, we need to store the information for how units are composed by the base ones. We also need the corresponding scale factors, in order to allow for multiples. Then, we have to define all possible operations like comparison, addition, subtraction, multiplication, division and exponentiation.

To store all this information, we need a special type of data called “structure”. It can contain multiple values of different types in a single variable. In the case of units, we will include a name and two vectors. The first one will store the exponents for all seven physical dimensions (mass, length, time, current, temp, substance, luminosity) and the second one – the respective scaling factors:

`Unit = {name: "name", exponents: [p1, p2, p3, p4, p5, p6, p7], factors: [f1, f2, f3, f4, f5, f6, f7]}`

A base unit can include only one value of “1” for the respective dimension. All the rest must be exactly zero “0”. Composite units can have several nonzero values for the corresponding dimensions. Bellow are some examples of unit definitions:

Base units:

```Kilogram = {name: "kg", exponents: [1, 0, 0, 0, 0, 0, 0], factors: [103, 1, 1, 1, 1, 1, 1]} Meter = {name: "m", exponents: [0, 1, 0, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]} Second = {name: "s", exponents: [0, 0, 1, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]}```

Composite units:

```Newton = {name: "N", exponents: [1, 1, -2, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]} Pascal = {name: "Pa", exponents: [1, -1, -2, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]}```

Multiples:

```Ton = {name: "t", exponents: [1, 0, 0, 0, 0, 0, 0], factors: [106, 1, 1, 1, 1, 1, 1]} Millimeter = {name: "mm", exponents: [0, 1, 0, 0, 0, 0, 0], factors: [1, 10-3, 1, 1, 1, 1, 1]} Minute = {name: "min", exponents: [0, 0, 1, 0, 0, 0, 0], factors: [1, 1, 60, 1, 1, 1, 1]} Kilonewton = {name: "kN", exponents: [1, 1, -2, 0, 0, 0, 0], factors: [103, 1, 1, 1, 1, 1, 1]} Megapascal = {name: "MPa", exponents: [1, -1, -2, 0, 0, 0, 0], factors: [106, 1, 1, 1, 1, 1, 1]}```

## Units consistency

You can compare, add or subtract only values, which units are consistent. For example, “kg + m” does not make any sense, because both operands represent different physical dimensions. The expression “m2 + m3” does not make sense as well, because you are trying to add area to volume. However, “m + mm” is possible, because both represent length and we can convert them to each other. So, units are consistent only if they are of the same dimensions and with the same exponents. What we need to do for our structure, is to check if the exponents vectors match exactly.

Even if consistent, units are not necessarily equal, because they can have different scaling factors. So, we still cannot operate directly with the respective values. First, we have to convert all values to the same units. For example:

`3 dm + 2 cm = 3 dm + 0.2 dm = (3 + 0.2) dm = 3.2 dm`

Subtraction and comparison are performed in the same way as the addition. The following algorithm can be used:

1. Check if the units are consistent (compare the exponent vectors). If not, report an error. Otherwise, continue to step 2.
2. Compare the scale factors. If different, compute the conversion factor for the second unit and use it to multiply the second value.
3. Perform the operation and return the result with the units of the first value.

## Multiplication, division and exponentiation

Unlike the previous ones, these operations do not require the units to be consistent. Actually, this is the way we use to derive new units by combining the existing ones. In the software, these operations are performed by manipulation of the respective exponents as follows:

1. Multiplication is performed by addition of the exponents:
`m·m2 = m(1 + 2) = m3`
2. Division is equivalent to exponent subtraction:
`N/m2 = kg·m·s-2/m2 = kg·m(1 - 2)·s-2 = kg·m-1·s-2 = Pa`
3. Exponentiation is performed by multiplication of the old and new exponent:
`(m2)3 = m2·3 = m6`

The following algorithm is applied:

1. Check if there are different scale factors and compute the conversion factor.
2. Perform the required operation (multiplication, division …) with the units and obtain the resulting units.
3. Perform the operation with the values, apply the conversion factor and return the result in the calculated units.

## Target units

If you do not specify any target units, the result will be decomposed to the base units. For example:

`100N·10m = 1000 kg·m2/s2`

That is because the program cannot understand the physical meaning of your formulas. Sometimes, the result can be ambiguous and the program will not know what to do. For the above expression:

• If it is bending moment or torque, the result will be in Nm.
• If it is work or energy, the result will be in Joules.

In such cases, you have to specify the target units and if they are compatible, the result will be converted automatically. Always write the target units at the end of the expression, separated by a vertical bar “|”:

`500N·20m|kJ = 10kJ`

`2000N·5m|kNm = 10kNm`

# Calculating with units

Unit conversion is one of the most tedious jobs when doing calculations. Although not very difficult, it is repetitive and time consuming.

For example, lets calculate the stress in a steel beam. Assume that span length is in meters, section dimensions are in mm, geometrical properties are in cm2 (cm3, cm4), section forces are in kN (kNm) and stress is in MPa. That requires you to include a lot of conversion factors in your formulas. So, it would be very helpful if you could write the units next to the respective values and let the software do all the conversions for you.

The most recent version Calcpad Pro 3 supports physical units in calculations. For now, only metric units (SI compatible) are included, but more units will be added in the next versions.

You can attach units to every value in an expression and obtain the result in the specified target units. The target units are added at the end, separated by a vertical bar “|”.  , Open the online calculator and try the following expression by yourself:

`2m + 50 cm + 200 mm|dm`

Since the target units are “dm“, the answer is 27dm (not just adding 20 + 50 + 200 = 252). That is because all values are converted automatically to the same units before the addition: 20dm + 5dm + 2dm = 27dm. If you do not specify any target units, the units of the first operand will be used.

If you want to do simple unit conversion, just write the value, followed by the source units, a vertical bar and the target units:

`25m/s|km/h`

The result is 90km/h, which is correct.

You can also attach units to variables and functions. If you have units in an expression that defines a variable, the result will be stored in the variable, together with the calculated units. If you specify target units at the end, they will be used instead. From this point further, the variable will be substituted always with the internally stored units. You can try the following examples:

Input Output
Example 1: Speed
```'Distance -'S = 50m
'Time -'t = 2s
'Speed -'V = S/t|km/h```
Distance – S = 50m
Time – t = 2s
Speed – V = S/t = 50m/2s = 90km/h
Example 2: Force
```'Mass - 'M = 500t
'Acceleration - 'a = 4m/s^2
'Force -'F = M*a|kN```
Mass – M = 500t
Acceleration – a = 4m/s2
Force – F = M·a = 500t·4m/s2 = 2000kN
Example 3: Stress in column
```'Column load -'F = 2000kN
'Section size -'b = 500mm
'Section area -'A = b^2|cm^2
'Stress -'σ = F/A|MPa```
Column load – F = 2000kN
Section size – b = 500mm
Area – A = b2 = (500mm)2 = 2500cm2
Stress – σ = F/A = 2000kN/2500cm2 = 8MPa

If you specify target units after a function definition, they will be attached permanently to this function. The result will be converted to them whenever the function is used further. However, you have to be careful to provide the proper units for the arguments. Otherwise, you can get an error.

Although Excel is still the most popular platform for engineering spreadsheets, it lacks such advantages as automated units. I think that math software is more appropriate for engineers in general, but this will be a topic for future discussions.

Read the next post to see how do units actually work. We will lift the cover and have a quick look inside the Calcpad engine, together.