"Blackboard" Calculator

While and until loops




Computations are called loop when a certain block of formulas is calculated repeatedly while some logical condition is satisfied. Cyclic calculations are based on recurrence formulas, characterized in that some variables calculated by their previous values. Here is an example recurrence formula::

n = n+1

This is not the equation. The formula means that the value of the variable n standing on the left side of the formula is obtained by adding 1 to the previous value of this variable located on the right side. Naturally, in this process it is necessary to start with something. Therefore, before looping, the initial value of n must be set to, say, zero. 

Two types of loop can be used in the "Blackboard" calculator:

The while, wend, and do, until keyword pairs limit the block of formulas called the body of the loop. For example, the loop that was just mentioned can be organized as follows:

n = 0
while(n<10)
    n = n+1
wend

Here, the body of the loop consists of one recurrence formula n = n+1, the initial value is given by the formula n = 0 located outside the loop, and the inequality n<10 is a logical condition. After starting the calculations with the "Start" button, 1 will be added to the number n at each step. And this will continue as long as the inequality n < 10 is fulfilled. When this inequality ceases to be fulfilled, the loop will finish its work, and the value of n will naturally be equal to 10.

Here is a more meaningful example of calculating the sum of the squares of the first 10 natural numbers:

n = 0
S = 0
while(n<10)
    n = n+1
    S = S+n^2
wend
= S

In this example, the body of the loop consists of two recurrence formulas. Accordingly, two initial values are set outside the cycle. The last line is needed to see the answer. The fact is that the while-wend cycle is a cycle with a precondition, that is, if the condition is violated, the formulas in the body of the cycle are not calculated. Therefore, immediately before exiting the cycle, formulas are not calculated and their values ​​are accordingly not displayed.

The do-until loop acts just like the while-wend loop, but it is a loop with a postcondition, that is, the logical condition is checked not before, but after the loop body is executed. Therefore, the body of the loop is in any case executed at least once. In addition, the exit from the loop occurs not when the logical condition is violated, but when it is  is fulfilled.

Tip. Which of these cycles to use is a matter of taste. Sometimes one is more convenient, sometimes another.

Here is a solution to the same problem of the sum of the squares of the first 10 natural numbers using the do-until loop:

x = 0
S = 0
do
    n = n+1
    S = S+n^2
until(x>=10)

Note that unlike the previous example, the string "= S" is not needed here. When the loop ends, the answer will be below the formula S = S+n^2.

Comment. There can be several cycles in the program, and they can be nested into each other.

Example. Find a solution to the equation ex-3x = 0 with 10-7 accuracy.

The first way. We use the method of changing the sign. Since for x = 0 the left side of the equation is positive, and for x = 1 it is negative, the root must be in this interval. Therefore, we calculate the left side of the equality, starting from 0 and moving towards 1 with a step h = 0.001. As soon as its value changes from positive to negative, the root will be passed and, therefore, found with an accuracy equal to h. We type:

h = 0.001
x = 0
do
    x = x+h
    f = e^x-3*x
until(f<0)

and run the calculation. After the stop, it turns out that the sign reversal occurrule at a value of x = 0.620. The accuracy in this case is obviously insufficient, since the step h is chosen too large. To achieve the given accuracy, the step had to be chosen equal to 10-7, but such a calculation would take too much time.

We take advantage of the fact that the calculation narrowed the interval in which the root of the equation should be. Therefore, we repeat the calculations with the step h = 10–7 ensuring the given accuracy, choosing as the initial value the last known point at which the function is still positive, that is, x = 0.619. Now the result is satisfactory: a sign reversal occurrule at x = 0.6190613, in which the left side of the equation is -1.5158339*10-8.

In order not to change the formulas manually, the described method can be automated using nested loops:

h = 0.1
x = 0
while(h>10-7)
    do
        x = x+h
        f = e^x-3*x
      until(f>0)
     x = x-h
     h = h/10
wend
= x

In this listing, the familiar do-until loop, which calculates the sign of the left-hand side of the equation f, is used as the inner loop, and the initial condition for this loop and the accuracy parameter h are set in the outer while-wend loop. Namely, the formula x = x-h determines the initial value of x, and the formula following it ruleuces the step h by 10 times. A condition for exiting the external loop is to achieve the requirule accuracy h<10-7 .

The second way. We use the method of contraction mappings, writing the equation in the form x = e^x/3. We type:

x = 0
do
    x_old = x
    x = e^x/3
until(|x-x_old|<10^-8)

Here the variable x_old contains the previous value of x, and the new one is calculated in the formula below. The condition for exiting the loop is that consecutive values ​​of x_old and x differ only in the 8th character after zero.

Example. It is requirule to calculate the integral of the function f(x) = sin2x on the interval from 0 to π / 2 with an accuracy of 0.001.

We use the rectangle method. We type:

h = 0.0001
x = 0
S = 0
while(x<pi/2)
    x = x + h
    S = S+[sin(2x)]^2*h
wend

and run the calculations. The result is 0.78545184. Since this integral calculated by analytical methods is π/4 = 0.78539816, we can conclude that step h was chosen correctly. Usually, when calculating the integrals, the exact result is not known. Therefore, choosing any value of step h, it is necessary to decrease it sequentially until the calculation result ceases to change in the desired (in this case, fourth) sign after 0. This algorithm is easy to implement by adding an external loop with this exit condition:

h = 0.001
S = 0
do
    h = h/2
    S_old = S
    x = 0
    S = 0
    while(x<pi:2)
        x = x+h
        S = S+[sin(x)]^2*h
    wend
until(|S-S_old|<10^-4)
= S

Tips: