# HOW TO USE: SIGMA --"It's a Wonderful Function"

### By Ed Keefe

This article explores SIGMA(), the summing function in HP Calc's Solver. SIGMA is one of the more powerful features of Solver. Let's see it in action before we try to describe it. Graphic

Press (HPCALC) (MENU) Solve. Move the cursor down to an empty line and press (F2) (Edit). Then key the following lines into SOLVE's edit screen. (The (<Tab>) acts like a carriage return in the Solve Editor. You can indent terms using (<Spacebar>). I've laid the formula out in this line-by-line fashion because I think it's easier to read. You don't really need to put carriage returns or indents for the formula to work. Also, the answers in this article assume that you have HP Calc set to display four decimal places (press (MENU) Options Format Fix 4).)

SIGMA
(
counter,
initial,
final,
step,
counter^2
)

When you're ready, press (ESC) to leave the Solve Editor screen (press (F6) to "Save changes before exiting?"). Then press (F9) (Calc) to go to the "Solve Calc" screen.

Set initial= 1, final= 3, and step= 1. Press (F2) (answer) and see below, "answer = 14.0000." (Editor's Note: All the formulas in this article are found in KEEFE.EQN (ON DISK ICON).)

What SIGMA Does

SIGMA turns out to be a looping routine, built on the ability to add the results of successive mathematical operations to an "accumulator." The accumulator is a memory buffer that SIGMA stores its results in.

Here's a step-by-step description of what SIGMA does when you press the "answer" key:

1. 1. SIGMA clears out the accumulator. (like pressing the [C/CE] key on a pocket calculator)
2. 2. SIGMA assigns the value of "initial" (1) to counter
3. 3. SIGMA checks to see if the value of "counter" is less than or equal to the value of "final." If it is, SIGMA performs the following calculation loop:
4. a) compute "counter^2" (i.e. square the present value of "counter")
5. b) add this result to the hidden accumulator
6. c) add the value of "step" to "counter"
7. d) Go back to step 3 above

8.

When step 3 above determines that the value of "counter" exceeds the value of "final," SIGMA ceases to perform the loop calculation, copies the value from the hidden accumulator to the "answer =" line, and quits.

Some Features of SIGMA

SIGMA is very much like BASIC's "FOR..NEXT,STEP" loop in that it uses four arguments: 1) a counter, 2) an initial value, 3) a final value, and 4) a step value.

However, unlike the "FOR..NEXT" loop, SIGMA takes a fifth argument; a function or expression that is evaluated and added to a built-in, hidden accumulator.

The HP 95LX User's Guide (page 29-22) doesn't mention this, but the values for "initial, final, and step" can be any numbers. The numbers can be positive or negative, whole or fractional. For example, in the above SIGMA function example, change the value of step to 2 and press (F2) to find "answer = 10." SIGMA summed up 1 + 3 (1 + 9) and then quit. The next run through the loop would have made the value of counter exceed the value of final (3), so SIGMA quit before doing that.

To see the effect of using a negative, fractional value for step, try this: Set initial = 3; final = 1; and step = -0.5. Press (F2) (answer) to see that the answer = 22.5. (In this case, SIGMA accumulated 3^2 + 2.5^2 + 2^2 + 1.5^2 + 1^2 and then quit.)

Note that the value of the first argument (counter) never shows up in the Solve Calc menu.

A Neat Trick

Create a loop with a built in pause. Return to the Solve Editor screen and modify the equation like this.

SIGMA
(
counter,
initial,
initial,
1 !dummy step !,
initial^2+0*L(initial:initial+10)
)

Press (ESC) (F6) (YES to save changes) and (F9) (Calc) to display the Solve Calc screen. Set initial = 0 and press (F2) (answer) to see "answer = 0." Now press (F2) again to see that the answer = 100. Press (F2) again and the answer = 400. Each time you press (F2), a new answer is calculated.

