The Museum of HP Calculators

HP Articles Forum

Better trig functions on HP17BII using Maclaurin series

Posted by Michael Blankenship on 10 Sept 2001, 11:35 a.m.

[This is a reprint of a message I posted in the forum]

All,

I found a way to program the trigonometric functions into my HP 17BII business calculator. True story: my calculus professor told me on the first day of the course to ditch my business calculator and go get a trigonometric calc. Bummed, I leafed through the back of the calculus book and read about the Maclaurin power series. Click, click, click and now I have the trig functions programmed into my HP. You'd think that a certified calculus professor would have suggested this, huh? Anyway, I had a blast figuring out the nifty HP programming language after much struggle to find the manual, searching the Internet for a source of the programming instructions, failure and the "turning up" of the actual manual during a physical search for something else. Anyway, without further ado, the cosine function for the HP business calculator's SOLVER functionality:

SOLVE -> NEW -> COS=1+SIGMA(I:2:12:2:((MOD(I/2:2)x-2)+1)x(THETA^I)/FACT(I))

Note: Replace SIGMA above with the actual greek sigma character (looks like a capital E) from the ALPHA -> WXYZ -> OTHER -> MORE menu. Replace THETA above likewise with the greek theta character by pressing MORE twice more. The slash character above is simply the "divide by" character and the character "x" is the times sign, not the letter. The colon is also found on the ALPHA -> OTHER menu.

Since this forum doesn't provide for good formatting I can't directly show the equivalent formula but basically:

COS(theta) = 1 + sum of series as I steps from 2 to 12, stepping by 2 each time and alternating signs (theta raised to the I power over I-factorial).

The modulo section is just so that the terms alternate signs, in other words:

COS(theta) = 1 -((theta^2)/2!) +((theta^4/4!) -((theta^6/6!) +...

Once you've typed this into the HP, press INPUT and CALC and enter in 0 and press the function key associated with theta, then press the COS function key. The answer should be 1.00000, depending upon your current number of decimal places (which should be about six for these kinds of calculations). Note that the angle input is in radians rather than degrees. More on that in a moment...

Without much further explanation (other than the fact that the SIN function uses the odd powers of I instead), here are the remaining functions you might need:

SIN=SIGMA(I:1:13:2:((MOD((I-1)/2:2)x-2)+1)x(THETA^I)/FACT(I))

TAN=SIGMA(I:1:13:2:((MOD((I-1)/2:2)x-2)+1)x(THETA^I)/FACT(I))/(1+SIGMA(I:2:12:2:((MOD(I/2:2)x-2)+1)x(THETA^I)/FACT(I)))

Notes: The "DEGREES:" text above is just a label so that you can more easily find the conversion formula. By inputting in the cosine you want (1.00, for example) into the COS function and then pressing the function key associated with theta, you get the ARCCOS function, yielding 0.0 in this case.

Don't forget that you can store a variety of temporary numbers here and there using the [STO + number] and [RCL + number] syntax. I could have replaced the...

"((MOD(I/2:2)x-2)+1)" code with "IF(MOD(I/2:2)=1:-1:1)"

...but this resulted in more characters. I was a little disappointed to find that the IF() function doesn't evaluate expressions as C/C++ does. I could have saved a little more program space. If you want more precision, increase the number of decimal places in your calculator first and then if that's not enough, increase the upper range of the sigma function to something larger than twelve (in the cosine function, for example).

P.S. - Those of you familiar with the Maclaurin series for the SIN function might say, "Hey, the first term of the series is just theta itself." Well, yes, but I was optimizing for size of program, not speed of execution. If you feel otherwise, feel free to change the code so that the series begins at 3 instead of 1 and add theta somewhere outside of the series function itself.

P.S.S. - Okay, so why didn't I optimize out the first term of the power series for the COS function? Note that (0^0) is undefined had I started the series at the zeroeth instead of the second power.

Edited: 16 Feb 2006, 8:42 p.m.