Showing posts with label error function. Show all posts
Showing posts with label error function. Show all posts

Sunday, September 24, 2023

HP 15C: Error Function and Lower Tail Normal Cumulative Function

 HP 15C:   Error Function and Lower Tail Normal Cumulative Function






Formulas Used 



Error Function 


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




CDF (Cumulative Normal Distribution Function)


CDF(x) = 1 ÷ √(2 * π) * ∫( e^(-t^2) dt, t = -∞ to t = x)


We can use the error function to calculate CDF(x):


0.5 - erf(x), for x < 0


0.5 + erf(x), for x ≥ 0



This program uses the integration function inside the program.  We will need 23 free memory registers to run the program, which will be terminated at the end of program execution.



Labels used:


B:  erf function (error function)

C:  lower tail normal CDF 

4:  function used for integration


(Of course, feel free to use the labels you want, just be mindful to make the appropriate adjustments)




HP 15C Program Code:  Error Function and Lower Tail Normal Cumulative Function


Steps:  35

Bytes:  40



Step # :  Key Code :  Key


001 : 42,21,12:  LBL B

002 :  0  :   0

003 : 34 : x<>y

004 : 42,20, 4 : ∫_x_y 4

005 : 43, 32 : RTN


006 : 42,21,13 : LBL C

007 : 43,30, 2 :  TEST 2 (x<0)

008 : 43, 4, 0 : SF 0

009 : 43, 16 :  ABS

010 : 0   :   0

011 : 34 :  x<>y

012 : 2   :  2

013 : 11 :  √

014 : 10 :  ÷

015 : 42,20, 4 : ∫_y_x 4

016 : 2   :  2

017 : 10 :  ÷

018 : 48 :  .

019 : 5   :  5

020 : 34 :  x<>y

021 : 43, 6, 0 :  F?0

022 : 16  : CHS

023 : 40  :  +

024 : 43, 5, 0 : CF 0

025 : 43, 32 : RTN


026 : 42,21, 4 : LBL 4

027 : 43,11 : x^2

028 : 16 :  CHS

029 : 12 :  e^x

030 :  2  :  2

031 :  20  : ×

032 :  43,26 : π

033 :  11 :  √

034 :  10 :  ÷

035 :  43,32 : RTN




Examples


x = -0.7:  erf (N/A), CDF ≈ 0.2420


x = 0.7:  erf ≈ 0.6778, CDF ≈ 0.7580


x = 1:   erf ≈ 0.8427,  CDF ≈ 0.8413


x = 2.5  erf ≈ 0.9996,  CDF ≈ 0.9938



Eddie



All original content copyright, © 2011-2023.  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. 


Saturday, July 15, 2023

Casio fx-3900PV: Program Bank

Casio fx-3900PV:  Program Bank



Here are a few programs for the Casio fx-3900Pv.  These programs should also work on the fx-3600P/fx-180P series (as long as the number of steps is less than 38) except that we can review and edit the steps on the fx-3900Pv.   See my review of the fx-3900Pv here:  


http://edspi31415.blogspot.com/2023/05/retro-review-casio-fx-3900pv.html



Error Function


The program allows the you to calculate the error function:


erf(b) = ∫( 2 * e^(-x²) ÷ √θ dx, x = 0, x = b)


Code:


001   Min

002   MR

003   x²

004   +/-

005   eˣ

006   ×

007   2

008   ÷

009   π

010   √

011   =



To calculate the erf(b):  run integral mode (Mode 1).  Select the program, enter n to get 2^n divisions by pressing [SHIFT] [RUN].   Next, enter 0 [RUN], then b [RUN].


Examples:  n = 6,  (2^6 = 64 divisions)


erf(1.5):

a = 0,  b = 1.5,  Result:  9.661051 * 10^-1


erf(0.4):

a = 0, b = 0.4,  Result:  4.28392 * 10^-1


erf(3.9):

a = 0, b = 3.9,  Result:  9.9999997 * 10^-1




Range and Height of a No-Air Projectile


R = v^2 * sin(2 * θ) ÷ g


H = v^2 * sin^2 θ ÷ (2 * g)


where:

R = range of the projectile (m)

H = maximum height of the projectile (m)

v = initial velocity (m/s)

θ = angle in degrees

