Friday, July 28, 2017

HP Prime and TI-84 Plus CE: Cosine Regression

HP Prime and TI-84 Plus CE:  Cosine Regression

Introduction

The program COSREG attempts to fit data to the curve:

Y = A*cos(C*(X-D)) +B

The user will provide the x-data, y-data, and the period of the data.  Radians mode will be used. 

HP Prime Program: COSREG
Arguments  x list, y list, estimated period

EXPORT COSREG(L1,L2,P)
BEGIN
// EWS 2017-07-25
// x list, y list, period
// Estimated Cosine Regression
// Y = A*COS(C(X-D))+B
// Radians mode

HAngle:=0;

LOCAL C:=2*π/P;
LOCAL I:=POS(L2,MAX(L2));
LOCAL D:=L1(I);
LOCAL S:=SIZE(L1);
LOCAL L0:=MAKELIST(1,X,1,S);
L1:=COS(C*L1-D);


LOCAL M1,M2,M3;
M1:=list2mat(CONCAT(L0,L1),S);
M1:=TRN(M1);
M2:=list2mat(L2,S);
M2:=TRN(M2);
M3:=CAS.LSQ(M1,M2);
LOCAL A:=M3[2,1];
LOCAL B:=M3[1,1];

RETURN {"A*COS(C(X-D))+B",
A,C,D,B};

END;


TI-84 Plus CE Program:  COSREG

"EWS 2017-07-26"
Disp "COSINE FIT"
Disp "A*cos(C*(X-D))+B"
Radian
Input "X: ",L1
Input "Y: ",L2
Input "PERIOD: ",C
2π/C→C
0→K
Repeat L2(K)=max(L2)
K+1→K
End
L1(K)→D
cos(C*L1-D)→L1
LinReg(a+bx) L1,L2
Disp "{A,C,D,B}"
Pause {a,C,D,b}
Disp "r=",r

The coefficients are returned in a list.

Cosine Regression: Y = A * COS(C*(X – D)) + B

Example 1

X = {0, π/4, π/2, 3π/4 ,1}
Y = {5, 2, -1, 2, 5}
Period:  π

Results:

A = 3
C = 2
D = 0
B = 2
(TI-84 Plus CE:  r = 1)
(Y = 2 COS(2X) + 3 – exact match)


Example 2

X = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Y = {2.5, 4.2, 6.8, 4.4, 2.24, 4.35, 7, 4.41, 2.6, 4.2, 6.76, 4.48}
Period = 4


Results:

A = 4.495
C = 1.570796327
D = 7
B = -1.37970927
(TI-84 Plus CE:  r = -0.6213278417)
(An OK… fit)


Eddie


This blog is property of Edward Shore, 2017

HP Prime and TI-84 Plus CE: EDM Slope Reduction

HP Prime and TI-84 Plus CE:  EDM Slope Reduction

Introduction



The program EDMSLOPE calculates the following:

* curvature correction due to the Earth
* horizontal distance from observer to reflector at both elevation of the observer and at sea level
* change of in elevation from observer to reflector

Given:

*  slope distance between observer to reflector
*  height of both the observer and reflector’s instruments
*  the elevation the observer
*  zenith angle:  angle from directly above to the slope distance line

Notes:

All measurements are given in feet.  Assume that the correction factor due to light refraction is negligible.  The radius of Earth is approximately 20,902,231 feet.

Formulas (see diagram above)

C = asin((90* sin Z * S)/(π * radius))

H = (S * sin(Z – C))/cos(Z)

L = H * (radius / (radius + D))

E = (S * cos Z / cos C) + M – F

HP Prime Program EDMSLOPE

EXPORT EDMSLOPE()
BEGIN
// 2017-07-26 EWS
// in feet
// Radius of Earth:
// based off 6371 km, to nearest
// integer
LOCAL R:=20902231;

// Degrees
HAngle:=1;

LOCAL Z,S,M,D,F;
INPUT({Z,S,M,D,F},"EDM Slope
Reduction",{},{"Zenith Angle",
"Slope Distance (ft)",
"Instrument Height (ft)",
"Elevation (ft)",
"Reflector Height (ft)"});

// Curvature correction
LOCAL C:=ASIN((90*SIN(Z)*S)/(π*R));

// Horizontal dist-elevation
LOCAL H:=(S*SIN(Z-C))/COS(C);

