|
|
|
|
The 2007 retro HP 35s programmable calculator.
|
Curve Fitting for the HP 35s Programmable Calculator
Hewlett-Packard's new-for-2007
HP 35s programmable scientific calculator
isn't historical yet,
but it has the HP quality, look, and feel that has been missing for over a
decade. The 35s is more like HP's historical offerings than it is like their
other current products, which is a good thing. Since I used to enjoy
programming calculators when I was younger and hadn't
done so in years, I decided to tackle some largish (for a calculator) programs
on my brand new HP 35s. In addition to this curve fitting program, I've written
a matrix multi-tool.
Both the HP-41C/CV/CX Advantage Pac and the HP-42S calculator contain a curve
fitting function which can fit a sequence of (x,y) data
pairs to a straight line, logarithmic, exponential, or power curve. The much
newer HP 35s will only fit points to a straight line. The manual provides
a program to handle the other curve types, but that program has two drawbacks:
you must decide in advance which curve you want to fit to the data, and it uses
up a large number of labels (the machine has only 26).
The program presented below rectifies both of these issues. It uses only a
single label, and it allows you to fit multiple curves to the same data set.
Like the HP-41/42 programs, it also has a best-fit mode wherein it
automatically chooses the curve with the best correlation coefficient. The
available curves are:
Linear:
y = m x + b
Logarithmic:
y = m ln x + b
Exponential:
y = b em x
Power:
y = b xm
The logarithmic and power curves require that all the x values be
positive. Similarly, the exponential and power curves require that all the
y values be positive. After choosing a curve to fit, or having the
program choose one for you, you can predict values of y for given
x values, or vice versa.
Using the Program
This program is integrated as closely as possible with the HP35s' built-in
statistical registers and functions. Before entering a new data set, clear the
statistics registers by pressing
CLEAR 4Σ.
Then press XEQ C ENTER to run the curve fitting program. This will
display the main menu, which looks like this:
The menu choices are:
-
Go to data entry mode.
-
Delete the last data pair entered.
-
Go to the curve fitting menu.
-
Compute x given a y value.
-
Compute y given an x value.
Pressing R/S without selecting a choice exits the curve fitting
program (this is important, to ensure flag 10 is cleared so equations will work
in other programs).
Data Entry Mode
Press 1 R/S to go to data entry mode. The X-register of the display
will show the number of points entered so far (which will be zero if you had
just cleared the statistics registers). To enter a data pair, first enter
the y value, press ENTER, enter the x value,
and press R/S. The program will briefly display RUNNING,
and then display the updated number of points (the Y-register will contain
the y value just entered).
When you've completed data entry, press XEQ C ENTER to return to
the main menu.
Deleting the Last Entry
If you've made a mistake entering a data pair, here's how to correct it:
-
If you haven't pressed
R/S yet, just enter the correct data
pair and then press R/S.
-
If you have pressed
R/S already, press XEQ C ENTER
to return to the main menu, and then press 2 R/S to select the
delete data option. This will delete the last data pair entered, and return
you to data entry mode (the X-register will contain the number of points entered,
which will be one less than it was previously, and the Y-register will contain
the y value of the point just deleted).
Curve Fitting
If you're still in data entry mode, press XEQ C ENTER to return
to the main menu. Then press 3 R/S to display the curve fitting
menu:
The menu choices are:
-
LINear
-
LOGarithmic
-
EXPonential
-
POWer
-
Best Fit
Select the type of curve you want by pressing the corresponding number,
followed by R/S. If you select best fit (5 R/S), the
program will run for a few seconds to find the best fit. If you select a model
that is disallowed by your data (for example, a negative y data
value rules out the exponential and power curves), the program will display
INVALID MODEL. Pressing R/S will take you back to the
curve fitting menu for another try.
Next, the curve type you or the program chose will be displayed (one of
LIN, LOG, EXP, or POW).
Press R/S to continue.
The program will then display the value of m for the chosen curve
type. For example, M= -25.94960. Press R/S to
continue.
Next the program will display the corresponding b value. For example,
B= 84.94137. Once again, press R/S to continue.
Finally the absolute value of correlation coefficient r is
displayed. For example, R= 0.99973. Pressing R/S
one last time will return you to the main menu.
Pressing R/S without selecting a choice from the curve fitting
menu will return you to the main menu.
Predicting x Given y
From the main menu, press 4 R/S to go to x-prediction
mode. The program will prompt you with Y?. Enter a y
value and press R/S. The corresponding x value will be
displayed. For example, X= 23.51674. Press R/S
again to be prompted for another y value, or press XEQ C
ENTER to return to the main menu.
Predicting y Given x
From the main menu, press 5 R/S to go to y-prediction
mode. The program will prompt you with X?. Enter an x
value and press R/S. The corresponding y value will be
displayed. For example, Y= 4.73007. Press R/S
again to to be prompted for another x value, or press XEQ C
ENTER to return to the main menu.
Other Statistics
This program uses the HP 35s' built-in statistics registers (n,
Σx, etc.) to accumulate the linear coefficients, the same
way the built-in statistics functions do. This means you can do the following
at any time while using this program:
-
Perform a linear fit to your data and make predictions using it via the
L.R menu.
-
Examine the stored linear coefficient sums with the
SUMS menu.
-
Determine the x and y means using the
x,y
menu.
-
Compute the x and y sample and population
standard deviation with the
s,σ menu.
Program Listing
| Line | Instruction | Comments |
| C001← | LBL C |
| C002 | CLx |
| C003 | SF 10 |
| C004 | EQN 1+ 2− 3F 4x^ 5y^ |
| C005 | CF 10 |
| C006 | x=0? |
| C007 | RTN |
| C008 | 1 |
| C009 | x=y? |
| C010 | GTO C034 |
| C011 | R↓ |
| C012 | 2 |
| C013 | x=y? |
| C014 | GTO C029 |
| C015 | R↓ |
| C016 | 3 |
| C017 | x=y? |
| C018 | GTO C101 |
| C019 | R↓ |
| C020 | 4 |
| C021 | x=y? |
| C022 | GTO C200 |
| C023 | R↓ |
| C024 | 5 |
| C025 | x=y? |
| C026 | GTO C213 |
| C027 | XEQ C226 |
| C028 | GTO C001 |
| C029← | RCL Y |
| C030 | RCL X |
| C031 | SF 0 |
| C032 | Σ− |
| C033 | GTO C048 |
| C034← | n |
| C035 | x≠0? |
| C036 | GTO C046 |
| C037 | STO P |
| C038 | STO Q |
| C039 | STO S |
| C040 | STO T |
| C041 | STO U |
| C042 | STO V |
| C043 | STO W |
| C044 | CF 3 |
| C045 | CF 4 |
| C046← | STOP |
| C047 | Σ+ |
| C048← | R↓ |
| C049 | STO Y |
| C050 | x≤0? |
| C051 | SF 4 |
| C052 | ENTER |
| C053 | ENTER |
| C054 | LASTx |
| C055 | STO X |
| C056 | x≤0? |
| C057 | SF 3 |
| C058 | x>0? |
| C059 | LN |
| C060 | FS? 0 |
| C061 | +/− |
| C062 | STO+ P |
| C063 | LASTx |
| C064 | R↑ |
| C065 | x>0? |
| C066 | LN |
| C067 | FS? 0 |
| C068 | +/− |
| C069 | STO+ S |
| C070 | x2 |
| C071 | FS? 0 |
| C072 | +/− |
| C073 | STO+ T |
| C074 | R↓ |
| C075 | LASTx |
| C076 | × |
| C077 | STO+ U |
| C078 | R↓ |
| C079 | ENTER |
| C080 | ENTER |
| C081 | LASTx |
| C082 | × |
| C083 | FS? 0 |
| C084 | +/− |
| C085 | STO+ W |
| C086 | R↓ |
| C087 | x2 |
| C088 | FS? 0 |
| C089 | +/− |
| C090 | STO+ Q |
| C091 | R↓ |
| C092 | LASTx |
| C093 | × |
| C094 | STO+ V |
| C095 | R↓ |
| C096 | CF 0 |
| C097 | GTO C034 |
| C098← | SF 10 |
| C099 | EQN INVALID MODEL |
| C100 | CF 10 |
| C101← | CLx |
| C102 | SF 10 |
| C103 | EQN 1L 2G 3E 4P 5B |
| C104 | CF 10 |
| C105 | x=0? |
| C106 | GTO C001 |
| C107← | CF 1 |
| C108 | CF 2 |
| C109 | 1 |
| C110 | x≠y? |
| C111 | GTO C116 |
| C112 | SF 10 |
| C113 | EQN LIN |
| C114 | CF 10 |
| C115 | GTO C193 |
| C116← | R↓ |
| C117 | 2 |
| C118 | x≠y? |
| C119 | GTO C127 |
| C120 | FS? 3 |
| C121 | GTO C098 |
| C122 | SF 1 |
| C123 | SF 10 |
| C124 | EQN LOG |
| C125 | CF 10 |
| C126 | GTO C193 |
| C127← | R↓ |
| C128 | 3 |
| C129 | x≠y? |
| C130 | GTO C138 |
| C131 | FS? 4 |
| C132 | GTO C098 |
| C133 | SF 2 |
| C134 | SF 10 |
| C135 | EQN EXP |
| C136 | CF 10 |
| C137 | GTO C193 |
| C138← | R↓ |
| C139 | 4 |
| C140 | x≠y? |
| C141 | GTO C152 |
| C142 | FS? 3 |
| C143 | GTO C098 |
| C144 | FS? 4 |
| C145 | GTO C098 |
| C146 | SF 1 |
| C147 | SF 2 |
| C148 | SF 10 |
| C149 | EQN POW |
| C150 | CF 10 |
| C151 | GTO C193 |
| C152← | R↓ |
| C153 | 5 |
| C154 | x=y? |
| C155 | GTO C158 |
| C156 | XEQ C226 |
| C157 | GTO C101 |
| C158← | CF 1 |
| C159 | CF 2 |
| C160 | 1 |
| C161 | STO M |
| C162 | XEQ C301 |
| C163 | SF 1 |
| C164 | FS? 3 |
| C165 | GTO C172 |
| C166 | XEQ C301 |
| C167 | x<y? |
| C168 | GTO C171 |
| C169 | 2 |
| C170 | STO M |
| C171← | x↔y |
| C172← | SF 2 |
| C173 | FS? 4 |
| C174 | GTO C191 |
| C175 | FS? 3 |
| C176 | GTO C183 |
| C177 | XEQ C301 |
| C178 | x<y? |
| C179 | GTO C182 |
| C180 | 4 |
| C181 | STO M |
| C182← | x↔y |
| C183← | CF 1 |
| C184 | FS? 4 |
| C185 | GTO C191 |
| C186 | XEQ C301 |
| C187 | x<y? |
| C188 | GTO C191 |
| C189 | 3 |
| C190 | STO M |
| C191← | RCL M |
| C192 | GTO C107 |
| C193← | XEQ C285 |
| C194 | XEQ C290 |
| C195 | XEQ C301 |
| C196 | VIEW M |
| C197 | VIEW B |
| C198 | VIEW R |
| C199 | GTO C001 |
| C200← | INPUT Y |
| C201 | FS? 2 |
| C202 | LN |
| C203 | RCL B |
| C204 | FS? 2 |
| C205 | LN |
| C206 | − |
| C207 | RCL÷ M |
| C208 | FS? 1 |
| C209 | ex |
| C210 | STO X |
| C211 | VIEW X |
| C212 | GTO C200 |
| C213← | INPUT X |
| C214 | FS? 1 |
| C215 | LN |
| C216 | RCL× M |
| C217 | RCL B |
| C218 | FS? 2 |
| C219 | LN |
| C220 | + |
| C221 | FS? 2 |
| C222 | ex |
| C223 | STO Y |
| C224 | VIEW Y |
| C225 | GTO C213 |
| C226← | SF 10 |
| C227 | EQN INVALID CMD |
| C228 | CF 10 |
| C229 | RTN |
| C230← | FS? 1 |
| C231 | GTO C234 |
| C232 | Σx |
| C233 | RTN |
| C234← | RCL P |
| C235 | RTN |
| C236← | FS? 1 |
| C237 | GTO C240 |
| C238 | Σx2 |
| C239 | RTN |
| C240← | RCL Q |
| C241 | RTN |
| C242← | FS? 2 |
| C243 | GTO C246 |
| C244 | Σy |
| C245 | RTN |
| C246← | RCL S |
| C247 | RTN |
| C248← | FS? 2 |
| C249 | GTO C252 |
| C250 | Σy2 |
| C251 | RTN |
| C252← | RCL T |
| C253 | RTN |
| C254← | FS? 1 |
| C255 | GTO C262 |
| C256 | FS? 2 |
| C257 | GTO C260 |
| C258 | Σxy |
| C259 | RTN |
| C260← | RCL U |
| C261 | RTN |
| C262← | FS? 2 |
| C263 | GTO C266 |
| C264 | RCL V |
| C265 | RTN |
| C266← | RCL W |
| C267 | RTN |
| C268← | XEQ C230 |
| C269 | XEQ C242 |
| C270 | × |
| C271 | n |
| C272 | ÷ |
| C273 | +/− |
| C274 | XEQ C254 |
| C275 | + |
| C276 | RTN |
| C277← | XEQ C230 |
| C278 | x2 |
| C279 | n |
| C280 | ÷ |
| C281 | +/− |
| C282 | XEQ C236 |
| C283 | + |
| C284 | RTN |
| C285← | XEQ C268 |
| C286 | XEQ C277 |
| C287 | ÷ |
| C288 | STO M |
| C289 | RTN |
| C290← | XEQ C230 |
| C291 | RCL× M |
| C292 | +/− |
| C293 | XEQ C242 |
| C294 | + |
| C295 | n |
| C296 | ÷ |
| C297 | FS? 2 |
| C298 | ex |
| C299 | STO B |
| C300 | RTN |
| C301← | XEQ C268 |
| C302 | x2 |
| C303 | XEQ C277 |
| C304 | ÷ |
| C305 | XEQ C242 |
| C306 | x2 |
| C307 | n |
| C308 | ÷ |
| C309 | +/− |
| C310 | XEQ C248 |
| C311 | + |
| C312 | ÷ |
| C313 | √x |
| C314 | STO R |
| C315 | RTN |
Length: 1023, Checksum: EC35
Registers and Flags
|
Register
|
Use
|
|
B
|
|
M
|
|
P
|
|
Q
|
|
R
|
|
S
|
|
T
|
|
U
|
|
V
|
|
W
|
|
X
|
|
Y
|
|
Σx
|
|
Σx2
|
|
Σy
|
|
Σy2
|
|
Σxy
|
|
n
|
|
Flag
|
Meaning
|
|
0
|
|
1
|
|
2
|
|
3
|
|
4
|
|
10
|
Revision History
2007-Oct-17 — Initial release.
2007-Dec-16 — Changed x-prediction and y-prediction
routines to prompt for further y or x input respectively
instead of returning to the main menu each time. Use XEQ C ENTER
to return to the menu. I've found this to be more convenient than having to
repeatedly select option 4 or 5 from the main menu when making multiple
predictions.
Why I Wrote this Program
One of my first programmable calculators was a Texas Instruments SR-52 that I
purchased at Eaton's for $149 back in 1978 or so. At that time, the TI-59 had
already been introduced and the store was clearing out the SR-52. This
calculator had 224 steps of program memory, 20 registers, and a magnetic card
reader. It was only slightly less powerful than the HP-67.
One of the programs in the very comprehensive SR-52 programming manual was a
curve fitting program somewhat like the one presented here, except that it only
supported linear and exponential curves, and one had to choose the curve type
before entering data. What was interesting about this program was that it was
presented in great detail, complete with the design processes that went into
it. It was the study of TI's program and the design methods that made me really
comfortable with programming their calculator. And of course, the program was
useful.
Now, almost 30 years later, an expanded version of this program came to mind as
an ideal first project on the HP 35s, especially since my earlier HP
calculators (41CX with Advantage Pac, and 42S) had this capability built in.
A curve fitting tool such as this one is useful in some of my other work, such
as the development of MotoCalc
when I'm trying to determine a mathematical model to fit empirical data.
Other HP Calculator Programs
I've written programs for many of the HP calculators calculators in my
collection. You may be interested in some of these:
- HP 35s
- HP-41C/CV/CX
- HP-67
|
Buy Stefan a coffee!
If you've found this article
useful, consider
leaving a donation
to help support
stefanv.com
|
|
|
Disclaimer:
Although every effort has been made to ensure accuracy and
reliability, the information on this web page is presented without
warranty of any kind, and Stefan Vorkoetter assumes no liability for direct or
consequential damages caused by its use.
It is up to you, the reader, to determine the suitability of, and
assume responsibility for, the use of this information.
Copyright:
All materials on this web site, including the text, images, and HTML
mark-up, are Copyright © 2009 by Stefan Vorkoetter unless
otherwise noted. All rights reserved. Unauthorized duplication
prohibited. You may link to this site or pages within it, but
you may not link directly to images on this site, and you may
not copy any material from this site to another web site or
other publication without express written permission. You may make
copies for your own personal use.
|
|