home

hp 9g

one of hp's newer range which includes the 30s and the 9s. they all look very similar. poorly built, slim with hard plastic covers and just enough space inside to hold a small quick reference card.

this unit is more expensive than the 9s but the extra cost translates into features rather than build quality. it would be nice if the more advanced machines were better built, especially in terms of the buttons which are barely adequate for their purpose.

like all the new range, the machines are formula entry. rpn is gone and the only clue to what might have been is enter written on the equals button.

the accuracy of the machine is very good. like the 30s, it appears operate internally in binary with a 24 decimal digit equivalent. unfortunately the luxury of e500 exponents has been left behind to older models, so too has factorials of non-integers (using the gamma function).

the unit claims to be a "graphing calculator", but in this regard its abilities are rather poor. the small screen means that graphs are plotted in low resolution and only the left half of the screen is used for the graph itself. furthermore the plotting speed is embarrassingly slow for a modern machine. so graphing appears here a marketing afterthought. if you want graphs, check out the sharp el-9900

the documentation is appalling. you get two sheets of paper. one is like a very quick reference guide and the other a set of example calculations from which you can deduce how it works. its programmable and to find out about the programming language and syntax you have to read the examples and figure it out for yourself.

rather shocking is the lack of solve and numerical integration. surely they are not a lot to ask for. the casio fx-3900pv offers a straightforward no-frills simpson's rule and usually gives acceptable results quickly. even the, recently discontinued, hp20s, offers a rom-based simpson's rule program that can be called up to help plug the fact that it hasn't this feature native. the hp20s display is fantastic by the way, unlike this model which is blurry and difficult to read, especially in low light.

the feature set is good for a scientific model, with trigs, hyperbolics, stats, regression, unit conversions, physical constants, fractions and number bases.

programmatically, the 9g appears to implement yet another bastardised basic with syntax mixed in from C, like for (i=0;i<10;i++) { ... } and so on. unfortunately the machine has only 400 steps of capacity for a basic program. this is a humiliation for the machine as you can't fit in more than a few lines of program with the syntax.

there are better machines available at this price point and hp no longer offers superior engineering quality either in design or operation and many of the finer points are missing making this unit inferior to offerings from casio and sharp.

program example: tvm solve

let's solve the "penny for your thoughts" financial problem cited from the hp manuals.

a bank retains a legal consultant whose thoughts are so valuable that she is paid for them at the rate of a penny per second, day and night. lest the sound of the pennies dropping distract her, they are deposited into her account to accrete with interest at the rate of 10% per annum compounded every second. how much will have accumulated after a year of 365 days?

we have the following:

n = 60*60*24*365 = 31,536,000
i = 0.10/N = 3.1709792e-9
pv = 0
pmt = -0.01
fv = ?

This is a standard tvm problem for financial models like the hp12c. this particular example has been specially chosen because it involves a calculation of the form (1+k/n)^n which is difficult to preserve accuracy when n is large (tends to exp(k) as n->inf).

but, with the 9g we don't care because it has 24 internal digits, so let's just bang out the answer from the textbook formula:

    fv= -pmt*((1+i)^n-1)/i (when pv=0)

thus:
    60*60*24*365->n
    .1/n->i
    .01*((1+i)^n-1)/i
gives 331.6670067e3 so, fv=$331,667.0067

now let's solve the same problem backwards, solving for i

n = 60*60*24*365 = 31,536,000
i = ?
pv = 0
pmt = -0.01
fv = 331667.0067

unfortunately, the 9g doesnt have a solve feature so let's write one. here's an idea based on the secant method:

prog 0 is to be the solver, thus:

INPUT L,H,E;
E=10^(-E)
X=L
GOSUB PROG 9;
A=Y
Lbl 1:
IF (ABS(H-L) < E*ABS(H)) THEN { GOTO 2; }
X=H
GOSUB PROG 9;
B=Y
C=H-B*(H-L)/(B-A)
L=H
A=B
H=C
GOTO 1;
Lbl 2:
X=C
END

