Programming with the HP 50g
New to RPL (Reverse Polish Language) Programming? This page teaches the basics of RPL, which opens up the rich world of the hp 50g.
It will help if you know how RPN and the stack works. I will try to explain each step along the way.
All the programs will be taught in RPN Mode. To set your calculator in RPN Mode, press MODE, and select RPN as your operating mode.
Program Brackets
Pressing Right Shift, Plus Key invokes the the program brackets (<< >>) on the screen. Everything that happens between these brackets is the program.
This page will use examples to show programming.
The First Program: Square a Number, Then Add One
Let _ be the symbol of our cursor.
| Press... |
Result |
Comments |
| Right Shift, Plus Key (<< >>) |
<< _ >> |
Start the program |
|
Left Shift, Square Root Key (x^2)
|
<< SQ _ >> |
Inserts the Square (SQ) command |
| 1 Key (1) |
<< SQ 1 _ >> |
|
| Plus Key ( + ) |
<< SQ 1 + _ >> |
Inserts the Add (+) command |
| ENTER Key |
1: << SQ 1 + >> |
Returns the program to line 1 of the stack. |
| 'FIRSTPROG', STO |
|
Stores the program as FIRSTPROG |
Running a program:
1. Invoke the program from the VAR menu.
2. If the program is in "raw" form (for lack of a better word), that is , the commands are surrounded by the program brackets, then you can press the EVAL key to run the program.
Be SURE to enter the arguments on the stack BEFORE you run the program.
Use FIRSTPROG to evaluate 2^2 + 1 and 3.72^2 + 1.
| Press... |
Screen... |
Commments... |
| VAR |
|
FIRSTPROG shows up on the var menu. If the above exercise was the very last thing you did, FIRSTPROG will be on the first soft key of the VAR menu. Note that only "FIRST" shows up because the soft menu only fits the first five characters of the program name. |
| 2 |
1:
2_
|
2 is entered. You may press ENTER to put it on the first level of the stack, but in this case it is not necessary. |
| FIRST |
1: 5 |
2^2 + 1 = 5 |
| 3.72 |
1: 5
3.72_
|
|
| FIRST |
2: 5
1:14.8384
|
3.72^2 + 1 = 14.8384 |
Success! Your first program. Consider yourself an RPL Programmer. Now let's tackle more complicated programs.
Remember, you can always change a program and most always test a program along the way. Do not be afraid of making mistakes.
Program 2: Introducing IF, THEN, ELSE, END and Local Variables
One of the most powerful command sets of RPL is the IF-THEN-ELSE-END command set. We will use IF-THEN-ELSE-END to evaluate this:
f(x) = { x^2 + 1 if x>=0 (x is greater than or equal to 0), |x^2 - 1| if x<0 (x is less than zero) }
|
IF - THEN - ELSE - END Structure:
argument1 argument2 IF comparison statement
THEN do these commands if the comparison is true
ELSE do these commands if the comparison is false
END
You may use the IF - THEN - END structure without the ELSE.
|
We will also use a local variable. A local variable is a variable used in a program, but the original contents (if there are any to begin with) stored
in the variable does not change. Store local variables by pressing Right Shift, 0 Key (->). You will have to invoke another set of programming
brackets (<< >>) after the variable name.
We will call this program SPLITFX (Spilt "Function of X").
I like to clear the stack (press Right Shift, Delete Arrow (CLEAR)) before working on programs.
| Press... |
Screen |
Comments |
| Right Shift, Plus Key (<< >>) |
1:
<< _
>>
|
Send the calculator into Program Mode. |
| Right Shift, Zero Key (->) |
<< -> _
>>
|
Local Variable |
| X Key, Right Shift, Plus Key (<< >>) |
<< -> X << _
>> >>
|
Starts "secondary" program. Remember, storing to local variables require that you start a program within a program. This also leaves your stack "blank". |
| X Key, 0 Key, Left Shift, EVAL Key (PRG) |
<< -> X << X 0 _
>> >>
|
Inserts X and 0 and calls up the Programming Menu |
| BRCH [Branch], Right Shift, IF |
<< -> X << X 0
IF
THEN
ELSE
END_
>> >>
|
Inserts the IF-THEN-ELSE-END Structure (This assumes you have your calculator set on SOFT Menus) |
| After IF, press Left Shift, 1/X Key (>=) |
<< -> X << X 0
IF >=
THEN
ELSE
END_
>> >>
|
The Comparison Test |
| After THEN, press X Key, Left Shift, SQRT Key (x^2), 1 Key, Plus Key |
<< -> X << X 0
IF >=
THEN X SQ 1 +
ELSE
END_
>> >>
|
Commands if the comparison is true. |
| After ELSE, press X Key, Left Shift, SQRT Key (x^2), 1 Key, Minus Key, Left Shift, Ă· Key (ABS) |
<< -> X << X 0
IF >=
THEN X SQ 1 +
ELSE X SQ 1 - ABS
END_
>> >>
|
Commands if the comparison is false. |
| ENTER, 'SPLITFX', STO Key |
|
Puts the program on the stack and stores it in the variable SPLITFX. |
The whole program SPLITFX is:
<< -> X << X 0 IF >= THEN X SQ 1 + ELSE X SQ 1 - ABS END >> >>
Evaluate f(-1) and f(1).
Press 1, +/-, SPLITFX. The program returns 0. So, f(-1) = 0.
Press 1, SPLITFX. The program returns 2. So, f(1) = 2.
Program 3: Introducing FOR-NEXT
The FOR-NEXT loop is another basic structure. It repeats a set of instructions on a specific "dummy" variable.
|
FOR-NEXT Structure:
start end FOR variable commands to be repeated (with variable) NEXT
Access:
Left Shift, EVAL Key (PRG), BRCH (Branch), Left Shift, FOR (puts FOR and NEXT on the stack).
|
Problem: Calculate the first ten powers of 2 using FOR-NEXT. Name this program POWERTWO (Powers of Two).
POWERTWO:
| Press: |
Screen: |
Comments: |
| Right Shift, Plus Key |
<< _ >> |
|
| One Key, SPC Key, One Key, Zero Key |
<< 1 10 _ >> |
|
|
Left Shift, EVAL Key (PRG), BRCH (Branch), Left Shift, FOR (puts FOR and NEXT on the stack).
|
<< 1 10
FOR _
NEXT >>
|
Inserts the FOR-NEXT structure. |
| After FOR, press ALPHA Key, TOOL Key (for the letter I), SPC Key, 2 Key, SPC Key, ALPHA Key, TOOL Key (I), Y^X Key |
<< 1 10
FOR I 2 I ^ _
NEXT >>
|
Insert the commands for finding the first ten powers of 2. |
| ENTER Key, 'POWERTWO', STO Key |
|
You're finished. |
Here's the program:
<< 1 10 FOR I 2 I ^ NEXT >>
To execute POWERTWO, just press POWERTWO. This program does not take arguments.
The answers:
2 (on line 10 of the stack)
4
8
16
32
64
128
256
512
1024 (on line 1 of the stack)
Program 4: Use Another Program in a Program
In RPL, to use another program in your program, all you have to do is call it's name. Let's demonstrate this by finding all the values of f(x) from -2 to 2, in 1/2 steps,
where f(x) is defined in the SPLITFX program.
Note: I will use the FOR-STEP structure. The FOR-NEXT structure only deals with incrementing the variable by 1. STEP allows me to do a custom increment.
Structure:
start end FOR variable commands increment (or decrement) STEP
Access: BRCH, Right Shift, FOR
This time, I will only type out the entire program. If you need help with how to get the keys, please email me at ews773@hotmail.com.
<< -2 2 FOR I I SPLITFX .5 STEP >>
Store this as TABSPLT (Table of SPLITFX). Note that no arguments are required because we supplied it for the SPLITFX portion of the program.
Results:
3 (line 9 of the stack)
1.25
0
.75
1
1.25
2
3.25
5
Future things planned:
Stack Commands in programs
Making results look nice
I hope this provides a good start. - Eddie
Back to Table of Contents
Comments (0)
You don't have permission to comment on this page.