g = Earth's gravity = 9.80665 m/s^2


Inputs:

K1 = v

K2 = θ


Outputs:

K3 = R

K4 = H


Used:

K5 = g


Code:


001   DEG  ( MODE 4 )

002   9

003   .

004   8

005   0

006   6

007   6

008   5

009   Kin 5

010   Kout 1

011   x²

012   ÷

013   Kout 5

014   ×

015   (

016   2

017   ×

018   Kout 2

019   )

020   SIN

021   =

022   Kin 3

023   HLT

024   Kout 1

025   x²

026   ×

027   Kout 2

028   SIN

029   x²

030   ÷

031   2

032   ÷

033   Kout 5

034   =

035   Kout 4


Examples:


Inputs:  K1 = 11.9 m/s,  K2 = 40°

Outputs:

R = 14.22082219 m

H = 2.98317166312 m


Inputs:  K1 = 11.9 m/s,  K2 = 60°

Outputs:

R = 12.0558115 m

H = 5.415075484 m

   


Magnitude and Phase of a LCR Series Circuit


Z = √(R² + (w * L - 1 ÷ (w * C))² )


θ = arctan((w * L - 1 ÷ (w * C)) ÷ R)


where:

Z = magnitude (Ω)

θ = phase angle (degrees)

R = resistance (Ω)

L = inductance (H)

C = capacitance (F)

w = angular frequency, where w = 2 * π * f

f = frequency


Inputs:  R, f, C, L

M = f  

K2 = L

K3 = R

K4 = C


Outputs:

K1 = w

K5 = Z

K6 = θ


Note that the source (see Source section below) omitted the square root in the formula for Z.  The above formula is correct.


Code:


001   DEG    (Mode 4)

002   MR

003   Kin 1

004   2

005   Kin× 1   (shown as K×1)

006   π

007   Kin× 1    (shown as K×1)

008   Kout 1

009   ×

010   Kout 2

011   -

012   (

013   Kout 1

014   ×

015   Kout 4

016   )

017   1/x

018   =

019   Kin 6

020   Kout 3

021   x²

022   +

023   Kout 6

024   x²

025   =

026   √

027   Kin 5

028   HLT

029   (

030   Kout 6

031   ÷

032   Kout 3

033   )

034   tan⁻¹

035   =

036   Kin 6



Example:


Inputs:

f = 100 Hz,  [ Min ]

L = 4 * 10^-3 H  (store in K2)

R = 6300 Ω  (store in K3)

C =  5 * 10^-6 F  (store in K4)

Outputs:

Z = 6307.909915 Ω  (K5)

θ = -2.869631956° (K6)



Quadratic Equations:  Real Roots


Solve the equation: 


x^2 + K1 * x + K2 = 0


The roots are:


x = (-K1 ± √(K1^2 - 4 * K2)) ÷ 2


Inputs:

K1 = coefficient of x

K2 = constant


Outputs:

K4 = root 1

K5 = root 2


Used:  K3 = √(K1^2 - 4 * K2)


Code:


001    (

002    Kout 1

003    x²

004    -

005    4    

006    ×

007    Kout 2

008    )

009    √

010    Kin 3

011    (

012    Kout 3

013    +/-

014    -

015    Kout 1

016    )

017    ÷

018    2

019    =

020    Kin 4

021    HLT

022    +

023    Kout 3

024    =

025    Kin 5



Example:


Solve x^2 - 19 * x + 10 = 0

Inputs:

K1 = -19

K2 = 90

Outputs:  9, 10



Source 

(for Range and Height of a No-Air Projectile and Magnitude and Phase of a LCR Series Circuit):


Rosenstein, Morton.   Computing With the Scientific Calculator.  Casio.  1986



Enjoy,


Eddie 


All original content copyright, © 2011-2023.  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. 


Saturday, November 2, 2019

HP 12C: Error Function Approximation

HP 12C: Error Function Approximation

Introduction

The program for the HP 12C calculator approximates the error function defined as

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

by using the series

erf(x) = (2*x) / √π * Σ( (-x^2)^n / (n!*(2*n+1)), n = 0 to ∞)

In the approximation, up to 69 terms are calculated for the sum (the loop stops when n = 69). 

Since there is no π constant on the HP 12C, the approximation 355/113 for π is used.