it prompts for three inputs, L, H & E, L is lower bound for the answer, H an upper bound and E is the number of significant figures required (eg 8). This program expects a subroutine in prog9 which takes the value of X and computes the Y=F(X), Prog0 then solves for X, where Y=0.

For this problem, write prog9 as follows:

Y=((1+X)^31536000-1)*.01/X
Y=Y-331667.0067
END

run prog0 and supply L=1e-30, H=1e-7, E=8. because our prog9 divides by X we can't supply L=0. after only 5 seconds the program finishes. it doesnt print out the answer, you have to change back to main mode and examine the x register. and we get,

x=3.1709792e-9 which is correct.

numerical integration program: simpson's rule

since the 9g has no solve nor integrate features built in. i thought these would make good example programs. solve is done in the previous example. so here is integrate, using the same idea of prog9 coding Y=F(X).

INPUT A,B,N
W=(B-A)/2N;X=A;GOSUB PROG 9;S=Y
FOR (N=N;N>0;--N) {
X=X+W;GOSUB PROG 9;S=S+2Y
X=X+W;GOSUB PROG 9;S=S+Y
}
S=(2S-Y)W/3;PRINT S
END

the program above implements simpson's rule to approximate the definite integrate of prog9 between A and B (given) using 2N+1 samples. for example giving N as 128 will call prog9, 257 times. this takes about 45 seconds for a short prog9 function.

numerical integration program: romberg method

a slightly more sophisticated integration algorithm is the romberg method shown here. again prog9 computes Y=F(X). the program prints the result and leaves the answer in S. the program prompts for A & B, the integration limits and E, the relative error.

if the result does not converge fast enough, the method gives up after 513 evaluations (about 2.5 mins). you can get an approximation of the error by examining D-L.

INPUT A,B,E
H=2;D=0;M=0;J=1;B=B-A
FOR (K=0;K<9;++K) {
L=D;F=H/2-1;C=0
FOR (I=0;I<J;++I) {
D=1-FF;X=((F+DF/2)*B+B)/2+A
GOSUB PROG 9;C=C+DY;F=F+H}
F=4;D=M;M=(M+HC)/2
FOR (I=0;I<=K;++I) { C=N[I];N[I]=(F*M[I]-D)/(F-1);D=C;F=4F }
D=M[I]
IF ((ABS(D-L) < 16E*ABS(D)) THEN { GOTO 1 }
J=2J;H=H/2 }
Lbl 1:
S=3DB/4;PRINT S
END

a better solver

the example above used a simple implementation of the secant method to solve a problem. unfortunately, although the secant method can be quite fast and efficient, in some cases it doesnt converge and fails to find a solution.

here's a better method based on ridder's algorithm:

INPUT A,B,E
X=A;GOSUB PROG 9;C=Y
X=B;GOSUB PROG 9;D=Y
FOR (I=0;I<100;++I) {
L=(A+B)/2;X=L;GOSUB PROG 9;G=Y
H=GG-CD;IF (H<=0) THEN { GOTO 1 }
H=G(L-A)/SQRT(H);
IF (C<=D) THEN { H=-H }
X=L+H;GOSUB PROG 9
IF (GY<0) THEN { A=L;C=G;B=X;D=Y }
ELSE {IF (CY<0) THEN { B=X;D=Y}; ELSE {A=X;C=Y}}
IF (ABS(A-B) < E*ABS(B)) THEN { GOTO 1 }
}
LBL 1:
PRINT X
END

same as before, use prog0 to run the solver. it prompts for A and B, the lower and upper limits of the root search. then for E, the tolerance. enter a relative error (eg 1e-6). prog0 calls prog9. the subroutine that defines y=f(x) so that prog0 solves for x where y = 0. of course, the drawback with the better solver is that its bigger and theres not a lot of program memory available.

bugs

ive encountered some bugs whilst writing these programs. they are either bugs or poor design. here are my findings in case you are having similar troubles:

bulletany goto back to a label generates a Nest Err after 3 loops.
bulletany goto inside a for-loop appears to terminate the for-loop so that it wont loop on the closing }