5 reasons to choose math worksheets over Excel spreadsheets for structural calculations

Although there are plenty of software nowadays, engineers still have to develop custom calculations to solve specific problems. Some may still make them “by hand”, but most engineers use some kind of software for that.

The problem is that in most cases, we have to document our calculations and collect them in design reports or calculation notes. And it would bevalso very good, if we could give them professional look and feel. The other issue is to make the calculations reusable, in case we have similar projects in future.

Apparently, a peace of paper and pencil would not do the job anymore. Even a simple hand calculator (except for quick estimates). What we need is a suitable software for that purpose, but what?

To my experience, the most widely used solution for custom engineering calculations is Excel. At the same time, it is probably the most inappropriate for the job. But it is not actually an Excel fault. In general, it is a great software, very mature and high quality, but for tabular data. I myself usе it quite a lot for bills of materials and construction cost calculations. Spreadsheets are the best for simple formulas that have to be repeated on multiple rows/columns.

However, structural design algorithms are tree-like in nature, not tabular. Even if a tree data structure can fit in a table, this is not the optimal solution. It has some issues that cannot be neglected. During the years, I had to write a lot of structural desing spreadsheets on Excel, as well as math worksheets on software like MathCAD or Calcpad. Over time, I found some important reasons to prefer the math approach over Excel spreadsheets:

1. Formulas are built by referencing cells.

It is slow to write and difficult to check and follow, especially for the longer ones. “M = FL + qL^2/2″ is much easier to type and more readable than “= D2A3 + C1A3^2/2″. It is like obfuscation of the actual formula. For longer calculations, it gets even harder. Imagine that you have 1000 lines (rows) of calculations and your formula uses some cells at row 1, 500, 200, 700, 900, etc. You will have to scroll up and down multiple times to find the required cells to be referenced. Of course, you can use named cells, to improve readability, but it requires additional work.

2. Formulas are hidden inside the cells.

That makes the printed report a real black box for the checking engineer. There is an option to make them visible, but without formatting they are difficult to read. The best option is to write them once again with MathType. Besides it is a double work, you can mistype something and there is no guarantee that the formulas you see, are the same inside the cells. Also, variable substitution in formulas is not available in this case. So, Excel spreadsheets are prone to errors.

3. You cannot apply units of meaurements to values and use them in formulas.

You can write units of measurement in the next cell, but you cannot attach them to values and they will not take part in your calculations as it happens in math software. So, you must include the proper conversion factors manually in all of your formulas. Otherwise, you will simply get incorrect results.

4. It is difficult to define your own custom functions.

You must use VBA programming, which is not an option for a newbie. In math software, this is a piece of cake. You can simply type f(x) = 2*x^3 – … Also, it is very easy to plot a function graph directly from its equation. In Excel, you must calculate it in multiple cells and then, plot the respective range. If you have discontinuities or infinities and if you are not quite familiar with the function properties in advance, you can easily miss them and end up with incorrect graph.

5. It is almost impossible to branch the solution path and the report contents.

There are a lot of cases in structural design when the solution continues in completely different ways, depending on some intermediate checks. In systems like Calcpad, you can just add if-else if-else-end if condition blocks and put different formulas, text, and images in each one. In Excel, you can achieve limited results with intensive use of VBA.

So, even if you are an experienced spreadsheet developer and user, it is worth to give the math approach a try. Calcpad is perfect for this purpose, because it is lightweight, fast, easy to learn and free.

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:

NamegreeklatinGreekLatin
alphaαaΑA
betaβbΒB
gammaγgΓG
deltaδdΔD
epsilonεeΕE
zetaζzΖZ
etaηhΗH
thetaθqΘQ
theta-altϑvV
iotaιiΙI
kappaκkΚK
lambdaλlΛL
muμmΜM
nuνnΝN
xiξxΞX
omicronοoΟO
piπpΠP
rhoρrΡR
sigmaσsΣS
tauτtΤT
upsilonυuΥU
phiφfΦF
chiχcΧC
psiψyΨY
omegaωwΩW
phi-diamøjØJ

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.