HP 12C Program Error Function

Step;   Key Code;  Key
01;  44,1;  STO 1
02;  35;   CLx
03;  44, 2;  STO 2
04;  44, 3;  STO 3
05;  45, 1;  RCL 1   
06;  2;   2
07;  21;  y^x
08;  16;  CHS
09;  45, 2;  RCL 2
10;  21;  y^x
11;  45, 2;  RCL 2
12;  43, 3;  n!
13;  45, 2;  RCL 2
14;  2;   2
15;  20;  *
16;  1;  1
17;  40;  +
18;  20;  *
19;  10;  ÷
20;  44,40,3;  STO+ 3
21;  43, 35;  x=0
22;  43,33,31;  GTO 31
23;  1;  1
24;  44,40,2;  STO+ 2
25;  45, 2;  RCL 2
26;  6;   6
27;  9;   9
28;  43,34;  x≤y
29;  43,33,31; GTO 31
30;  43,33,05; GTO 05
31;  45,3;  RCL 3
32;  45,1;  RCL 1
33;  20;  *
34;  2;  2
35;  20;  *
36;  3;  3
37;  5;  5
38;  5;  5
39;  36;  ENTER
40;  1;  1 
41;  1;  1
42;  3;  3
43;  10;  ÷
44;  43,21;  √
45;  10;  ÷
46;  43,33,00;  STO 00

Examples

(FIX 5)

erf(0.5) ≈ 0.52050

erf(1.6) ≈ 0.97635

erf(2.3) ≈ 0.99886

Source

Ball, John A.  Algorithms for PRN Calculators  John Wiley & Sons: New York  1978  ISBN (10) 0-471-0370-8

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, 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.


Sunday, September 22, 2019

The Error Function and Normal Distribution: Norm(x) and Erf(x)

The Error Function and Normal Distribution:  Norm(x) and Erf(x)

Introduction

The error function is defined as:

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

norm(x) = 1/√(2*π) * ∫( e^(-z^2/2) dx, -∞, x)

We were assuming that the mean μ = 0 and variance σ = 1.

Derivation:  norm(x) in terms of erf(x)

norm(x)

= 1/√(2*π) * ∫( e^(-z^2/2) dx, -∞, x)

= 1/√(2*π) * ∫( e^(-z^2/2) dx, -∞, 0) + 1/√(2*π) * ∫( e^(-z^2/2) dx, 0, x)

= 1/2 + 1/√(2*π) * ∫( e^(-z^2/2) dx, 0, x)

-----------------------------
Substitution:

t^2 = z^2/2
t = z/√2
√2 * t = z
√2 dt = dz

z = 0,  t = 0
z = x,  t = x/√2
-----------------------------

= 1/2 + 1/√(2*π) * ∫( e^(-z^2/2) dx, 0, x)

= 1/2 + 1/√(2*π) * ∫(√2 *  e^(-t^2) dt, 0, x/√2)

 = 1/2 + 1/√π * √π/2 * 2/√π * ∫(√2 *  e^(-t^2) dt, 0, x/√2)

 = 1/2 + (1/√π * √π/2) * (2/√π * ∫(√2 *  e^(-t^2) dt, 0, x/√2))

 = 1/2 + 1/2 * 2/√π * ∫(√2 *  e^(-t^2) dt, 0, x/√2)

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

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

norm(x) - 1/2 = 1/2 *erf(x/√2)

2 norm(x) - 1 = erf(x/√2)

Let t = x/√2, then:

2 norm(√2 * t) - 1 = erf(t)

Summary

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

erf(t) = 2 * norm(√2 * t) - 1 

with μ = 0 and σ = 1.

Examples

norm(1) = 1/2 + 1/2 * erf(1/√2) ≈ 0.841344746069

erf(1) = 2 * norm(√2) - 1 ≈ 0.84270079295


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, August 22, 2019

Integrals with the Error (erf) Function

Integrals with the Error (erf) Function

Introduction - Integrals Involving the Erf Function

erf(x) = 2 / √π * ∫( e^-(t^2), t, 0, x)   ** graphing calculator syntax


Problem 1:  ∫( e^-(t^2), t, a, x) with a > 0 and x > a

