Programacion Traducir 72-118
Programacion Traducir 72-118
BEEP takes two arguments from the stack: the tone frequency from level 2 and the tone duration from level 1.
Example: The following edited version of TPROMPT sounds a 440-hertz, one-half-second tone at the prompt
for data input.
Program: Comments:
«
"ENTER a b IN ORDER:" Sounds a tone just
440 .5 BEEP before the prompt
PROMPT for data input.
TORSA
»
Output
You can determine how a program presents its output. You can make the output more recognizable using
the techniques described in this section.
Example: The following program TTAG is identical to TINPUT, except that it returns the result as AREA: value.
Program: Comments:
«
"Key in a b"
{ ":a: :b:" {1 0} V
} INPUT OBJ→
TORSA
"AREA" Enters the tag (a
→TAG string).
Uses the program
result and string to
» create the tagged
object.
Stores the program in
`OTTAG ‰ TTAG.
Execute TTAG to calculate the area of a torus of inner radius a = 1.5 and outer radius b = 1.85. The
answer is returned as a tagged object.
J %TTAG% 1.5 ˜1.85 `
Program: Comments:
«
"Key in a b" Converts the result to a string.
{ ":a: :b:" {1 0} Enters the labeling strings.
V} INPUT OBJ→
TORSA
→STR
"Area = "
SWAP + Swaps and adds the two strings.
CLLCD 1 DISP Displays the resultant string,
3 FREEZE without its delimiters, in line
1 of
the display.
»
Stores the program in TSTRING.
`OTSTRING ‰
Execute TSTRING to calculate the area of the torus with a = 1.5 and b = 1.85. The labeled answer appears in
the status area.
@ J%TSTRI%
1.5 ˜1.85 `
Program: Comments:
« LST TMENU » Displays the custom
menu stored in LST.
Stores the program in
`OWGT ‰
WGT.
Use WGT to calculate the mass of an object of weight 1.25 N. The program sets up the menu, then completes
execution.
J%WGT%
Select the SI unit system, which starts the program in the menu list.
%SI%
Example: The following program, EIZ, constructs a custom menu to emulate the HP Solve application for a
capacitive electrical circuit. The program uses the equation E = IZ, where E is the voltage, I is the current, and
Z is the impedance.
Because the voltage, current, and impedance are complex numbers, you can’t use the HP Solve application to
find solutions. The custom menu in EIZ assigns a direct solution to the left-shifted menu key for each variable,
and assigns store and recall functions to the unshifted and right-shifted keys — the actions are analogous to the
HP Solve application. The custom menu is automatically stored in CST, replacing the previous custom menu —
you can press
!£ to restore the menu.
Program: Comments:
«
DEG -15 SF -16 SF Sets Degrees mode. Sets flags –15 and –
2 FIX 16 to display complex numbers in polar
form. Sets the display mode to 2 Fix.
{ Starts the custom menu
{ "E" { « 'E' STO » list. Builds menu key 1
« I Z * DUP 'E' for E.
STO "E: " SWAP +
CLLCD Unshifted action: stores the object in E.
1 DISP 3 FREEZE » Left- shift action: calculates I × Z, stores it
«E»} } in E, and displays it with a label. Right-shift
action: recalls the object in E.
{ "I" { « 'I' STO »
« E Z / DUP 'I' STO Builds menu key 2.
"I:"SWAP + CLLCD
1 DISP 3 FREEZE »
«I »} }
{ "Z"{ « 'Z' STO »
« E I / DUP 'Z' Builds menu key 3.
STO "Z:" SWAP +
CLLCD
1 DISP 3 FREEZE »
«Z»} }
} Ends the list.
ME Displays the custom menu.
NU
»
Stores the program in EIZ.
`OEIZ ‰
For a 10-volt power supply at phase angle 0°, you measure a current of 0.37-amp at phase angle 68°.
Find the impedance of the circuit using EIZ.
@ J%EIZ%
Key in the voltage value.
!Ü10 ~@6 0
Store the voltage value. Then key in and store the current value. Solve for the impedance.
Recall the current and double it. Then find the voltage.
@%%I%% 2 *%%I%% !%%E%%
Press (!&H)%FMT% %STD% and L %MODES %ANGLE %RECT% to restore Standard and Rectangular modes.
Fibonacci Numbers
This section includes three programs that calculate Fibonacci numbers:
■ FIB1 is a user-defined function that is defined recursively (that is, its defining procedure contains its own
name).
FIB1 is short.
■ FIB2 is a user-defined function with a definite loop. It’s longer and more complicated than FIB1, but faster.
■ FIBT calls both FIB1 and FIB2 and calculates the execution time of each subprogram.
FIB1 and FIB2 demonstrate an approach to calculating the nth Fibonacci number Fn, where:
F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2
FIB1 (Fibonacci Numbers, Recursive Version)
Level → Level
1 1
n → Fn
Program: Comments:
«
→ n 'IFTE(n Defines local variable n.
‰1 The defining procedure,
n an algebraic expression. If
FIB1(n-1)+FIB1(n- n ≤ 1, Fn = n, else Fn =
2))' Fn-1+Fn-2.
»
Stores the program in FIB1.
`OFIB1 K
Level → Level
1 1
n → Fn
Program: Comments:
«
→n Creates a local variable structure.
«
IF n 1 ‰ If n ≤ 1,
THEN n then Fn = n;
ELSE otherwise …
0 1 Puts F0 and F1 on the stack.
2n From 2 to n does the following loop:
START
DUP Copies the latest F (initially F1)
ROT Gets the previous F (initially F0)
+ Calculates the next F (initially F2)
NEXT Repeats the loop.
SWAP DROP Drops Fn-1.
END Ends the ELSE clause.
» Ends the defining procedure.
»
Stores the program in FIB2.
`O FIB2 K
Calculate F10.
10 %FIB2%
FIBT (Comparing Program-Execution Time)
FIB1 calculates intermediate values Fi more than once, while FIB2 calculates each intermediate Fi only once.
Consequently, FIB2 is faster. The difference in speed increases with the size of n because the time required for
FIB1 grows exponentially with n, while the time required for FIB2 grows only linearly with n.
FIBT executes the TICKS command to record the execution time of FIB1 and FIB2 for a given value of n.
Required Programs
■ FIB1 (page 2-1) calculates Fn using recursion.
■ FIB2 (page 2-2) calculates Fn using looping.
Program: Comments:
«
DUP TICKS SWAP Copies n, then executes FIB1,
FIB1 SWAP TICKS SWAP recording the start and stop time.
- B→R 8192 / Calculates the elapsed time, converts it
to a real number, and converts that
number to seconds.
Leaves the answer returned by FIB1 in level
2.
"FIB1 TIME" →TAG Tags the execution time.
ROT TICKS SWAP Executes FIB2, recording the start and
FIB2 TICKS stop time.
SWAP DROP SWAP Drops the answer returned by FIB2 (FIB1
- B→R 8192 / returned the same answer). Calculates the
elapsed time for FIB2 and converts to
seconds.
"FIB2 TIME" →TAG Tags the execution time.
»
Stores the program in FIBT.
`OFIBT K
Checksum: #
23164d Bytes:129
Example: Calculate F13 and compare the execution time for the two
methods. Select the VAR menu and do the calculation.
J
13 %FIBT%
F13 is 233. FIB2 takes fewer seconds to execute than FIB1 (far fewer if n is large). (The times required for the
calculations depend on the contents of memory and other factors, so you may not get the exact times shown
above.)
Level → Level 1
1
object → "object"
Program: Comments:
«
→STR Makes sure the object is in string form.
(Strings
are unaffected by this command.)
WHILE Repeats if the string contains fewer
DUP SIZE 22 < than 22 characters.
REPEAT Loop-clause adds a leading space.
" " SWAP +
END End loop.
»
Stores the program in PAD.
`OPAD K
Checksum: #
6577d Bytes:57.5
Level 1 → Level 1
«program» → res o program
ult f
'program' → o
program
res
ult f
Program: Comments:
«
RCLF Recalls the list of four 64-bit
binary integers representing the
status of the 128 system flags
and
128 user flags.
→f Stores the list in local variable f.
« Begins the defining procedure.
IFERR Starts the error trap.
EVAL Executes the program placed on
the stack as the level 1
argument.
THEN If the program caused an error,
f STOF ERRN DOERR restores flags, shows the error,
and
aborts execution.
END Ends the error routine.
f STOF Puts the list back on the stack,
then restores the status of all
flags.
» Ends the defining procedure.
»
Stores the program in
`OPRESERVE K PRESERVE.
Checksum: #
26834d Bytes:
71
Level 1 → Level 1
#n → #n
n → n
Techniques used in BDISP
■ IFERR…THEN…END (error trap). To accommodate real-number arguments, BDISP includes the
command R→B (real-to-binary). However, this command causes an error if the argument is already a binary
integer. To maintain execution if an error occurs, the R→B command is placed inside an IFERR clause. No
action is required when an error occurs (since a binary number is an acceptable argument), so the THEN
clause contains no commands.
■ Enabling LASTARG. In case an error occurs, the LASTARG recovery feature must be enabled to return
the argument (the binary number) to the stack. BDISP clears flag –55 to enable this.
■ FOR…NEXT loop (definite loop with counter). BDISP executes a loop from 1 to 4, each time
displaying n (the number) in a different base on a different line. The loop counter (named j in this program) is
a local variable created by the FOR…NEXT program structure (rather than by a → command), and
automatically incremented by NEXT.
■ Unnamed programs as arguments. A program defined only by its « and » delimiters (not stored in a
variable) is not automatically evaluated, but is placed on the stack and can be used as an argument for a
subroutine. BDISP demonstrates two uses for unnamed program arguments:
O BDISP contains a main program argument and a call to PRESERVE. This program argument goes on the
stack and is executed by PRESERVE.
O BDISP also contains four program arguments that “customize” the action of the loop. Each of these contains a
command to change the binary base, and each iteration of the loop evaluates one of these arguments.
When BDISP creates a local variable for n, the defining procedure is an unnamed program. However, since
this program is a defining procedure for a local variable structure, it is automatically executed.
Required Programs
PAD
■ PAD (Pad with Leading Spaces) expands a string to 22 characters so that DISP shows it right-justified.
PRESERVE
■ PRESERVE stores the current status, executes the main nested program, and restores the status.
BDISP program listing
Program: Comments:
«
« Begins the main nested program.
DUP Makes a copy of n.
-55 CF Clears flag –55 to enable LASTARG.
IFERR Begins error trap.
R→B Converts n to a binary integer.
TH If an error occurs, do nothing (no commands
EN in the THEN clause).
EN
D
→n Creates a local variable n and begins the
« defining program.
CLLCD Clears the display.
« BIN » Nested program for BIN.
« OCT » Nested program for OCT.
« DEC » Nested program for DEC.
« HEX » Nested program for HEX.
1 4 Sets the counter limits.
FOR j Starts the loop with counter j.
EVAL Executes one of the nested base programs
(initially for HEX).
n →STR Makes a string showing n in the current base.
Checksum: #
22884d Bytes:187
Example: Switch to DEC base, display #100 in all bases, and check that BDISP restored the base to DEC.
Clear the stack and select the MTH BASE menu. Make sure the current base is DEC and enter #100.
‚
‚ã%DEC%
!â100 `
Execute BDISP.
J %BDISP%
Return to the normal stack display and check the current base.
−
‚ã
Although the main nested program left the calculator in BIN base,
PRESERVE restored DEC base. To check that BDISP also works for
real numbers, try 144.
J
144 %BDISP%
Press − to return to the stack display.
Program: Comments:
«
SWAP SORT Brings the list to level 1 and sorts it.
DUP SIZE Copies the list, then finds its size.
1 + ROT % Calculates the position of the specified
percentile.
DUP2 Makes a copy of the list and the percentile.
FLOOR Rounds the position to the lower integer.
1 MAX OVER SIZE MIN Ensures it is between 1 and the size of the list.
GET Gets the corresponding number.
ROT ROT Moves the list to level 1.
CEIL Rounds the position to the upper integer.
1 MAX OVER SIZE MIN Ensures it is between 1 and the size of the list.
GET Gets the corresponding number.
+2 / Calculates the average of the two numbers.
»
Stores the program in %TILE.
`O%TILE K
Checksum: #
3805d Bytes:92.5
Level 1 → Level 1
→ [ x1 x2 … xm ]
Techniques used in MEDIAN
■ Arrays, lists, and stack elements. MEDIAN extracts a column of data from ΣDAT in vector form. To
convert the vector to a list, MEDIAN puts the vector elements on the stack and combines them into a list.
From this list the median is calculated using %TILE.
The median for the mth column is calculated first, and the median for the first column is calculated last. As
each median is calculated, ROLLD is used to move it to the top of the stack.
After all medians are calculated and positioned on the stack, they’re combined into a vector.
■ FOR…NEXT (definite loop with counter). MEDIAN uses a loop to calculate the median of each
column. Because the medians are calculated in reverse order (last column first), the counter is used to reverse
the order of the medians.
Required Program
■ %TILE (page 2-10) sorts a list and returns the value of a specified percentile.
MEDIAN program listing (Note: Use approximate mode for this program and example).
Program: Comments:
«
RCLΣ Puts a copy of the current statistics matrix ΣDAT
on the stack.
DUP SIZE Puts the list { n m } on the stack, where n is the
number of rows in ΣDAT and m is the number of
columns.
OBJ→ DROP Puts n and m on the stack, and drops the list size.
→snm Creates local variables for s, n, and m.
« Begins the defining
'ΣDAT' TRN procedure. Recalls and
transposes ΣDAT.
Now n is the number of columns in ΣDAT and m
is the number of rows. (To key in the Σ character,
press @½, then delete the parentheses.)
1 m Specifies the first and last rows.
FOR For each row, does the following: Extracts the last
j row in ΣDAT.
Σ- Initially this is the mth row, which corresponds to
the
mth column in the original ΣDAT.
(To key in the Σ– command, use @µ.)
OBJ→ DROP Puts the row elements on the stack. Drops the
index list { n }.
n →LIST Makes an n-element list.
50 %TILE Sorts the list and calculates its median.
j ROLLD Moves the median to the proper stack level.
NEXT Increments j and repeats the loop.
Program: Comments:
m Combines all the medians into an m-element
→ARRY vector. Restores ΣDAT to its previous value.
s STOΣ Ends the defining procedure.
»
»
Stores the program in MEDIAN.
`OMEDIAN K
Checksum: #
256d Bytes:140
Example: Calculate the median of the following data.
18 12
4 7
3 2
11 1
31 48
20 17
20 `17 `
` %OK%
The matrix is now stored in ΣDAT.
Calculate the median.
J %MEDIA%
Clear approximate mode (set exact mode) before going on to the next example.
Level 1 → Level 1
'algebraic' → 'algebr
aic'
'algebraic' →
z
Required Programs
■ MULTI (Multiple Execution) repeatedly executes the programs that EXCO provides as arguments.
Program: Comments:
«
« EXPAN » Puts a program on the stack as
the level 1 argument for
MULTI. The program
executes the EXPAN
command.
MULTI Executes EXPAN until the
algebraic object doesn’t
change.
« COLCT » Puts another program on the
stack for MULTI. The
program executes the COLCT
command.
MULTI Executes COLCT until the
algebraic object doesn’t
change.
»
Stores the program in EXCO.
`OEXCO K
Checksum: #
41162d Bytes:
65.5
Example: Expand and collect completely the expression:
2
3x4y +z +8x –5z
Enter the expression.
O3 * X *
!Ü4 *Y+Z ™+
! Ü 8 *X -5 *Z
™Q2
`
Select the VAR menu and start the program.
J %EXCO%
■ Custom menu. MNX builds a custom menu that lets you choose whether to sort for the minimum or
maximum element. Key 1, labeled %MAX%, sets flag 10. Key 2, labeled %MIN%, clears flag 10.
■ Logical function. MNX executes XOR (exclusive OR) to test the combined state of the relative value of the
two elements and the status of flag 10.
MNX program listing
Program: Comments:
«
{{ "MA Defines the option menu. %MAX%
X" sets flag 10 and continues
« 10 SF CONT » } execution.
{ "MIN" %MIN% clears flag 10 and
« 10 CF CONT » }} continues execution.
TMENU Displays the temporary menu
"Sort for MAX or MIN?" and a prompt message.
PROMPT
1 GETI Gets the first element of the array.
DO Begins the DO loop.
ROT ROT GETI Puts the index and the array in
levels 1 and 2, then gets the new
array element.
4 ROLL DUP2 Moves the current minimum or
maximum array element from
level 4 below 1, then copies both.
IF Tests the combined state of the
> 10 FS? XOR relative value of the two elements
and the status of flag 10.
THE If the new element is either less
N than the current maximum or
SW greater than the current
AP minimum, swaps the new
END element into level 1.
DROP Drops the other element off the
stack.
UNTIL Begins the DO test-clause.
-64 FS? Tests if flag –64 is set — if the
index reached the end of the
array.
END Ends the DO loop.
SWAP DROP 0 MENU Swaps the index to level 1 and
drops it. Restores the last menu.
»
Stores the program in MNX.
`OMNX K
Checksum: #
20991d Bytes:194.5
■ Logical function. MNX2 executes XOR (exclusive OR) to test the combined state of the relative value of the
two elements and the status of flag 10.
■ Custom menu. MNX2 builds a custom menu that lets you choose whether to sort for the
minimum or maximum element. Key 1, labeled %MAX%, sets flag 10. Key 2, labeled %MIN%, clears flag 10.
MNX2 program listing
Program: Comments:
«
{{ "MA Defines the temporary option menu.
X" %MAX% sets flag 10 and continues
« 10 SF CONT » } execution. %MIN% clears flag 10 and
{ "MIN"
« 10 CF CONT » }} continues execution.
TMENU Displays the temporary menu
"Sort for MAX or MIN?" and a prompting message.
PROMPT
DUP OBJ→ Copies the array. Returns the individual
array
elements to levels 2 through nm+1, and
returns the list containing n and m to level
1.
1 Sets the initial counter value.
SWAP OBJ→ Converts the list to individual elements on
the stack.
DROP * 1 - Drops the list size, then calculates the final
counter value (nm - 1).
FOR n Starts the FOR…NEXT loop.
DUP2 Saves the array elements to be tested
(initially
the last two elements). Uses the last array
element as the current minimum or
maximum.
IF Tests the combined state of the relative
> 10 FS? XOR value of the two elements and the status
of flag 10.
THE If the new element is either less than the
N current maximum or greater than the
SW current minimum, swaps the new element
AP into level
END
1.
DROP Drops the other element off the stack.
NEXT Ends the FOR…NEXT loop.
0 MENU Restores the last menu.
»
Stores the program in MNX2.
`OMNX2 K
Checksum: #
6992d Bytes:188.5
Example: Use MNX2 to find the minimum element of the matrix from the previous example:
12 56
45 1
9 14
12 `56 `˜šš
45 `1 `
9 `14 `
`
Select the VAR menu and execute MNX2.
J %MNX2%
Program: Comments:
«
→a p Store the array and program in local
« variables. Begin the main local variable
RPL Programming Examples 2-20
structure.
Checksum: #
11132d Bytes:314
Example: Apply the function, f(x) = Ax3-7 to each element x of the vector [ 3 -2 4 ].
!Ô3†2W†4`
‚å 3 QA *7 -
`J %APLY%
H#DISP ˜˜
(select small stack display to
see all vector elements.)
Program: Comments:
«
1 CF 0 RND SWAP 0 RND RCLF Clear flag 1, round both arguments
to
integers and recall flag settings.
→b n f Store the base, number and flag
settings
in local variables.
« Begin the outer local variable
structure.
STD n LOG b LOG / Sets “standard” display mode and
computes the ratio of the common
logarithms of number and base.
10 RND Rounds result to remove imprecision
in
last decimal place.
IP n 0 Find the integer part of log ratio,
recall
the original number, and initialize
the
counter variable k for use in the
DO...UNTIL loop.
→i mk Store the values in local variables.
« Begin inner local variable
" structure, enter an empty string
" and begin the
DO...UNTIL...END loop.
D
O Compute the decimal value
'm' EVAL b of the (i – k) th position in
i 'k' EVAL the string.
- ^
DUP2 MOD Makes a copy of the arguments and
computes the decimal value still
remaining that must be accounted
for
by other positions.
IF DUP 0 == Is the remainder zero and m b?
'm' EVAL b
Š
AND
THEN 1 SF If the test is true, then set flag 1.
END 'm' STO Store the remainder in m.
/ IP Compute the number of times the
current position-
value goes into the remaining
decimal
value. This is the “digit” that belongs
in
the current position.
IF DUP 10 Š Is the “digit” ≥ 10?
THEN 55 Then convert the digit into a
ELSE 48 alphabetic digit (such as A, B, C,
END + …).
CHR
+ 'k' 1 STO+ Append the digit to the current
result
string and increment the counter
variable k.
Program: Comments:
UNTIL 'm' EVAL 0 == Repeat the DO...UNTIL loop until m
=
0 (i.e. all decimal value have been
accounted for).
END Is flag 1 set? Clear the flag after the
IF 1 FS?C test.
THEN "0" + Then add a placeholding zero to the
result string.
WHILE i 'k' EVAL Begin WHILE...REPEAT loop to
- 0 ‹ determine if additional placeholding
zeros are needed.
Loop repeats as long as i≠k.
REPEAT "0" + Add an additional placeholding zero
1 'k' STO+ and increment k before repeating
the test-
clause.
E End the WHILE...REPEAT...END
ND loop, the IF...THEN...END
EN structure, and the inner local
D variable structure.
»
" base" b End the outermost
+ n SWAP + IF...THEN...ELSE...END structure
→TAG and
create the label string and tag the
result
string using the original arguments.
f STOF Restore original flag settings.
»
»
Stores the program in nBASE.
`OnBASE K
Checksum: #
54850d Bytes:433
Example: Convert 100010 to base 23.
1000 `23 J %NBASE%
Level 1 → Level 1
{ valid →
list }
{ } → (er messa i stat area)
invalid ror ge n us
list
Program: Comments:
«
IF Starts the outer conditional
structure.
OBJ→ Returns the n objects in the list to
levels 2 through (n + 1), and
returns the list size n to level 1.
DUP 2. SAME Copies the list size and tests if it’s
2.
THEN If the size is 2, moves the
DROP objects to level 1 and 2, and
IF starts the inner conditional
structure.
TYPE 6. SAME Tests if the object is a name:
returns 1 if so, otherwise 0.
SWAP TYPE 6. SAME Moves the second object to level 1,
then tests if it is a name (returns 1
or 0).
AND Combines test results: Returns 1 if
both tests were true, otherwise
returns 0.
NOT Reverses the final test result.
THEN If the objects are not both
"List needs two names, displays an error
names" DOERR message and aborts execution.
END Ends the inner conditional structure.
ELSE If the list size is not 2, drops the list
DROP size, displays an error message, and
N aborts execution.
"Illegal list
size" DOERR
END Ends the outer conditional.
»
Stores the program in NAMES.
`ONAMES K
Checksum: #
10752d Bytes:141.5
NAMES is demonstrated in the program VFY.
Level 1 → Level 1
'name' → 'name'
( message i us
a n
Required Programs
NAMES
■ NAMES verifies that a list argument contains exactly two names.
Program: Comments:
«
DUP Copies the original argument to
leave on the stack.
DTAG Removes any tags from the
argument for subsequent testing.
→ argm Stores the argument in local variable
argm.
« Begins the defining procedure.
CASE Begins the case structure.
argm TYPE 5. SAME Tests if the argument is a list.
THEN If so, puts the argument back on the
argm NAMES stack and calls NAMES to verify that
END the list is valid, then leaves the
CASE structure.
Program: Comments:
argm TYPE 6. SAME Tests if the argument is not a name.
NOT THEN If so, displays an error message and
"Not name or list" aborts execution.
DOERR
END
END Ends the CASE structure.
» Ends the defining procedure.
»
Enters the program, then stores it in
`OVFYK VFY.
Checksum: #
31403d Bytes:139.5
Example: Execute VFY to test the validity of the name argument BEN. (The argument is valid and is simply
returned to the stack.)
OBEN `
J %VFY%
Example: Execute VFY to test the validity of the list argument {BEN JEFF SARAH }. Use the name from the
previous example, then enter the names JEFF and SARAH and convert the three names to a list.
OJEFF `
OSARAH `
3 !° %LIST% %²LIST%
Execute VFY. Since the list contains too many names, the error message is displayed and execution is aborted.
J %VFY%
Level 1 → Level 1
‘symb’ → { obje }
cts
Techniques used in →RPN
■ Recursion. The →RPN program calls itself as a subroutine. This powerful technique works just like
calling another subroutine as long as the stack contains the proper arguments before the program calls itself.
In this case the level 1 argument is tested first to be sure that it is an algebraic expression before →RPN is
called again.
■ Object Type-Checking. →RPN uses conditional branching that depends on the object type of the
level 1 object.
■ Nested program Structures. →RPN nests IF…THEN…END structures inside FOR…NEXT loops
inside a IF…THEN… ELSE…END structure.
■ List Concatenation. The result list of objects in RPN order is built by using the ability of the +
command to sequentially append additional elements to a list. This is a handy technique for gathering
results from a looping procedure.
Program: Comments:
«
OBJ→ Take the expression apart.
IF If the argument count is nonzero,
OVER then store the count and the
THEN → n f function.
« Begins local variable defining
procedure.
1 n Begins FOR…NEXT loop, which
FOR converts any algebraic arguments to
i lists.
IF DUP TYPE 9. SAME Tests whether argument is an
algebraic.
THEN →RPN If argument is an algebraic, convert it to
a
list first.
END n ROLLD Roll down the stack to prepare for the
next argument.
NEXT Repeat the loop for the next
argument.
IF DUP TYPE 5. ‹ Tests to see if level 1 object is a list.
THEN 1 →LIST If not a list, then convert it to one.
END Ends the IF…THEN…END structure.
IF n 1 > Tests to see if there is more than one
argument.
THEN 2 Combine all of the arguments into a
n list.
START
+
NEXT
END f + Append the function to the end of the
list.
» End the local variable defining
procedure.
ELSE 1 →LIST SWAP DROP For functions with no arguments,
converts to a simple list.
END End the IF…THEN… ELSE…END
structure.
»
`O→RPN K Stores the program in →RPN.
Checksum: #
1522d Bytes:189.5
Example: Convert the following algebraic expression to a series of objects in RPN syntax:
'A*COS(B+ƒ(C/D))-X^3'.
OA *TB +R!Ü
C /D ™™-X Q3 `%²RPN%
Bessel Functions
This section contains a program, BER, that calculates the real part Bern(x) of the Bessel function Jn (xe3πi/4). When n
= 0,
4
x 2 x 28 4!
Berx = 1 – -+ -–
2! 2 2
Level → Level
1 1
z → Ber(z)
Program: Comments:
«
→x Creates local variable x.
« Begins outer defining procedure.
'x/2' →NUM 2 1 Enters x/2, the first counter value,
→ xover2 j sum and the first term of the series, then
creates
local variables.
« Begins inner defining procedure.
DO Begins the loop.
sum Recalls the old sum and
'sum+(-1)^(j/2)* calculates the new sum.
xover2^(2*j)/SQ(j!
)'
EVAL
2 'j' STO+ Increments the counter.
DUP 'sum' STO Stores the new sum.
UNTIL Ends the loop clause.
== Tests the old and new sums.
END Ends the loop.
sum Recalls the sum.
» Ends inner defining procedure.
» Ends outer defining procedure.
»
Stores the program in BER.
`OBER K
Checksum: #
15837d Bytes:203
Example: Calculate BER(3).
J
3 %BER%
Calculate BER(2) in algebraic syntax.
O %BER% !Ü2
N
Program: Comments:
«
'SIN(X)' STEQ Stores the expression
for sin x in EQ.
FUNCTION '-2*π' Sets the plot type and
→NUM DUP NEG XRNG x- and y-axis display
-2 2 YRNG ranges.
ERASE DRAW Erases PICT, then plots
the expression.
PICT RCL 'SINT' STO Recalls the resultant
graphics object and
stores it in SINT.
»
Stores the program in
`O SINTP K SINTP.
Checksum: #
41184d Bytes:
94
SINTP is demonstrated in the program TSA.
SETTS (Superimposing Taylor’s polynomials)
SETTS superimposes successive Taylor’s polynomials on a sine curve and stores each graphics object in a list.
Program: Comments:
«
SINTP Plots a sine curve and
stores the graphics object
in SINT.
1 17 FOR n Sets the range for the
FOR loop using local
variable n.
'SIN(X)' 'X' n Plots the Taylor’s
TAYLR STEQ ERASE polynomial of order n.
DRAW
PICT RCL SINT + Returns the plot to the
stack as a graphics object
and executes + to
superimpose the sine plot
from SINT.
2 STEP Increments the loop
counter
n by 2 and repeats the
loop.
SINT Puts the sine curve
10 graphics object on the
→LIST stack, then builds a list
'TSL' containing it and the nine
STO
graphics objects created in
the loop. Stores the list in
TSL.
»
Stores the program in
`OSETTS K SETTS.
Checksum: #
41304d Bytes:
130.5
SETTS is demonstrated in the program TSA.
TSA (Animating Taylor’s Polynomials)
TSA displays in succession each graphics object created in SETTS.
Techniques used in TSA
■ Passing a global variable. Because SETTS takes several minutes to execute, TSA does not call SETTS.
Instead, you must first execute SETTS to create the global variable TSL containing the list of graphics objects.
TSA simply executes that global variable to put the list on the stack.
■ ANIMATE. TSA uses the ANIMATE command to display in succession each graphics object from the list.
Program: Comments:
«
TSL OBJ→ Puts the list TSL on the
stack and converts it to 10
graphics objects and the
list
count.
{ { #0 #0 } .5 0 } Set up the parameters for
+
ANIMATE.
ANIMATE Displays the graphics in
succession.
11 DROPN Removes the graphics
objects
and list count from the
stack.
»
Stores the program in
`OTSA K
TSA.
Checksum: #
24644d Bytes:92.5
Example: Execute SETTS and TSA to build and display in succession a series of Taylor’s
polynomial approximations of the sine function.
Ensure Radians mode is set and execute SETTS to build the list of graphics objects. (SETTS takes several
minutes to execute.) Then execute TSA to display each plot in succession. The display shows TSA in progress.
!&H %!ANGLE% %!RAD% ( if necessary)
J %SETTS%
%TSA%
Press − to stop the animation. Press !&H %!ANGLE% %!DEG% to restore Degrees mode if desired.
Programmatic Use of Statistics and Plotting
This section describes a program PIE you can use to draw pie charts. PIE prompts for single variable data,
stores that data in the statistics matrix ΣDAT, then draws a labeled pie chart that shows each data point as a
percentage of the total.
Program: Comments:
«
RCLF → flags Recalls the current flag status
and stores it in variable flags.
«
RAD Sets Radians mode.
{{ "SLICE" Σ+ } Defines the input menu: key 1
{ } executes Σ+ to store each data
{ "CLEAR" CLΣ } point in ΣDAT, key 3 clears
{ } { } ΣDAT, and key 6 continues
{ "DRAW" CONT }} program execution after data
entry.
TMENU
"Key values Displays the temporary
into SLICE menu. Prompts for inputs.
DRAW represents the newline
restarts character (… ë) after you
program."
PROMPT enter the program on the
stack.
ERASE 1 131 XRNG Erases the current PICT and sets
1 64 YRNG CLLCD plot parameters.
"Please wait... Displays “drawing” message.
Drawing Pie
Chart"
1 DISP
Program: Comments:
(66 32) 20 0 6.28 ARC Draws the circle.
»
Stores the program in PIE.
`OPIE K
Checksum: #
16631d Bytes:737
Example: The inventory at Fruit of the Vroom, a drive-in fruit stand, includes 983 oranges, 416 apples, and
85 bananas. Draw a pie chart to show each fruit’s percentage of total inventory.
J %PIE%
983 %SLICE%
416 %SLICE%
85 %SLICE%
%DRAW%
Program: Comments:
«
PR1 Prints the command line
OBJ→ text, then converts the
string to an
object and evaluates it.
»
Stores the program in
`O αENTER K
αENTER. (Press ~‚A to
type α. You must use this
name.)
Checksum: #
127d Bytes:25.5
Program: Comments:
«
PR1 DROP Prints the command that
PRSTC caused the processing, then
drops it and
prints the stack in compact
form.
»
Stores the program in
`O ßENTER K
ßENTER. (Press ~‚B to
type ß. You must use this
name.)
Checksum: #
31902d Bytes:
28
Inverse-Function Solver
This section describes the program ROOTR, which finds the value of x at which f(x) = y. You supply the
variable name for the program that calculates f(x), the value of y, and a guess for x (in case there are multiple
solutions).
Program: Comments:
«
→ fname yvalue xguess Creates local variables.
« Begins the defining procedure.
xguess 'XTEMP' STO Creates variable XTEMP (to be solved
« XTEMP for). Enters program that evaluates f(x)
fname yvalue
- » - y.
Checksum: #
4708d Bytes:163
Example: Assume you often work with the expression
3.7x3 + 4.5x2 + 3.9x + 5 and have created the program X→FX to calculate the value:
« → x '3.7*x^3+4.5*x^2+3.9*x+5' »
You can use ROOTR to calculate the inverse function.
FINNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
N
Full Command and Function Reference 3-40