// Horizontal dist-Sea level
LOCAL L:=H*R/(R+D);

// Elevation change
LOCAL E:=S*COS(Z)/COS(C)+M-F;

PRINT();
PRINT("Curvature correction");
PRINT(C);
PRINT("Horizontal dist-elevation");
PRINT(H);
PRINT("Horizontal dist-Sea level");
PRINT(L);
PRINT("Elevation change");
PRINT(E);

END;

TI-84 Plus CE Program EDMSLOPE

"2017-07-26 EWS"
"IN FEET"
20902231→R
Degree
Input "ZENITH ANGLE: ",Z
Input "SLOPE DIST (FT): ",S
Input "INSTRUMENT (FT): ",M
Input "ELEVATION (FT): ",D
Input "REFLECTOR (FT): ",F
sin^-1((90*sin(Z)*S)/(π*R))→C
(S*sin(Z-C))/cos(C)→H
H*R/(R+D)→L
S*cos(Z)/cos(C)+M-F→E
Disp "CURVATURE CORR.",C
Disp "HORIZ-ELEVATION",H
Pause
Disp "HORIZ-SEA LEVEL",L
Disp "ELEVATION CHANGE",E


Example

Observer:  D = 1,238.32 feet, instrument height = M = 3.5 feet
Slope distance:  S = 1,474 feet, at a zenith angle of 86.11°
Reflector height = F = 3 feet

Results:
Curvature correction = C = 0.1154831691
Horizontal Distance-Elevation = H = 1,470.40255 ft
Horizontal Distance-Sea Level = L = 1,470.315444 ft
Elevation Change = E = 100.4980742 ft

Sources:

TI Programmable 58/59 Surveying – Texas Instruments 1977

Eddie


This blog is property of Edward Shore, 2017

Sunday, July 23, 2017

Retro Review: Texas Instruments TI-68

Retro Review:  Texas Instruments TI-68



Company:  Texas Instruments
Years:  1989 - 2002
Type:  Scientific, Formula Programming
Memory:  440 bytes, in 55 8-bit registers
Operating System: Algebraic
Memory Registers: Up to three characters

Batteries:  1 CR2032

Hard to Find:  One with a good screen is hard to find.  It took me two tries to do so. 

Features

The TI-68 is a popular scientific calculator and back in the day, it had pretty much most of the bells and whistles (with the obvious exception of graphing):

*  Base Conversions and Boolean Logic (9 bits, signed integer, max binary value: 511, minimum binary value: -511)
*  Linear Regression
*  Polynomial Solver: Quadratic, Cubic, Quartic
*  Simultaneous Equation Solver:  Up to 5 x 5 systems
*  Expanded Storage Arithmetic:  +, -, *, /, ^, Δ%, and, or, xor
* Number Parts: signum, integer, fraction, real, imaginary, absolute value (but strangely enough, no argument/angle function, did TI run out of space?)

Complex Numbers

What I love about the TI-68 is how complex numbers are integrated in the operating system.  There is no need to switch to a separate mode.  Best of all, the TI-68 handles exponential, logarithmic, power, and trigonometric functions with complex numbers. 

The simultaneous solver also allows for complex numbers.  This is indeed rare, as not even most graphing calculators’ simultaneous solving apps allow for complex numbers as coefficients.  (Note:  The HP Prime’s simult command allows for complex numbers)

Complex numbers on the TI-68 are notated as such:

Rectangular:  (x, y)
Polar:  (r θ)

Part extraction of complex numbers works slightly different:  real and imag extract the real and imaginary portions of the complex number, regardless of setting.  

Choosing the Precision?

The TI-68 allows for two precision settings:  10 digits or 13 digits.   The display uses 10 digits.  I think this is a rarity, if not a completely unique feature, since calculators in general uses an accuracy of 13 to 15 digits automatically.

I tested a couple of integrals and the precision setting does not affect the length of time either way.  Both integrals were calculated in about 3 seconds. 

Test Integral 1:  ∫ (T^3 * e^(-T) dT, 0, 100, intervals = 6)
Test Integral 2:  ∫ (X^2/(X^2 + X – 1) dX, 25, 75, intervals = 12)

Integration

The TI-68 uses the Simpson’s Rule during integration.  To integrate, during evaluation, designate the variable to be integrated by pressing [CLEAR], [ 3rd ], [ Σ+ ] (dx). 