∫( e^-(t^2), t, 0, x) = ∫( e^-(t^2), t, 0, a)  + ∫( e^-(t^2), t, a, x)

 ∫( e^-(t^2), t, 0, x) - ∫( e^-(t^2), t, 0, a)  = ∫( e^-(t^2), t, a, x)

√π/2 * 2/√π * (  ∫( e^-(t^2), t, 0, x) - ∫( e^-(t^2), t, 0, a) ) =   √π/2 * 2/√π * ∫( e^-(t^2), t, a, x)

√π/2 * ( 2/√π *  ∫( e^-(t^2), t, 0, x) - 2/√π *  ∫( e^-(t^2), t, 0, a) ) =   ∫( e^-(t^2), t, a, x)

√π/2 * ( erf(x) - erf(a)  ) =   ∫( e^-(t^2), t, a, x)


 ∫( e^-(t^2), t, a, x) = √π/2 * (erf(x) - erf(a))

Problem 2:  ∫( e^-((t + b)^2), t, 0, x)

∫( e^-((t + b)^2), t, 0, x)

Substitutions:
u = t + b
u - b = t
du = dt

Limits:
t  = x, u = x + b
t = 0, u = b

∫( e^-((t + b)^2), t, 0, x)

= ∫( e^-(u^2), u, b, b+x)

Note √π/2  * 2 /√π  = 1

=  √π/2  * 2 /√π  * ∫( e^-(u^2), u, b, b+x)

Using problem 1 as an aid to help us get:

= √π/2  * (eft(x + b) - erf(x))

∫( e^-((t + b)^2), t, 0, x) = √π/2  * (eft(x + b) - erf(x))

Problem 3:   ∫( e^(-a*t^2 + b), t, 0, x)

∫( e^(-a*t^2 + b), t, 0, x)

= ∫( e^(-a*t^2) * e^b, t, 0, x)

e^b is a constant.

= e^b * ∫( e^(-a*t^2), t, 0, x)

Substitutions:
u = √a * t
u^2 = a * t^2

du = dt * √a
du / √a = dt

Limits:
t  = x, u = √a * x
t = 0, u = 0

= e^b * 1/√a  * ∫( e^(-u^2), u, 0, √a * x)

= e^b * 1/√a  * √π/2  * 2 /√π  * ∫( e^(-u^2), u, 0, √a * x)

= e^b * 1/√a  * √π/2  * erf(√a * x)

∫( e^(-a*t^2 + b), t, 0, x) = e^b * 1/√a  * √π/2  * erf(√a * x)

Problem 4:  ∫( e^-(ln^2 t)/t, t, 1, x)

∫( e^-(ln^2 t)/t, t, 1, x)

Substitutions:
u^2 = ln^2 t
u = ln t
du = 1/t dt
t  du = dt

1/t * t = 1

Limits:
 t = x, u = ln x
t = 1, u = ln 1 = 0

= ∫( e^-(u^2), u, 0, ln x)

= √π/2  * 2 /√π  * ∫( e^-(u^2), u, 0, ln x)

= √π/2  * erf(ln x)


∫( e^-(ln^2 t)/t, t, 1, x) = √π/2  * erf(ln x)

Problem 5:  2/√π * ∫(e^(t^2), t, 0, x)

2/√π * ∫(e^(t^2), t, 0, x)

Substitutions:
-u^2 = t^2
u^2 = -t^2
u = i * t   where i = √ -1
du = i dt
- i du = dt  because 1/i = -i

Limits:
t = x, u = i *x
t = 0, u = 0

= 2/√π * ∫(e^-(u^2) * -i, t, 0, i*x)

= 2/√π * -i * ∫(e^-(u^2), t, 0, i*x)

= -i * erf(i*x)

2/√π * ∫(e^(t^2), t, 0, x) = -i * erf(i * x)

Alternative substitution:
-u^2 = t^2
i * u = t
u = -i * t
du = -i dt
i du = dt

Limits:
t = x, u = -i *x
t = 0, u = 0

Then we get

2/√π * ∫(e^(t^2), t, 0, x)

= 2/√π * i * ∫(e^-(u^2), t, 0,- i*x)

= i * erf(-i * x)

In summary....

2/√π * ∫(e^(t^2), t, 0, x) = -i * erf(i * x) = i * erf(-i * x)