You can download the latest version for free by using one of the following links:

https://calcpad.eu/download/calcpad-setup-en-x64.zip – official website;

https://github.com/Proektsoftbg/Calcpad/blob/main/Setup/calcpad-setup-en-x64.exe – GitHub;

https://sourceforge.net/projects/calyx/files/latest/download – SourceForge.

How to use Calcpad with Notepad++

Notepad++ is one of the most popular and free text/code editors. It supports many different programming and scripting languages. You can also edit HTML, XML, CSS, JSON and other types of files. Currently, its text editing capabilities significantly exceeds those of the Calcpad own editor. Some features that worth mentioning are:

  • multiple file tabs;
  • predefined and custom styles;
  • advanced search/replace capabilities;
  • column editor;
  • auto completion;
  • bracket matching;
  • line operations/sorting;
  • tab/white space management;
  • content folding, etc.

And and last but not least – syntax highlighting. Unfortunately, the Calcpad language is not natively supported by Notepad++. However, there is an option to add a custom language with user defined syntax (UDL). Then, you can export it as an xml file and import it everywhere else.

The good news is that we have already created the required language definition files, so you can use them to write Calcpad programs with Notepad++.

To use Notepad++ as Calcpad code editor you need to the following:

  1. Download and install Notepad++ from the following link:
    https://notepad-plus-plus.org
  2. Download the Calcpad language definition files:
    https://calcpad.eu/download/Notepadpp.zip
  3. Unzip the archive in the preferred folder. You will find two files inside:
    – Notepad++.xml – for light mode and
    – Notepad++dark.xml – for dark mode.
  4. Start Notepad++. Click the “Language” menu, then “User defined language” and “Define your language…”
  5. Click the “Import…” button in the dialog that will appear.
  6. Browse for the one of the xml files above and click “Open“.

From now on, each time you open a .cpd file, it will be automatically displayed with the Calcpad syntax highlighting. For best results with the dark theme, select the appropriate style from the “Style Configurator” dialog.

You can find it in the “Settings” menu. For example, a good option is the Twillight theme. You can also set a nice monotype font, such as “Noto Mono“, “Liberation Mono“, “Source Code Pro“, “Consolas“. etc. You can also make the whole interface t dark, by checking “Enable dark mode” in the “Preferences” dialog from the same menu.

You can also make possible to run the Calcpad programs directly from inside the Notepad++ editor. For that purpose, you will have to install the Franco Stellari’s RunMe plugin for Notepad++. You can find detailed instructions in this file: install.txt. It will add a small button at the end of the Notepad++ toolbar . Click the button or press Shift+F5 to run the current program.

.

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:

Plotting Julia set with Calcpad

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:

Colorful plot of Julia set

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.

Dendrite fractal image, obtained for c = i

Plotting the Mandelbrot Set

Calcpad has an interesting and undocumented feature, that I am going to reveal in this post. You can use it to quickly plot the Mandelbrot set. This is a set of complex numbers c, for which the iterative equation zn+1 = zn2 + c does not go to infinity. The most beautiful part is that it shows fractal behavior at its boundaries. If you plot it with the appropriate colors, you can create stunning images and animations. I really like those on the Maths Town’s YouTube channel:

Mandelbrot fractal zoom videos on YouTube

Usually, such videos are created by complicated software, specially developed for that purpose. However, you can also plot the Mandelbrot set with Calcpad and a few lines of code. There are two ways to do that. The first one is to use the Calcpad capabilities for complex arithmetic, as follows:

"Mandelbrot set
'Define the function
MandelbrotSet(z; c) = $Repeat{z = z^2 + c @ i = 1 : n}
#hide
'Set the plotting parameters
PlotStep = 1','PlotWidth = 500','PlotHeight = 500
#show
'Plot for'n = 50'iterations
$Map{abs(MandelbrotSet(0; x + 1i*y)) @ x = -1.5 : 0.5 & y = -1 : 1}