Formula Programming

The TI-68 has formula programming.  There are no loops or comparison tests, but all variables are local, meaning their stored values can be transferred between formulas.

Something I learned about the TI-68: you can have variables up to 3 characters. 

Each formula can be evaluated (right hand side of the equation) by pressing [SOLVE].  Each variable can accept a real or complex number.

Keyboard



Let’s talk about the keyboard.  The keys are nice and responsive.  But get a look of all those shift keys!  There are two shift keys, [ 2nd ], [ 3rd ], along with an inverse key [ INV ].  This is reminiscent of the 1974 Hewlett Packard HP 65 calculator, where its shift keys were [ f ], [ f^-1 ], and [ g ].

Here is what the inverse [INV] key (it’s like a “4th” key) operates on:

Key
[ INV ] Key
Key
[ INV ] Key
[2nd] (DRG>)
D: D to R
R: R to G
G: G to D
Angle conversion*
D:  D to G
R:  R to D
G:  G to R

[2nd] (>DD)
Convert: DMS>DD
Convert: DD>DMS
[HYP]
Hyperbolic
Inverse hyperbolic
[2nd] (P>R)
Polar to Rectangular
Rectangular to Polar
[SIN] Sine
Arcsine (sin^-1)
[2nd] (in-cm)
Inches to centimeters
Centimeters to inches
[COS] Cosine
Arccosine (cos^-1)
[2nd] (gal-l)
Gallons to liters
Liters to gallons
[TAN] Tangent
Arctangent (tan^-1)
[2nd] (lb-kg) Pounds to kilograms
Kilograms to pounds
[ Σ+ ] add a data point
Erases the last data point (Σ-)
[3rd] (°F-°C)  Fahrenheit to Celsius
Celsius to Fahrenheit


‘*  D = Degrees, R = Radians, G = Grads.  Angle mode is unaffected.  To change angle mode, press [ 3rd ] (DRG) (it cycles Degrees, Radians, Grads).

The Basic Cousin, TI-60X

TI-60X (left), TI-68 (right).  Their memory capacities are shown.


In 1991, Texas Instruments released a more basic version of the TI-68, the TI-60X.  For more details, check out this link:  http://edspi31415.blogspot.com/2017/01/retro-review-ti-60x.html.

Final Verdict

I regret not getting the TI-68 when it first came out (which would have consisted in asking my family for one).  It’s finally nice to have one and it’s worth the hype and praise it got. 

Eddie


This blog is property of Edward Shore, 2017

Tuesday, July 18, 2017

Expanding Linear Regression

Expanding Linear Regression


Introduction – Linear Regression

All scientific calculators that have two-variable dimensions has curve fitting.  The most (and universal) curve fitting mode is linear regression.   Linear regression attempts to model bivariate data ((x,y)) to a line by the equation:

y = a + bx

Where a is the y-intercept (ITC) and b is the slope (SLP).  The correlation coefficient, r, is calculated by:

r = SXY / √(SX2 * SY2) where
SX2 = Σ(x – μx)^2
SY2 = Σ(y – μy)^2
SXY = Σ( (x – μx)*(y – μy) )
μx = mean of x values, μy = mean of y values

Source:  HP 17B II Financial Calculator Owner’s Manual, Corvallis, OR  1989.

The best fits are were |r| = 1, or as close to 1 as we can get.  If r is close to 0, the first is not good.  Note that the formula for correlation does not involve the slope or intercept.  We can use this to our advantage.

“Linearize” the Equation

If you can “linearize” equation, we can use the linear regression mode to fit a variety of curves.  The ultimate goal is of course:

y = a + bx

Now, most calculators use the form for linear equations.  However, some switch the a and b around and use the form, like the TI-30X IIS, 

y = ax + b

Hewlett Packard calculators has the b for the intercept and m for slope. 

For clarity, I will use the form Y = ITC + SLP*X. 

  
Calculator Comparison

To demonstrate how we can use the linear regression to fit a variety of curves, I use two calculators:



Sharp EL-W516T which offers 7 regressions.

TI-30X IIS which offers just linear regression.  I use the TI-30X IIS to use the linearized form.

[screen shot of the calcs] 

Sharp EL-W516T
TI-30X IIS
SLP
B
a
ITC
A
b
Linear Regression Equation
y = a + bx
y = ax + b