These are several of integrals using the erf function. - 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.

Sunday, January 14, 2018

Curve Fitting: Fitting to the Curve y = a*e^(-b*x^2)

Curve Fitting:  Fitting to the Curve y = a*e^(-b*x^2)

Introduction

This blog is to fit data to the equation y = a*e^(-b*x^2).  A famous example is the equation:

y = 2/√(π) * e^(-x^2)

which the indefinite integral of 0 to real number t is the basis of the error function. 

Turning y = a*e^(-b*x^2) into a Linear Form

y = a*e^(-b*x^2)

ln y = ln (a*e^(-b*x^2))

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

Y’ = A’ + B’ * x^2

Where:  Y’ = ln y,  A’ = ln a, B’ = -b

Results:  e^A, B

Inputs:  square each of the data points of X, take the natural logarithm for Y.

Example

I use the HP Prime for this example. 

Fit the data below to the curve y = a*e^(-b*x^2).

x
y
-0.75
0.642931
-0.35
0.998284
0
1.128379
0.35
0.998284
0.62
0.768267
0.94
0.466951

Adjust data:

x
C1 = x^2
y
C2 = ln y
(to 6 decimal places)
-0.75
0.5625
0.642931
-0.441718
-0.35
0.1225
0.998284
-0.001717
0
0
1.128379
0.120782
0.35
0.1225
0.998284
-0.001717
0.62
0.3844
0.768267
-0.263618
0.94
0.8836
0.466951
-0.761531

Running the linear_regression(C1^2, LN(C2)) returns the matrix [B’, A’]:

[-0.998758, 0.120567]

The results must be adjusted: e^(.120567) = 1.128136

Final adjusted curve:  y ≈ 1.128136 * e^(-0.998758*x^2)

Using the Exponential Regression Model

We can use exponential regression function as an alternative to the linear regression. 

For the input, square all of the x data.

x
C1 = x^2
C2 = y
-0.75
0.5625
0.642931
-0.35
0.1225
0.998284
0
0
1.128379
0.35
0.1225
0.998284
0.62
0.3844
0.768267
0.94
0.8836
0.466951

The function exponential_regression from the HP Prime that fits data to the model y = B * A^x. 

To fit this model asked, y = a*e^(-b*x^2), we will need to take the natural logarithm of B. 

Hence:  exponential_regression(C1^2, C2) returns

[0.368337, 1.128136]

With ln(0.368337) ≈ -0.998757, the required equation is

y = 1.128136*e^(-0.998757*x^2)

Summary

To fit data to the curve y = a*e^(-b*x^2):

Linear Regression, Y’ = A’ + B’*X’
Exponential Regression, Y’ = B’*A’^X’
Input:  X’ = x^2, Y’ = ln y
Output:  a = e^A’, b = B’
Input: X’ = x^2, Y’ = y
Output: a = A’, b = ln(B’)

Eddie


This blog is property of Edward Shore, 2018.

Wednesday, November 29, 2017

Casio fx-5800p Special Functions

Casio fx-5800p Special Functions

Programs

An Alternate Way of Extracting the Fraction and Integer Parts of a Number

The fraction part is stored in F, and the integer part is stored in I.  This algorithm can be used when a calculator or programming language does not have a fractional part or integer part function.

This program assumes the program is in Radians mode.

Casio fx-5800P Program FPIP

Rad
0.5    → F
? → X
cos(πX) = 0 ⇒ Goto 1
Abs(X) → F
tan^-1 (tan (πF)) ÷ π → F
X < 0 ⇒ -F → F
Lbl 1
F /right-triangle ([Shift] [x^2])
X – F → I

Bernoulli Numbers

The program BERNOULLI approximates the Bernoulli number of nth order.

Casio fx-5800P Program BERNOULLI

? → N
If N = 0
Then
1 → B
Goto 1
IfEnd
If Frac(N ÷ 2) ≠ 0
Then
Int( Abs(N – 2) ÷ (N – 2) – 1) ÷ 4 → B
Goto 1
IfEnd
Σ ( (2*J*π)^(-N), J, 1, 250) * (-1)^(N ÷ 2 + 1) * 2 * N! → B
Lbl 1
B

Euler Numbers

The program EULERNUM calculates the Euler number of order n.