Open Calcpad, switch to “complex”mode, paste the above code inside, and run the calculations. You will get the following result:

Mandelbrot set plot with Calcpad complex arithmetic

It is not too bad, but still not very impressive. At least, the exterior is missing any colors. Then, how we can do the coloring? Theoretically, it is satisfied for all numbers in the set that |zn| < 2 for any n. Outside, the process diverges and the modulus goes to infinity, as n increases. You can use that in the following way:
1. For each point, calculate the number of iterations n, where |zn| gets larger than 2.
2. Map these values to colors.
3. Plot the colors at the respective coordinates.

If you play with the number of iterations n in the above example, you will see that the plot changes. The shaded area is where |zn| converges for the respective n.

Mandelbrot set, plotted with different values of n

If you combine the above plots and apply some colors, you can get really beautiful artistic image like the one bellow:

Mandelbrot set with Calcpad + GIMP

The only problem with this method is that it is slow and elaborate. That is why, I included a special function Mandelbrot(x; y) in Calcpad. It calculates the number of iterations, needed for the modulus to overpass 2. You can plot this function directly with the preferred color scale:

Mandelbrot function plot
Mandelbrot function partial plot

To do that, I used even shorter piece of code:

"Mandelbrot set
#hide
'Set the plotting parameters
PlotStep = 1','PlotWidth = 500','PlotHeight = 500
#show
'Plot for'n = 50'iterations
$Map{Mandelbrot(x; y) @ x = -1.5 : 0.5 & y = -1 : 1}
'Plot for'n = 70'iterations
$Map{Mandelbrot(x; y) @ x = -0.59 : -0.58 & y = 0.55 : 0.56}

Here you can find the C# code of the Mandelbrot function that works inside Calcpad:

        private static readonly double Log2Inv = 1 / Math.Log(2);       
        //Calculates the number of iterations for which z = z^2 + c satisfies |z| > 2
        //Then transforms it to a smooth log scale and returns the result
        protected static double MandelbrotSet(double x, double y)
        {
            //Checks if the point is inside the set and directly returns NaN
            if (x > -1.25 && x < 0.375)
            {
                if (x < -0.75)
                {
                    if (y > -0.25 && y < 0.25)
                    {
                        double x1 = x + 1,
                            y2 = y * y,
                            x2 = x1 * x1;
                        if (x2 + y2 <= 0.0625)
                            return double.NaN;
                    }
                }
                else if (y > -0.65 && y < 0.65)
                {
                    double x1 = x - 0.25,
                        y2 = y * y,
                        q = x1 * x1 + y2;
                    if (q * (q + x1) <= 0.25 * y2)
                        return double.NaN;
                }
            }
            //For all other points performs detailed calculations
            double re = x, im = y;
            for (int i = 1; i <= 1000; ++i)
            {
                double reSq = re * re, 
                       imSq = im * im,
                       sumSq = reSq + imSq;
                //To avoid the sqrt function, the check |z| > 2 is replaced by z^2 > 4
                if (sumSq > 4)
                {
                    var logZn = Math.Log(sumSq) / 2;
                    var nu = Math.Log(logZn * Log2Inv) * Log2Inv;
                    return (1.01 - Math.Pow(i - nu, 0.001)) * 1000;
                }
                //Calculates z = z^2 + c
                im = 2 * re * im + y;
                re = reSq - imSq + x;
            }
            return double.NaN;
        }
    }

The complete source code is available on GitHub.

New version 5.6!

We are proud to present the new version 5.6 of the Calcpad software. It is much faster than the previous versions and includes a lot of new features.

Calcpad is ported to the latest Microsoft .NET 6.0 platform, which is faster than .NET Framework 4.x we used before. On the other side, the Calcpad code was significantly optimized. Now, we use expression trees to pre-compile parts of code that are repeatedly executed – bodies of functions, methods etc.