Here, we've forced SIGMA to perform a single pass through the loop by setting the second and third arguments to the same value. The fourth argument (step) is not needed, but it must be given some value. One works as well as any number.

We expanded the fifth argument by adding a Let() function. The expression "0*L(initial:initial+10)" adds nothing to the value of "initial^2" because it is multiplied by zero. However, it does increase the value of initial by 10 each time you solve for "answer."

Let's make a slight modification to the program so that the value that is being squared is displayed in the Solve Calc screen.

Press (ESC) (F2) (Edit) and add a couple more lines to the equation file.

SIGMA(counter,
initial,
initial,
1 !dummy step!,
initial^2+
! Let "CurrentVal" be the same as "initial" !
0*L(CurrentVal:initial)+
! Then add 10 to initial !
0*L(initial:initial+10))

The two additions, "+0*CurrentVal" and "0*L(CurrentVal:initial)" put the value of "initial" in "CurrentVal" and display it on the Solve Calc screen. Neither expression will affect the operation of the program since they're both multiplied by zero.

The phrases in between the exclamation points are comments. They don't hurt the equation, and can be helpful when you're trying to make sense of the equation later on.

The "factorial" of any number "N" is the product of the whole numbers from 1 to N. So, for example, the factorial of 5 is 1*2*3*4*5 = 120. To get Solver to calculate the factorial of N, just change the fifth argument (initial^2+) to FACT(initial)+ and change 0*L(initial:initial+10) to 0*L(initial:initial+1). After you've made the changes, press (ESC) Yes (F9) to return to the Solve Calc screen. There, set initial = 0, and press (F2) (answer) repeatedly. The number you are taking the factorial of is displayed on the "CurrentVal" line.

Numerical Integration Using SIGMA()

One of the classic problems in calculus is finding the area under a curve. The procedure for doing this is called definite integration and is represented by the following symbol:

b a f(x)dx

("f(x)" is some function and "a" and "b" are lower and upper bounds of "x")

To see how SIGMA can approximate definite integration, let's use a simple function, "f(x) = x^2." Press (ESC) a couple of times to get back to your Solve Catalog. Go to a blank line at the bottom of the catalog, press (F2) (Edit) and enter the equation, y=x^2. Press (F10) (Graph) to get a graphical idea of what the function looks like. Press (MENU) Erase Data and then press (F3) (AUTO). You should see a parabola that stretches from x = -5.00 to x = 4.94. It touches down on the x axis at x=0 and y=0.

Next, we'll compute the area under this curve and above the x axis, from the point where the curve touches the x axis (x = 0, y = 0) to the point where x = 6.00 and y = 36.00. We begin by dividing the area under the curve into a set of thin rectangles of uniform width. (We'll use "dx" to represent the width of the thin rectangles.)

The problem becomes one of summing up the areas of all the thin rectangles. But instead of multiplying the height of each rectangle by the width (dx) and then adding up the areas, we'll find the height of each rectangle, add all the heights, and then multiply that number by the width to get the area.

This will give us a close approximation to the total area. It's an approximation because the rectangles overfill the area under the curve. There are extra small triangular areas on the top of each rectangle, above the curve. However, if we make the width of the rectangles very, very thin, the triangular areas won't matter that much.

Here's a SOLVE equation that will compute the area under the parabola. (Again, the words between the exclamation marks are comments, and not needed for the function to work.)

Area=SIGMA(x,
a, !start!
b, !end!
dx, !width!
x^2 !height!
)*
dx !width!

The SIGMA() part of this equation finds the sum of the heights, from point a, to point b, along the x axis. The value of x is incremented by dx until point b is reached. At each point, the height is computed from the formula "height=x^2." Finally, the sum is multiplied by the width, dx, to give the Area.

