Showing posts with label chebyshev polynomials. Show all posts
Showing posts with label chebyshev polynomials. Show all posts

Thursday, October 3, 2019

HP 42S/DM42: Error Function, Bernoulli Numbers, Chebyshev Polynomials

HP 42S/DM42:  Error Function, Bernoulli Numbers, Chebyshev Polynomials

Error Function

The program ERF calculates the error function:

erf(x) = 2 / √π * ∫ e^-(t^2) dt from t = 0 to t = x

The calculation uses the series:

erf(x) = 2 * e^(-(x^2)) / √π * Σ( 2^n * x^(2n+1) / (1 * 3 * ... * (2n+1)) from n=0 to ∞

The error function can be used to calculate the cumulative normal distributive function from 0 to x by:

ncdf(x) = 1/2 + 1/2 * erf(x/√2)

HP 42S/DM42 Program: Error Function:  ERF

00 { 46-Byte Prgm }
01▸LBL "ERF"
02 STO 01
03 ENTER
04 ×
05 2
06 ×
07 STO 02
08 1
09 STO 03
10 RCL 01
11▸LBL 01
12 RCL 02
13 RCL 03
14 2
15 +
16 STO 03
17 ÷
18 RCL 01
19 ×
20 STO 01
21 +
22 X≠Y?
23 GTO 01
24 2
25 ×
26 PI
27 SQRT
28 RCL 02
29 2
30 ÷
31 E↑X
32 ×
33 ÷
34 RTN
35 END

Examples

erf(1.34) ≈ 0.9419
erf(2.43) ≈ 0.9994

Source:

Hewlett Packard  HP-65 Math Pac 2.   1974.

Bernoulli Numbers (Approximation)

The program BERN calculates Bernoulli numbers for positive integers n > 2.   The calculator is set to Radian mode.

The formula used:

B_n = (-cos(n * π/2))^((n+2)/2) * 2 * n! * Σ(2 * j * π)^(-n) from j=1 to ∞

HP 42S/DM42 Program: Bernoulli Numbers:  BERN

00 { 70-Byte Prgm }
01▸LBL "BERN"
02 RAD
03 STO 00
04 CLX
05 STO 02
06 1
07 +
08 STO 01
09▸LBL 00
10 2
11 PI
12 ×
13 RCL× 01
14 RCL 00
15 +/-
16 Y↑X
17 STO+ 02
18 1
19 STO+ 01
20 R↓
21 ABS
22 1ᴇ-10
23 X < Y ? 
24 GTO 00
25 RCL 02
26 RCL 00
27 N!
28 ×
29 2
30 ×
31 RCL 00
32 PI
33 ×
34 2
35 ÷
36 COS
37 +/-
38 2
39 RCL+ 00
40 2
41 ÷
42 Y↑X
43 ×
44 RTN
45 END


Examples

B_2 ≈ 0.1667
B_3 returns 1.1184E-85  (0)
B_4 returns -0.0333
B_6 returns 0.0238

Source:

Keith Oldhma, Jan Myland, and Jerome Spanier An Atlas of Functions 2nd Ed.  Springer: New York.  ISBN: 978-0-387-48806-6

Chebyshev Polynomials

The program CHEBY calculates the value of a first-order Chebyshev polynomial using the recurring formula where:

T_n(x) = 2 * x * T_n(x) - T_(n-1)(x)

where T_0(x) = 1,  T_1(x) = x

HP 42S/DM42 Program:  Chebyshev Polynomials: CHEBY

00 { 51-Byte Prgm }
01▸LBL "CHEBY"
02 STO 02
03 2
04 ×
05 STO 03
06 R↓
07 STO 00
08 X=0?
09 GTO 00
10 1
11 STO 01
12 STO- 00
13 X=Y?
14 GTO 01
15▸LBL 02
16 RCL 03
17 RCL× 02
18 RCL- 01
19 RCL 02
20 STO 01
21 R↓
22 STO 02
23 DSE 00
24 GTO 02
25 RTN
26▸LBL 00
27 1
28 RTN
29▸LBL 01
30 RCL 02
31 RTN
32 .END.


Instructions

Input n,  [ENTER], input x, XEQ CHEBY

Examples
T_3(0.4):  3, ENTER, 0.4, XEQ CHEBY
Result:  -0.9440

T_4(1.5):  4, ENTER, 1.5, XEQ CHEBY
Result:  23.5000

Source

Hewlett Packard  HP-65 Math Pac 2.   1974.



Happy programming,

Eddie

All original content copyright, © 2011-2019.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.


Thursday, February 14, 2019

TI-86: Angle Between Vectors, Vandermonde Matrix, Least Squares Algorithm, Chebyshev Polynomials (1st Kind)


TI-86: Angle Between Vectors, Vandermonde Matrix, Least Squares Algorithm, Chebyshev Polynomials (1st Kind)

The TI-86 has one of the best interfaces for a graphing calculator I ever had the joy to work with. The TI-86 is an update of the TI-85. Here are some programs for the TI-86.

Angle Between Vectors

The program vangle calculates the angle between two angles. The angle is calculated in degrees.



TI-86 Program vangle
(75 bytes)

Prompt V1
Prompt V2
Disp cos⁻¹ (dot(V1,V2)/(norm V1*V2))

Example: [2, -3, 4] and [8, 1, -2]
Angle: 83.5823268926°

Vandermonde Matrix

The program vander creates a matrix based on a list of coefficients.

Example: {x, y, z} produces the matrix

[ [x^0, x^1, x^2], [y^0, y^1, y^2], [z^0, z^1, z^2] ]



TI-86 Program vander
(125 bytes)

Input “List: “, L1
dimL L1 → N
{N, N} → dimL MA
For(R, 1, N)
For(C, 1, N)
L1(R)^(C-1) → MA(R,C)
End
End
Disp “MA=”
Pause MA

Example: {2, 4, 7}
Result: [ [1, 2, 4], [1, 4, 16], [1, 7, 49] ]

Least Square Algorithm

The program LSQ taxes the matrices X and Y (Y is a one-column matrix), and calculates
(X^T X)^-1 (X^T Y).

LSQ is used to fit statistical fits with least squares.



TI-86 Program LSQ
(118 bytes)

Disp “Least Squares”
Input “Matrix X: “,MX
Input “Matrix Y: “,MY
(MX^T * MX)^-1 * (MX^T * MY) → ML
Disp “ML= “
Pause ML

Example:
MX = [ [1, 3, 2.0], [1, 4, 2.3], [1, 5, 2.6], [1, 8, 2.9] ]
MY = [ [1.6], [1.8], [2.1], [2.3] ]

Results:
ML = [ [-0.14444444449], [-1.666666667E-2], [0.88888888889] ]

Chebyshev Polynomials (1st Kind)

The program tcheby calculates the numerical value of the Chebyshev polynomials of the 1st Kind given its point, X, and the order, N.

TI-86 Program tcheby
(127 bytes)

Prompt X,N
If X>1 : Goto A
If X<-1 :="" b="" goto="" span="">
cos(N * cos⁻¹ X) → A
Goto C
Lbl A
cosh(N * cosh⁻¹ X) → A
Goto C
Lbl B
(-1)^N * cosh(N * cosh⁻¹ X) → A
Lbl C
Disp A

Example:
X = -2.5, N = 4, Result: 263.5
X = 0.5, N = 4, Result: -0.5
X = 2.5, N = 4, Result: 263.5

Eddie

All original content copyright, © 2011-2019. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

First Look: HP 16C Collector's Edition

 First Look: HP 16C Collector's Edition I just got the HP 16C Collector's Edition.   This is the famous HP 16C that specializes in c...