Casio fx-5800P Program EULERNUM

? → N
0 → E
Frac(N ÷ 2) ≠ 0 ⇒ Goto 1
(2 ÷ π)^(N + 1) * N! ÷ 5 → E
-1 – 10 * Int(E) → E
Frac(N ÷ 4) ≠ 0 ⇒ Goto 1
4 – E → E
N ≠ 0 ⇒ Goto 1
1   → E
Lbl 1
E

Custom Formulas

How to create custom formulas:  [MODE], 5.  PROG, 1.  NEW, give the name, 3. Formula

To calculate the custom formula, press [CALC], enter the value for T (ignore X because X is a dummy variable).

Sine Integral:   SI:  ∫( sin(X)/X dX,0,T)

Dawson Integral:  DAWSON:  ∫(e^(T^2-X^2) dX,0,T)

Bessel Function, with order N:  BESSEL:  1/π * ∫(cos(T * sin(X)-N*X) dX,0,T)

Error Function:  ERF:  2/√π * ∫(e^(-X^2) dX,0,T)

Fresnel Sine:  FRESSIN:  ∫( sin(π*X^2/2) dX,0,T)

Fresnel Cosine: FRESCOS: ∫( cos(π*X^2/2) dX,0,T)

Source:

Jerome Spainer and Keith B. Oldham  An Atlas of Functions Hemisphere Publication Corporation: Washington  1987  ISBN 0-89116-5738-8

Eddie

This blog is property of Edward Shore, 2017

Monday, January 16, 2017

Fun with the Sharp EL-5500 III

Fun with the Sharp EL-5500 III


Even though the EL-5500 III has one programming space, we can fit many programs in it.  One of the great features is that how programs take relatively little space.  Obviously you can change the line numbers, the sequence of lines are most important.

Euclid Algorithm – RUN 10

10 PAUSE “GCD: A>B”
18 INPUT “A: “; A, “B: “; B
20 C = A – INT(A/B)*B
30 IF C = 0 THEN 40
32 A = B
34 B = C
36 GOTO 20
40 PRINT “GCD = “; B
46 END

My first program on the EL-5500 III.  We can probably make the coding more efficient.  PRINT with a proceeding WAIT pauses execution until [ ENTER ] is pressed.

The command END ends the program execution and allows the space to have more than one routines.

Examples:
A: 100, B: 20.  Result:  GCD = 20
A: 63, B: 60.  Result:  GCD = 3

Binomial Expansion – RUN 50

50 PAUSE “(Ax + B)^n”
55 INPUT “A: “;A, “B: “;B, “N: “,N
60 FOR I=0 TO N
65 C = FACT(N) / (FACT(I) * FACT(N-I))
70 T = C * A^(N-I) * B^I
75 PRINT T; “*x^”; N-I
80 NEXT I
85 END

The command PAUSE shows the text for about 0.85 seconds.

This program displays each coefficient one at time.

Example:  A = 3, B = -2, N = 3.  Expand (3 – 2x)^3.
Result:  27, -54, 36, -8  (27x^3 – 54x^2 + 36x – 8)

Days Between Dates – RUN 100

100 PAUSE “Days between Dates”
112 INPUT “M1:”; M1, “D1:”; D1, “Y1:”; Y1
113 INPUT “M2:”; M2, “D2:”; D2, “Y2:”; Y2
115 M = M1: D = D1: Y = Y1
117 GOSUB 140
119 F1 = F
121 M = M2: D = D2: Y = Y2
123 GOSUB 140
125 F2 = F
127 N = F2 – F1
129 PRINT “# DAYS: “; N
131 END
140 IF M > 2 THEN 150
142 X = 0: Z = Y – 1
144 GOTO 160
150 X = INT(.4 * M + 2.3): Z = Y
160 F = 365 * Y + 31* (M – 1) + D + INT(Z/4) – X
165 RETURN

If I had it my way, every calculator that isn’t a basic 4-function calculator will have a Days Between Dates function.

Example:  November 4, 1986 to January 12, 2017
M1:  11, D1: 4, Y1: 1986 
M2: 1, D2: 12, Y2: 2017
Result:  11,027

Power of a Complex Number – RUN 200

This calculates (a + bi)^n