Screen shots are generated from a Casio fx-CG50. 


Logarithmic Regression:  y = a + b ln x



Translation to Linear:  This is pretty simple, we already have the form that we need, except we note that we have ln x instead of x.  Set the following variables as such:

X = ln x, Y = y, a = ITC, b = SLP

Example data set:
x
y
X = ln x  (8 decimal places)
1.0
2.00
0
1.3
3.00
0.2623643
1.7
4.15
0.5306283
2.3
5.34
0.8329091
2.8
6.10
1.0296194
3.5
7.00
1.252763

To use the linear regression mode, enter the data as such:  (ln x, y).  Everything else remains the same.

Logarithmic Regression Results:
ITC = a = 1.990614146
SLP = b = 4.003372076
r = 0.99905634

Comparing results between using the Logarithmic regression with the Sharp EL-W516T and the Linear regression with the TI-30X IIS, I get the same results.




Exponential Regression:  y  = a*e^(b*x)



Translation to linear: 

y = a*e^(b*x)
ln y = ln (a*e^(b*x))
ln y = ln a + ln(e^(b*x))
ln y = ln a + b*x

We got our form of Y = ITC + SLP*X where:
X = x, Y = ln y, a = e^ITC, b = SLP

When using the linear regression mode, enter data as (x, ln y).  When calculating a, note that

ITC = ln a
e^ITC = a

Example data set:
X
y
Y = ln y  (8 decimal places)
1.0
109.2
4.693181
1.3
363.0
5.8944028
1.7
1795.6
7.4930945
2.3
19794.3
9.8931493
2.8
146260.8
11.893147
3.5
2405208.5
14.693147

Results:
ITC = 0.693829923;  a = 2.001365951
SLP = b = 3.99773169
r = 0.999999992

Inverse Regression: y = a + b/x



Translation to Linear:  Like logarithmic regression, we pretty much have the equation pretty much set, with the exception of 1/x instead of x.  Hence:

X = 1/x, Y = y, a = ITC, b = SLP


 When using the linear regression mode, enter data as (1/x, y). Everything else remains.

Example data set:
X
y
X = 1/x  (8 decimal places)
1.0
6.00
1
1.3
5.55
0.7692308
1.7
5.16
0.5882353
2.3
4.86
0.4347826
2.8
4.73
0.3571429
3.5
4.58
0.2857143

Results:
a = ITC = 4.005529472
b = SLP = 1.993191341
r = 0.999733184

Power Regression:  y = a*x^b



Translation to Linear:  X = ln x, Y = ln y, A = e^a, b

y = a*x^b
ln y = ln (a*x^b)
ln y = ln a + ln(x^b)
ln y = ln a + b * ln x

So we have the following: X = ln x, Y = ln y, ITC = ln a, SLP = b.

In the linear regression mode, enter data as (ln x, ln y). Also, a = e^ITC. 

Example data set:
x
y
X = ln x
Y = ln y
1.0
3.95
0
1.3737156
1.3
6.76
0.2623643
1.9110229
1.7
11.56
0.5306283
2.4475509
2.3
21.09
0.8329091
3.048799
2.8
31.40
1.0296194
3.4468079
3.5
49.00
1.252763
3.8918203

Results:
ITC = 1.379195052; a = e^ITC = 3.971703326
SLP = b = 2.007158681
CORR = r = 0.999991481
   

Table of Linear Regression Equivalents to Curve Fitting


Regression

X

Y

ITC

SLP

Logarithmic: y = a + b * ln x

ln x

y

a

b

Exponential: y = a*e^(b*x)

x

ln y

e^a

b

Inverse: y = a + b/x

1/x

y

a

b

Power: y = a*x^b

ln x

ln y

e^a

b

General Exponential: y = a * b^x

x

ln y

e^a

e^b

Simple Logistic: y = 1/(a + b*e^(-x))

e^(-x)

1/y

a

b

Square Root Linear: y = √(a + b*x)

x

y^2

a

b

Cosine: y = a + b*cos(ω(x – ϕ))

With

ϕ = the point (x) nearest to zero where the trough or peak begins

ω = (2*π)/period (radians) or

ω = 360°/period (degrees)

cos(ω(x – ϕ))

y

a

b


Eddie


This blog is property of Edward Shore, 2016