(45) Straight-Line Fitting - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (45) Straight-Line Fitting (/thread-14447.html) (45) Straight-Line Fitting - SlideRule - 01-31-2020 12:34 PM   The Hewlett-Packard HP-45 pocket calculator includes a key ∑+ that accumulates and stores the sums ∑1(=n), ∑x, ∑x², and ∑y, with another key that directs the calculator to compute the mean of x and its standard deviation. An independent y entry can be summed simultaneously.   Another problem occurring often in data reduction is to find the least-squares slope and intercept of a straight line and their standard deviations. For this case the sums ∑xy and ∑y² are needed, in addition to those provided by the ∑+ key. An efficient (though not unique) procedure for accumulating the necessary sums with the HP-45 is the following: In succession, key in the data pairs (xi, yi) together with keyboard steps in the order: xi, ENTER↑, ENTER↑, yi, ENTER↑, STO + 3, X², STO + 4, R↓, ×, x⇄y, ∑+. After processing all data pairs in this manner the sums can be recalled from storage as needed, with ∑y in R3, ∑y² in R4, n in R5, ∑x² in R6, ∑x in R7 and ∑xy in R8. It is then a simple task to calculate the slope, intercept, and standard deviations using the well-known least-squares formulas. If the standard deviations are not required, the sum ∑y² can be omitted, with a saving of six keyboard steps (steps 5, 9, 10, 11, 12, 13 in the previous list). source: AJP Volume 42, COMPUTER NOTES, Straight-Line Fitting with an HP-45 Calculator, W. C. ELMORE (Swarthmore College), April 1974, pg. 253 BEST! SlideRule RE: (45) Straight-Line Fitting - Thomas Klemm - 03-10-2024 02:26 PM (01-31-2020 12:34 PM)SlideRule Wrote:  If the standard deviations are not required, the sum ∑y² can be omitted, with a saving of six keyboard steps (steps 5, 9, 10, 11, 12, 13 in the previous list). This leaves us with: $$x_i$$ ENTER ENTER $$y_i$$ STO + 3 × x<>y Σ+ But we can do slightly better with: $$y_i$$ STO + 3 $$x_i$$ × LASTx Σ+ Registers These registers are used: $$\begin{array}{|c|c|} \hline \text{Register} & \text{Value} \\ \hline 1 & a \\ 2 & b \\ 3 & \sum{y} \\ 5 & n \\ 6 & \sum{x^2} \\ 7 & \sum{x} \\ 8 & \sum{xy} \\ \hline \end{array}$$ Formula (01-31-2020 12:34 PM)SlideRule Wrote:  It is then a simple task to calculate the slope, intercept, and standard deviations using the well-known least-squares formulas. The following formulas are used to calculate slope $$a$$ and intercept $$b$$: \begin{align} a &= \frac{\sum{xy} - \frac{\sum{x} \sum{y}}{n}}{\sum{x^2} - \frac{\sum{x}^2}{n}} \\ \\ b &= \frac{\sum{y} - a \cdot \sum{x}}{n} \\ \end{align} Program These steps calculate both $$a$$ and $$b$$: Code: # calculate a RCL Σ RCL 3 × RCL 5 ÷ − RCL 6 RCL 7 x² RCL 5 ÷ − ÷ STO 1 # calculate b RCL 7 × RCL 3 x<>y − RCL 5 ÷ STO 2 Example $$\begin{array}{|c|c|c|c|c|c|c|} \hline n & 1 & 2 & 3 & 4 & 5 & 6 \\ \hline C & 40.5 & 38.6 & 37.9 & 36.2 & 35.1 & 34.6 \\ \hline F & 104.5 & 102 & 100 & 97.5 & 95.5 & 94 \\ \hline \end{array}$$ Initialise the Registers CLEAR STO 3 Enter the Data 104.5 STO + 3 40.5 × LASTx Σ+ 102 STO + 3 38.6 × LASTx Σ+ 100 STO + 3 37.9 × LASTx Σ+ 97.5 STO + 3 36.2 × LASTx Σ+ 95.5 STO + 3 35.1 × LASTx Σ+ 94 STO + 3 34.6 × LASTx Σ+ Result RCL 1 1.76 RCL 2 33.53 HP-25 The statistics functions also store $$\sum{xy}$$. In addition to that it is programmable. Both makes entry of the data and calculating the best fit much easier. Initialise the Registers CLEAR REG Enter the Data 104.5 ENTER 40.5 Σ+ 102 ENTER 38.6 Σ+ 100 ENTER 37.9 Σ+ 97.5 ENTER 36.2 Σ+ 95.5 ENTER 35.1 Σ+ 94 ENTER 34.6 Σ+ Program Code: 01: 24 04    : RCL 4 02: 24 03    : RCL 3 03: 71       : / 04: 24 04    : RCL 4 05: 14 21    : f mean 06: 61       : * 07: 24 05    : RCL 5 08: 41       : - 09: 24 07    : RCL 7 10: 14 21    : f mean 11: 61       : * 12: 24 06    : RCL 6 13: 41       : - 14: 71       : / 15: 23 01    : STO 1 16: 14 21    : f mean 17: 61       : * 18: 41       : - 19: 23 02    : STO 2 Data Code: DATA 8 00: 0 01: 1.760149049 02: 33.5271295 03: 6 04: 593.5 05: 22093.4 06: 8306.23 07: 222.9 Python Just in case you want to compare the results: Code: C = [40.5, 38.6, 37.9, 36.2, 35.1, 34.6] F = [104.5, 102, 100, 97.5, 95.5, 94] Σx = sum(C) Σy = sum(F) Σx2 = sum([x**2 for x in C]) Σxy = sum([x*y for x, y in zip(C, F)]) n = len(C) a = (Σxy - Σx*Σy/n) / (Σx2 - Σx**2/n) b = (Σy - a*Σx) / n a, b, Σx, Σy, Σx2, Σxy, n (1.7601490488333176, 33.52712950250892, 222.9, 593.5, 8306.23, 22093.4, 6) References HP-45 Emulator HP-45 Applications Book: Curve Fitting pp. 77 HP-25 Emulator HP-25 Applications Programs: Curve Fitting - Linear Regression pp. 87 HP Calculators with native C-F temperature conversion Straight-Line Fitting with an HP-45 Calculator RE: (45) Straight-Line Fitting - Namir - 03-11-2024 11:28 AM And that is why I bought the HP-55 in 1975 as my first HP calculator because it had built-in linear regresssion! Earlier that year, I had done linear regression (ln(y) vs 1/(x+constant)) calculations on a Sharp scientific calculator with one memory register ... and I swore it was the first and last time I do linear regression in that painfull way!!! Namir