200 PAUSE “(A + Bi)^n”
203 INPUT “A: “;A, “B: “;B, “n: “;N
205 IF A<>0 THEN 210
207 A = A^N: GOTO 220
210 R = √(A^2 + B^2)^N
212 T = ATN(B/A)*N
214 A = R * COS T
216 B = R * SIN T
220 PRINT A; “+”; B; “i”
225 END

The command <> means not equals (≠) and ATN is the arctangent function.

Examples:
(2 + 3i)^3:  A = 2, Bi = 3.  Result: -46 + 9i
(-5 + i)^2:  A = -5, Bi = 1.  Result: 24 – 10i

Error Function Approximation – RUN 250

250 PAUSE “APPROX ERF(X)”
252 CLEAR
254 DIM A(3)
256 E = 0
260 INPUT “X > 0 : “; X
262 T = RCP(1 + .47047 * X)
270 FOR I = 1 TO 3
272 READ A(I)
274 E = E + A(I) * T^I
276 NEXT I
280 DATA .3480242, -.0958798, .7478556
290 E = 1 – E * EXP(-(X^2))
292 PRINT “ERF : “, F
294 CLEAR
296 END

Use the command CLEAR to clear all the variables.  At this point, I will use this command both at the beginning and end of calculations.  The command RCP is the reciprocal.  Finally, EXP is from e^x function, not the [EXP] key.

Example:
x = 0.53, erf ≈ 0.546455764
x = 0.88, erf ≈ 7.86708E-01 = 0.786708

Keep in mind this approximation is accurate 2.5 parts in 10^-5.

Source:  Smith, Jon M.  Scientific Analysis on the Pocket Calculator  John Wiley & Sons: New York. 1975.  ISBN 0-471-79997-1

Simpson’s Rule – RUN 300

Calculates the numeric integral ∫ f(x) dx from x = L to x = U.

You can edit the function at line 342.  In BASIC-PRO mode, call up line 342 by LIST 342.  Edit as desired.

300 PAUSE “Simpsons Rule”
302 CLEAR
304 INPUT “LOWER: “; L, “UPPER: “; U, “PARTS (even): “; N
306 RADIAN
308 X = L: GOSUB 340: T = F
310 X = U: GOSUB 340: T = T + F
314 H = (U – L)/N
320 FOR I = 1 TO N-1
322 X = L + I * H: GOSUB 340
324 R = I/2 – INT(I/2)
326 IF R = 0 THEN LET T = T + 2*F
328 IF R <> 0 THEN LET T = T + 4*F
330 NEXT I
332 T = T * H/3
334 PRINT “Integral: “, T
336 CLEAR
338 END
340 REM f(x)
342 F = [insert f(X) here]
344 RETURN

Note:  I finally learn why LET is included in BASIC.  In an IF-THEN statement, if you want to assign a value or calculation to a variable, you will need a LET command.

I use a REM (remark) command on line 340.  REM is for comments and nothing said after REM is executed.

Examples:

342 F = X^2 – 1
L = -2, U = 2, N = 14.  Result:  1.333333334 (Actual: 4/3 ≈ 1.333333333)

342 F = √(X – 1)
L = 2, U = 3, N = 14.  Result: 1.218951372 (Actual:  ≈ 1.2158951416)

Dancing Star Demo – RUN 400

400 PAUSE “Dancing Star Demo”
405 CLEAR
410 S$ = “         “  (9 spaces)
415 FOR I = 1 TO 48
420 N = RND 7 + 1
425 T$ = MID$(S$, 1, N-1) + “*” + MID$(S$, N+1, 9-(N+1))
430 WAIT 15: PRINT T$
440 NEXT I
445 BEEP 3: CLEAR: END

This is just show a dancing asterisk (*) on the screen.

WAIT 15: PRINT T$:   This causes the string T$ after a quarter of a second.  WAIT operates in 1/60 seconds.

RND n:  This is the random command.  If n = 1, the result is between 0 and 1. 

BEEP n:  This makes the EL-5500 III beep n times. 

Eddie

This blog is property of Edward Shore, 2017.



HP 15C: The I Squared Rule and HHC 2026

HP 15C: The I Squared Rule and HHC 2026 HHC 2026 Videos Before we start, the videos for HHC 2026 are up! Please take a look, ev...