We also implemented better algorithms for numerical analysis. For example, we replaced the adaptive Simpson’s method for numerical integration with the adaptive Lobatto quadrature proposed by Gander and Gautschi. We adopted this algorithm for the C# language. We also developed an improved version of Anderson and Bjorks‘s root finding algorithm. That is why Calcpad 5.6 runs 3-4 times faster than 3.x versions in general and over 20 times faster when numerical methods are involved.

For plotting, we introduced adaptive interpolation of functions. It provides smooth graphs with the minimum number of calculation points. They are distributed unequally, depending on the function curvature as shown on the following figure.

Output formatting was also improved in respect to variable substitution and units of measurement. Speed of units arithmetic was also enhanced by optimizing memory consumption. That is how we reduced the pressure on the Garbage Collector.

There are also some improvements in the UI layer. Recent file list was introduced in version 5.0. Behaviour of the Undo function was improved. Support for Open Office or Libre Office was added alternatively to MS Word. Thanks to the wkhtmltopdf project, now we have direct export to pdf.

And last, but not at least, we added a lot of new and useful functions as follows:

Since version 5.0:

  • new factorial operator “!”;
  • log_2(x) – binary logarithm function;
  • cbrt (x) – cubic root;
  • root(x; n) – n-th root;
  • csc(x) – cosecant;
  • sec(x) – secant;
  • csch(x) – hyperbolic cosecant;
  • sech(x) – hyperbolic secant;
  • acsc(x) – inverse cosecant;
  • asec(x) – inverse secant;
  • acsch(x) – inverse hyperbolic cosecant;
  • asech(x) – inverse hyperbolic secant;
  • acoth(x) – inverse hyperbolic cotangent;

Since version 5.6:

  • min(x; y; z…) – minimum of multiple values;
  • max(x; y; z…) – maximum of multiple values;
  • sum(x; y; z…) – sum of multiple values;
  • sumsq(x; y; z…) – sum of squares;
  • srss(x; y; z…) – square root of sum of squares;
  • average(x; y; z…) – average of multiple values;
  • product(x; y; z…) – product of multiple values;
  • mean(x; y; z…) – geometric mean;
  • switch(<cond1>; <value1>; <cond2>; <value2>; …; <default>) – selective evaluation;
  • take(n; a; b; c…) – returns the n-th element from the list;
  • line(x; a; b; c…) – linear interpolation for variable x;
  • spline(x; a; b; c…) – Hermite spline interpolation for variable x;

You can use the “take” function to represent vector and matrix-like behaviour. With the “line” and “spline“, you can interpolate over tabular data. Double interpolation is also possible with just a few line of code. If you have a function that is slow and heavy to calculate, you can use spline interpolation to speed up you program. Just sample the function in a few points and interpolate among them. We will discuss this in the next posts.

You can download the latest version from the following link:

https://calcpad.eu/download/calcpad-setup-en-x64.zip

Enjoy!

Even more 3D effects

In the last version I improved the 3D plot shading and added some nice 3D effects. Bellow is the plot of the function

f(x; y) = cos(x) + cos(y) + cos(x·y/3)/2 + cos(√(x2 + y2)·2)/2

with the “Rainbow” color scale and the “Shadows” option on:

Specular

What is new is the specular light effect (the little shiny reflections). I also replaced the Gouraud shading procedure with the Phong one. It improved the image quality  without any noticeable impact on the speed (surprisingly). Since the light is distant and the view is isometric, I use the Blinn–Phong shading model. Instead of calculating the reflection vector for each vertex, it calculates the halfway vector between the viewer and light-source. It is performed once for the whole model, and that is why it is faster.

In general, there is additional overhead for generating the Html output, converting the image to base64 and rendering the Html into the “Output” window. However, as long as it takes much less than a second for all of it, the speed is not of a big concern.

Math Art with Calcpad

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:

circles-and-hyperbolas

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

unsymmetric-hyperbolas

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

zig-zag-tiles

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

concentric-waves

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

intersecting-parabolas

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

intersecting-parabolas-x

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

puzzle

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

buttons

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

pins

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

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

Download the latest Calcpad 3.2 and try to create your own pictures. Enjoy!