(45) Straight-Line Fitting
03-10-2024, 02:26 PM
Post: #2
 Thomas Klemm Senior Member Posts: 2,159 Joined: Dec 2013
RE: (45) Straight-Line Fitting
(01-31-2020 12:34 PM)SlideRule Wrote:  If the standard deviations are not required, the sum ∑ 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:
1. $$x_i$$
2. ENTER
3. ENTER
4. $$y_i$$
5. STO
6. +
7. 3
8. ×
9. x<>y
10. Σ+

But we can do slightly better with:
1. $$y_i$$
2. STO
3. +
4. 3
5. $$x_i$$
6. ×
7. LASTx
8. Σ+

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
 « Next Oldest | Next Newest »

 Messages In This Thread (45) Straight-Line Fitting - SlideRule - 01-31-2020, 12:34 PM RE: (45) Straight-Line Fitting - Thomas Klemm - 03-10-2024 02:26 PM RE: (45) Straight-Line Fitting - Namir - 03-11-2024, 11:28 AM

User(s) browsing this thread: 1 Guest(s)