To see what kind of result you get, key the above formula into SOLVE's editor and press CALC [F9]. Set a = 0, b = 6, and dx= 0.5. Press (F2) and you should find that Area = 81.25. (If you set dx = 0.05 and solve for Area -- Area = 72.90. This is closer to the exact value of 72.00. You'll also find that the calculation takes a little longer.)

Refine the solution: Press (ESC) (F2) and modify the formula to read:

Area=SIGMA(x,
a+dx/2,
b,
dx,
x^2
)*dx

Adding "dx/2" to the value of "a" has the effect of using the average height of the rectangle.

Press (F9), set dx = 0.05, leave a = 0 and b = 6, and press (F2) to find that Area = 71.9988. Adding "dx/2" makes the answer more accurate.

Finally, make the solution more general, and force SOLVE to compute the width for us. Press (ESC) (F2) and modify the formula like this.

Area = 0*L(dx : -(a-b)/res)+
SIGMA(
x,
a+G(dx)/2,
b,
G(dx),
x^2
)
*G(dx)

The expression "0*L(dx,-(a-b)/res)+" forces SOLVE to compute the width (dx) of the rectangles, provided we tell it how many rectangles we want. ("res" abbreviates "resolution.")

Using "dx" as the first argument in L() and using it only inside G() everywhere else, hides "dx" so it won't appear in the menu. (See page 27 of the May/June HP Palmtop Paper for more on the L() and G() functions.)

When you're ready, press (F9) (Calc), set a = 0, b = 6, res = 100, and press (F2) to find Area = 71.9982.

This particular method of numerical integration is the simplest, but its accuracy is limited. Note that the final algorithm will let you find the numeric integral of any continuous function. Just substitute your function for "x^2" in the above equation.

For a different algorithm that does the same thing, but more precisely, see page 36 of the booklet, Technical Applications: HP-27S, HP-19B, Hewlett-Packard # 00027-90044. The booklet is available from EduCalc.

Viewing the Area Under a Curve

To see the area under the curve Y= X^2, start the SOLVE Editor and enter the following equation ("! then !" and "! else !" are comments).

AreaGr=IF(L(Z:-Z+1)>0,
! then ! X^2,
! else ! 0
)

Press (F10) (Graph) and set XMIN = 0, XMAX = 6.1 and RES = 360. Press (F3) (AUTO) and watch the area between the curve and X-AXIS get shaded in.

How does this work?

The answer is that Graph has its own built-in loop mechanism which feeds increasing values of x into the equation. Each time a new value is fed into the equation, the L(Z:-Z) argument is evaluated. This causes L() to flip-flop between positive and negative values. If the value is positive, then X^2 is plotted, otherwise the line y=0 is plotted. As Solve plots the graph and X increases, each point connects to the next point. Since Y alternates between X^2 and 0 as X increases, the area between Y=X^2 and Y=0 gets filled.

Z is just a variable that can be toggled from positive to negative numbers. Z's initial value will be whatever is in memory when the plotting starts. We don't care what the value is, just so it's not zero. To cover this rare case, we use "+1" to insure that Z never starts with 0.

Something Fishy Graphic

This technique can be used to plot the area between two curves as well. Enter the following equation in the SOLVE Editor.

FISH=IF(L(Z,-Z+1)>0,
-X^2,
X^2-36
)

Press GRAPH and set XMIN = -6, XMAX = 4.3 and RES = 300. Press (F3) (AUTO) and watch a silhouette of a fish appear on the screen.

Here's another plot from Jeff Mattox, the author of the HP 95LX add-in program 95Buddy. Key in the following equation in the Solve Editor.

IF(
L(z,-z+1)>0,
1,
-1
)
* SQRT( x^2 ( x/10)^4 )

Press (F10) (Graph), set XMIN = -125, XMAX = 125 and RES = 300. Then press (F3) (AUTO) and, with a little patience, Jeff claims that you can see a "butterfly." (I see a bow-tie.)

For more of this stuff, I refer you to a Rorschach ink-blot test.

Until next time, Happy Programming!