Showing posts with label Casio fx-9750GIII. Show all posts
Showing posts with label Casio fx-9750GIII. Show all posts

Sunday, January 7, 2024

Casio fx-9750GIII: Equatorial to Galactic Coordinates Conversions

Casio fx-9750GIII: Equatorial to Galactic Coordinates Conversions 



Introduction



Coordinate Systems


There are several coordinate systems used by astronomers to determine the placement of celestial objects, such as stars, galaxies, planets, and black holes, in our night skies.


Two coordinate systems are:



Equatorial System:


The equatorial system frames the University with the Earth's center as the center of the university.  


Right Ascension (α):   Customarily stated in hours, minutes, and seconds (H°M°S°).   Each hour is equivalent to 15 degrees.  The right ascension at 0 hours is aligned where the sun would be during the vernal equinox.  


Declination (δ):   Customarily stated in degrees, minutes, and seconds (D°M°S°).  



Galactic System:


The galactic system frames the University, aligning it with the Milky Way Galaxy.  The center of the system is our Sun.  The galactic longitude at 0 degrees aligns with the center of our galaxy (Sagittarius A*).


Galactic Longitude (l):  Customarily stated in degrees, minutes, and seconds (D°M°S°).  


Galactic Latitude (b):  Customarily stated in degrees, minutes, and seconds (D°M°S°).  



The program EQT2GLT converts equatorial coordinates (α, δ) to galactical coordinates (l, b).  


b = arcsin( cos δ × cos 27.1284° × cos(α - 192.8595°) + cos δ × cos 27.1284° )


l = 

arctan( ( sin δ - sin b × sin 27.1284° ) ÷ ( cos δ × sin(α -192.8595°) × cos 27.1284°) + 32.93117169°

=  atan2((sin δ - sin b × sin 27.1284°), (cos δ × sin(α -192.8595°) × cos 27.1284°)) + 32.93117169°


b is in hours, from -90 to 90 degrees.


l is in degrees, from 0 to 360 degrees.  Be sure to consider quadrants in your calculation.   



The program GLT2EQT converts galactical coordinates (l, b) to equatorial coordinates (α, δ).  


δ = arcsin( (cos b × cos 27.1284° × sin( l - 32.93117169° ) + sin b × sin 27.1284° )


α = arctan( ( cos b × cos( l - 32.93117169° ) ) ÷ ( sin b × cos 27.1284° - cos b × sin 27.1284° × sin( l - 32.93117169° ) ) + 192.8595°

= atan2((cos b × cos(l - 32.93117169°),(sin b × cos 27.1284° - cos b × sin 27.1284° × sin(l-32.93117169°)) + 192.8595°


δ is in degrees, from -90 to 90 degrees.  


α is in hours, from 0 to 24 hours.  Be sure to consider quadrants in your calculation.   Remember that 1 hour is equivalent to 15 degrees.  



The equations are from Practical Astronomy With Your Calculator by Peter Duffett-Smith, with the constants updated for J2000.0.


From the "Conversion of coordinates" page of Tobias Westmeier's webpage, the J2000.0 of the north pole are:


α0 ≈ 192.8595°  (12h 51m 26.28s)

δ0 ≈ 27.1284°  (27°07'42.24")


(Sources:   Duffett-Smith, Westmeier - refer to the Source section)


For more details, please refer to the Equatorial to Galactic Coordinates:  Updating the Constants posted on January 6, 2023.   


Casio fx-9750GIII Program:  EQT2GLT

a+bi
Deg
"EQUATORIAL -> GALACTIC"
"J2000.0"
"R.A. IN H°M°S°"
?->A
"DEC. IN D°M°S°"
?->D
15*A->A
192.8595->R
27.1284->E
32.93117169->C
sin^-1 (cos D*cos E*cos (A-R)+sin D*sin E)->B
sin D-sin B*sin E->Y
cos D*sin (A-R)*cos E->X
Arg (X+Yi)->T
Y<⇒T+360->T
T+C->L
ClrText
"B="
B ▶DMS◢
"L="
L ▶DMS

Note:  The calculator is set to the following modes: degrees, and rectangular complex mode. 

Example:  

The star Hamal in the constellation Aries (Alpha Arietis) is located approximately at:
α = 2 hours, 7 minutes, 10 seconds
δ = 23°27'44"

Enter hours-minutes-seconds and degrees-minutes-seconds by pressing [ OPTN ], [ F6 ] ( > ), [ F5 ] (ANGL), [ F4 ] (°''').

b:  -36°12'22.12"
L:  144°34'33.2"


Casio fx-9750GIII Program:  EQT2GLT

a+bi
Deg
"GALACTIC_->_EQUATORIAL"
"J2000.0"
"B (LAT) IN D°M°S°"
?->B
"L (LONG) IN D°M°S°"
?->L
192.8595->R
27.1284->E
32.93117169->C
sin^-1 (cos B*cos E*sin (L-C)+sin B*sin E)->D
cos B*cos (L-C)->Y
sin B*cos E-cos B*sin E*sin (L-C)->X
Arg (X+Yi)->T
R+T->A
A>360⇒A-360->A
A÷15->A
ClrText
"R.A.="
A ▶DMS◢
"DEC="
D▶DMS

Example:

The star Regulus in the constellation Leo (Alpha Leonis) is located approximately at:  
b:  48°56'03"
l:  226°25'36"

α:  10°08'22.31"  (10 hr, 8 min, 22.31 sec)
δ:  11°58'02.14"

Note:  The results are approximated.  


Sources


"Equatorial coordinate system"  Wikipedia.  Last Edited April 10, 2023.  Accessed December 10, 2023.  https://en.wikipedia.org/wiki/Equatorial_coordinate_system


"Galactic coordinate system" Wikipedia.  Last Edited April 21, 2023.  Accessed November 23, 2023.  https://en.wikipedia.org/wiki/Galactic_coordinate_system


Duffett-Smith, Peter.  Practical Astronomy With Your Calculator  Second Edition.  Cambridge University Press: Cambridge, UK.  1981.  

ISBN: 0 521 28411 2  (paperback) 


National Aeronautics and Space Administration (NASA).   "Coordinate Calculator"  NASA/IPAC Extragalactic Database.  Operated by the California Institute of Technology.  2023.   Accessed November 26, 2023.  https://ned.ipac.caltech.edu/coordinate_calculator?in_csys=Equatorial&in_equinox=J2000.0&obs_epoch=2000.0&ra=17h45m40.036s&dec=-29d00m28.17s&pa=0.0&out_csys=Galactic&out_equinox=J2000.0


Westmeier, Tobias.   "Conversion of coordinates"  Homepage of Tobias Westmeier.  The University of Western Australia.  Last Modified 26 September 2023.   Accessed November 26, 2023.   https://www.atnf.csiro.au/people/Tobias.Westmeier/index.php


Eddie


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

HP 15C and Casio fx-9750GIII: Solving Recurrence Relations

HP 15C and Casio fx-9750GIII:   Solving Recurrence Relations


Introduction


The following program attempts to find a closed-form formula to the recurrence relation:


(I)

a_n + P × a_n-1 + Q × a_n-2 = 0


or 


(II)

a_n = -P × a_n-1 + -Q × a_n-2


Without loss of generality, usually P and Q in the latter form (II) are presented as positive constants.  


Two initial conditions are provided, a_0 and a_1 (where n = 0 and n = 1, respectively).  


Equation (I) can be transformed into the characteristic equation:


x^2 + P × x + Q = 0


where the roots are:


x = ( -P ± √(P^2 - 4 × Q)) ÷ 2


and the roots are x = S and x = T.  Assume that the roots S and T are real numbers.


If S ≠ T, the solution is in the form of:


a_n = B × S^n + C × T^n


where B and C are determined by the equations:

a_0 = B + C

a_1 = B × S + C × T


If S = T, the solution is in the form of:


a_n = B × S^n + C × n × S^n


where B and C are determined by the equations:

a_0 = B

a_1 = S × (B + C)




HP 15C:  Solving Recurrence Relations 


Store values to the following registers:


R1 = P 

R2 = Q

R3 = I 

R4 = J


Output registers:


R5 = S

R6 = T

R7 = B

R8 = C


Code:

Step :  Key :  Code


001 :  LBL A :  42,21,11    (solve for S, T)

002 :  RCL 1 :  45,1

003 :  x^2 :  43,11

004 :  4  :   4

005 : RCL× 2 : 45,20, 2

006 : - :  30

007 : √  : 11

008 : STO 0 : 44, 0

009 : RCL- 1 : 45,30, 1

010 : 2 : 2

011 : ÷ : 10

012 : STO 5 : 44, 5

013 : RCL- 0 : 45,30, 0

014 : STO 6 : 44, 6

015 : RCL 5 : 45, 5

016 : TEST 6 : 43,30, 6  (x ≠ y)

017 : GTO 1 : 22, 1


018 : RCL 3 : 45, 3  (if S = T, solve for B, C)

019 : STO 7 : 44, 7

020 : RCL 4 : 45, 4

021 : RCL 5 : 45, 5

022 : RCL× 7 : 45,20, 7

023 : - : 30

024 : RCL÷ 5 : 45,10, 5

025 : STO 8 : 44, 8

026 : GTO 2 : 22, 2


027 : LBL 1 : 42,21, 1  (if S≠T, solve for B, C)

028 : RCL 6 : 45, 6

029 : RCL× 3 : 45,20, 3

030 : RCL- 4 : 45,30, 4

031 : RCL 6 : 45, 6

032 : RCL- 5 : 45,30, 5

033 : ÷ : 10

034 : STO 7 : 44, 7

035 : RCL 5 : 45, 5

036 : CHS : 16

037 : RCL× 3 : 45,20, 3

038 : RCL+ 4 : 45,40, 4

039 : RCL 6 : 45, 6

040 : RCL- 5 : 45,30, 5

041 : ÷ : 10

042 : STO 8 : 44, 8


043 : LBL 2 : 42,21, 2 (view results)

044 : RCL 7 : 45, 7

045 : R/S : 31

046 : RCL 5 : 45, 5

047 : R/S : 31

048 : RCL 8 : 45, 8

049 : R/S : 31

050 : RCL 6 : 45, 6

051 : RTN : 45, 32


Casio fx-9750GIII Program: RCHAR


Text file listing:


'ProgramMode:RUN

"2023_-_09_-_26 EWS"

ClrText

"SOLVE"

"A(N) _+_ P_*_A(N_-_1) "

"_+_ Q_*_A(N_-_2) = 0"

"P"?->P

"Q"?->Q

"A(0)"?->I

"A(1)"?->J

(-P+Sqrt(P^<2>-4Q))/2->S

(-P-Sqrt(P^<2>-4Q))/2->T

If S<>T

Then 

(T*I-J)/(T-S)->B

(-S*I+J)/(T-S)->C

ClrText

Locate 1,3,"B_*_S_^_N _+_ C_*_T_^_N"

Else 

I->B

(J-S*B)/S->C

ClrText

Locate 1,3,"B_*_S_^_N _+_ C_*_N_*_T_^_N"

IfEnd

Locate 1,4,"B="

Locate 4,4,B

Locate 1,5,"S="

Locate 4,5,S

Locate 1,6,"C="

Locate 4,6,C

Locate 1,7,"T="

Locate 4,7,T



Notes:  

This is the text file from Casio fx-9750GIII.  

_:  space

->:  store  (→)

^<2>:  ^2

Sqrt:  √

<>: ≠


Examples


1.  a_n - 11 × a_n-1 + 24 = 0,  a_0 = 3, a_1 = 8


Characteristic Equation:  x^2 - 11x + 24 = 0


Roots:  S = 3, T =-8


Different Roots


3 = B + C

8 = 3 × B - 8 × C


B = -0.2, C = 3.2


Solution:


a_n = -0.2  × 8^n + 3.2 × 3^n



2.  a_n + 6 × a_n-1 + 9 × a_n-2 = 0, a_0 = 2, a_ 11


Characteristic Equation:  x^2 + 6x + 9 = 0


Roots:  S = T = -3


Same Roots


2 = B 

10 = -3 × (B + C)


B = 2, C = -16/3 ≈ -4.3333


Solution:


a_n = 2 × (-3)^n - 16/3 × n × (-3)^n



3.  a_n - 6 × a_n-1 + 4 × a_n-2 = 0, a_0 = 1, a_1 = 15


Characteristic Equation:  x^2 - 6x + 4 = 0


Roots:  

S = 3 + √5 ≈ 5.236067977

T = 3 - √5 ≈ 0.7639320225


Different Roots


1 = B + C

15 = (3 + √5) × B - (3 - √5) × C


B ≈ 3.183281573

C ≈ -2.183281573


Solution:


a_n ≈

3.183281573 × 5.236067977^n - 2.183281573 × 0.7639320225^n



Source


Levin, Oscar.  "2.4 Recurrence Relations"  Discrete Mathematics: An Open Introduction  openmathbooks.org  University of Northern Colorado.   Retrieved August 30, 2023.  https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Discrete_Mathematics_(Levin)/2%3A_Sequences/2.4%3A_Solving_Recurrence_Relations


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, September 9, 2023

Casio fx-9750GIII: Coin Flips and Probability of Winning

Casio fx-9750GIII:  Coin Flips and Probability of Winning




Introduction


The program COINPROB answers two questions in probability:



Take a game, with the chance of winning p.    There are only wins (success) and losses (failures).  Each game is independent and has the same chance of win.


1.  What is the chance of having an amount of wins in a set amount of games?   The binomial distribution is used to answer this question.


2. How many losses must be endured before a win occurs?  For this question, we turn to the geometric distribution.


Three results are listed are:


PDF:  the probability

MEAN:  expected value

VAR:  variance


The probability of success is also listed.



Casio fx-9750GIII Program:  COINPROB


Program Code:

(most spaces are added for readability)


"EWS 2023-06-25"

.5 → P

Locate 1, 4, "P(WIN) = P"

Locate 1, 5, "P(LOSS) = 1-P"

Locate 1, 6, "GAMES = TRIALS" ◢


Lbl 0

ClrText

Menu "PROB WIN VS LOSS", "SETTINGS", S, "# WINS IN GAMES", 1,

"# LOSS BEF. WIN", 2, "EXIT", E


Lbl S

Menu "SETTINGS", "P(WIN) = 0.5", F, "SET P(WIN)", U

Lbl F

.5 → F

"P(WIN) = .5"

"P(LOSS) = .5" ◢

Goto 0


Lbl U

"P(WIN)"? → P

"P(LOSS) = "

1 - P ◢

Goto 0


Lbl 1

"# GAMES"? → N

"# WINS"? → S

N nCr S × P^S × (1 - P)^(N - S) → D

N × P → M

M × (1 - P) → V

Goto R


Lbl 2

"# LOSSES"? → F

(1 - P)^(F - 1) × P → D

P⁻¹ → M

M × (1 - P) ÷ P → V

Goto R


Lbl R

ClrText

Locate 1, 3, "PDF= "

Locate 7, 3, D

Locate 1, 4, "MEAN="

Locate 7, 4, M

Locate 1, 5, "VAR="

Locate 7, 5, V

Locate 1, 6, "P(WIN)="

Locate 9, 6, P ◢

Goto 0


Lbl E

"THANK YOU"


Note:  The bold C is the combination function (nCr).  


A typo has been corrected (see line in red).  I thank Richard Antley for pointing out my error. - 9/27/2023 


Examples



1.  A fair coin is flipped.  You win if the coin flipped is heads.   What is the probability that you win 7 out of 10 times?


P = 0.5


# GAMES?  10

# WINS?  7


Problem Type:  # WINS IN GAMES


Results:

PDF=  0.1171875

MEAN= 5

VAR= 2.5

P(WIN)= 0.5


2.  What is the probability that you flip 7 tails before flipping a head?  Assume a fair coin.


P = 0.5


Problem Type:  # LOSS BEF. WIN


# LOSSES? 7


Results:


PDF=  0.0078125

MEAN= 2

VAR= 2

P(WIN)= 0.5


3.  On any given day in Luau Town, the chance of rain is 35% each day.   On a given week, how many days are expected to be sunny?


WIN:  sunny days (because that is what we want)

P(WIN) = 1 - 35% = 65% = 0.65

We want the mean (expected value).


Problem Type:  # WINS IN GAMES


Change P(WIN) to 0.65 in settings.  


# GAMES?  7  (7 days)

# WINS? (since we are interested in the mean, we can enter any number 0 to 7)


Results:


PDF= (N/A)

MEAN= 4.55

VAR= 1.5925

P(WIN)= 0.65


A week is expected to have about 4.55 sunny days.  Expected value does not have to be an integer.


4.  Assuming the chance of rain is 35% in Luau Town, what is the chance that there are 3 rainy days before a sunny day?


WIN:  sunny days, P(WIN) = 0.65


# LOSSES?  3


Results:  


PDF= 0.079625

MEAN= 1.538461538

VAR= 0.8284023669

P(WIN)= 0.65



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. 


Sunday, August 27, 2023

Casio fx-9750GIII and Swiss Micros DM42: Bijections

Casio fx-9750GIII and Swiss Micros DM42:  Bijections



Bijection - Three Problems 



I have a subscription to Brilliant (brillant.org, this is not a paid ad), where I am taking courses of probability, quantitative finance, and quantum mechanics.  


Today's blog is dealing with bijections, a technique to solve a class of counting problems.   A counting problem calculates the number of ways an event, such as distributing pieces of candy among a group of people, can happen.  


Here, I am working with three types of problems:


Problem 1:  How many positive integer solutions are possible of the equation:


A + B + C + ... = M


M = sum of the integers A, B, C, etc.

M > A, M > B, M > C.  


Problem 2:  How many positive integer solutions are possible of the equation:


A + B + C + ... ≤ M


M = sum of the integers A, B, C, etc.

M > A, M > B, M > C.  


Problem 3:  How many ways can distribute a set amount of balls into a set of urns?  Assume that the urns do not have to have an equal amount of balls.  


Similar problem would be:


How many ways to distribute candy among a population of people?  


How many ways to give a deck of cards from a deck to a table full of players, given that each card is randomly distributed?  Assume that the players do not have equal amount of cards.  



The programs on this blog uses two different approaches.   The program for the fx-9750GIII uses a prompt approach while the program for the DM42 uses a direct approach.  



Casio fx-9750GIII Program:  BIJECT


Code:

"EWS 2023-06-12"

0 → S

ClrText


Menu "BIJECTIONS","A+B+C+...=M",1,"A+B+C+...≤M",2,"BALLS IN URNS",3


Lbl 2

1 → S

Lbl 1

"# OF VARIABLES"? → B

V + S - 1 → V

"SUM (POS. INTEGER)"? → M

Goto 4


Lbl 3

"# OF URNS"? → V

V - 1 → V

"# OF BALLS"? → M

Lbl 4

(M + V) nCr V → S

"# SOLUTIONS = "

S


Notes:


The character # is from the character map (CHAR) of the default program menu in program editing mode (Mode B).


The combination function is displayed as a bold C.  I have nCr in the code listing for clarity.



Swiss Micros DM42 Program:  BIJECT


Code also works for HP 42S, Plus42, Free42


Code:

00  {14-Byte Prgm}

01  LBL "BIJECT"

02  +

03  LAST x

04  COMB

05  END


Stack Set Up:


Y:  number of dividers

X:  number of balls


Set up the stack for the three types of problems.


Problem 1:

Y:   number of variables - 1

X:  sum


Problem 2:

Y:  number of variables

X:  sum


Problem 3:

Y:  number of urns - 1

X: number of balls



Examples


Problem 1:  How many positive integer solutions are possible of the equation:


A + B + C = 20


Number of variables:  3

Sum:  20


Number of dividers:  2   (the number of plus signs in the equation)


Number of solutions:  nCr(20 + 2, 20) = 231



Problem 2:  How many positive integer solutions are possible of the equation:


A + B + C ≤ 20


Number of variables:  3

Sum:  20


Number of dividers:  3   


Since we are allowing for A, B, and C to add up to less than 20, add a slack variable, S, to "catch" any deficit to make the sum 20.   Therefore, we are really solving:


A + B + C + S = 20   


In listing the solutions, S is ignored. 


Number of solutions:  nCr(20 + 3, 20) = 1,771



Problem 3:  How many ways can distribute 40 balls in to 6 urns?


Set the urns in a line, and imagine a divider in between each urn.


urn 1 | urn 2 | urn 3 | urn 4 | urn 5 | urn 6


Number of dividers: 5

Number of balls:  40


Number of distributions:  nCr(40 + 5, 40) = 1,221,759


Try listing all possible distributions.




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 8, 2023

HP Prime and Python (Casio fx-9750GIII): Direct Sum and Tensor Product

HP Prime and Python (Casio fx-9750GIII):  Direct Sum and Tensor Product



This blog is about two operations in tensor algebra.  



DIRSUM:  Direct Sum


DIRSUM is the direct sum of two tensors, specifically column vectors.   The direct sum, symbolized by ⊕ (circle with a plus symbol in it), stacks column vectors on top of each other.  The order of the two vectors matters.


Example:


V1 = [ [ 2 ] [ 3 ] ]

V2 = [ [ 5 ] [ 6 ] [ 8 ] ]


V1 ⊕ V2 = [ [ 2 ] [ 3 ] [ 5 ] [ 6 ] [ 8 ] ]


V2 ⊕ V1 = [ [ 5 ] [ 6 ] [ 8 ] [ 2 ] [ 3 ] ]


The dimension of the direct sum is the sum of the dimensions of the vectors.



TENSOR:  Tensor Product 


The tensor product, also known as the outer product multiplies the numbers from V1 to each element V2 in order.   The tensor product can be represented in a column vector or a matrix.  





 If V1 and V2 are matrices, the outer product is calculated as:


V1 ⊗ V2 = V1 × V2ᵀ = V1 × transpose(V2)


Example:


V1 = [ [ 2 ] [ 3 ] ]

V2 = [ [ 5 ] [ 6 ] [ 8 ] ]


V1 ⊕ V2 = [ [ 10, 12, 16 ] [ 15, 18, 24 ] ]


V2 ⊕ V1 = [ [ 10, 15 ] [ 12, 18 ] [ 16, 24 ] ]


The tensor product is not commutative, order matters.  




HP Prime Programs:  DIRSUM and TENSOR


Note: DIRSUM and TENSOR accepts matrices or vectors in the form of column matrices.  


EXPORT DIRSUM(v,w)

BEGIN

// direct sum of 2 vectors

// 2023-04-30 EWS

LOCAL lv,lw,lc;

lv:=mat2list(v); 

lw:=mat2list(w);

lc:=CONCAT(lv,lw);

RETURN list2mat(lc,1);

END;


EXPORT TENSOR(v,w)

BEGIN

// tensor product of 2 vectors or matrices

// 2023-04-23 EWS

RETURN v*TRN(w);

END;





Python:  tensor.py


In this file, the tensor and dirsum functions are meant to be used with vectors only.  No outside libraries are required. 


# tensor file


# outer product

def tensor(x,y):

  t=[]

  lx=len(x)

  ly=len(y)

  for i in range(lx):

    c=[]

    for k in range(ly):

      w=x[i]*y[k]

      c.append(w)

    t.append(c)  

  return t


# direct sum

def dirsum(x,y):  

  return x+y


Source:


Bradley, Tai-Danae.  "The Tensor Products, Demystified"  math3ma.com   November 18, 2018.  https://www.math3ma.com/blog/the-tensor-product-demystified  Accessed April 24, 2023.




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. 


Sunday, April 30, 2023

TI-74 and Python (Casio fx-9750GIII): A Treasure of Programs

TI-74 and Python (Casio fx-9750GIII):  A Treasure of Programs



Today's programs illustrates an approach from both the BASIC and Python programming languages. 



Skiing Stars



This program paves a ski path by randomly plotting a path.  A slight difference:  the TI-74 version goes indefinitely until the user presses a key and uses one line.  The Python version goes 50 steps and the result must be scrolled up after the program completes.  I optimized the program to fit each screen.


TI-74 BASICALC Program


100 REM "Skiing Stars"

102 RANDOMIZE RND

104 P=14

106 PRINT "Press any key to stop.": PAUSE .25

120 CALL KEY(K,S)

122 IF S<>0 THEN 140

124 R=RND

126 IF R<.4 AND P>0 THEN LET P=P-1

128 IF R>.4 AND P<26 THEN LET P=P+1

130 DISPLAY ERASE ALL AT(P),"*****": PAUSE .15

132 GOTO 120

140 PRINT RPT$("*",31): PAUSE .25

142 STOP


Python (Casio fx-9750GIII):  skistar.py


# skiing stars

from random import *

rnd=int(10000000*random())

seed(rnd)

p=8

for i in range(50):

  r=random()

  if r<.4 and p>0:

    p-=1

  if r>.6 and p<14:

    p+=1

  print(" "*p+"*****")

print("scroll up")

    


Phase Speed of a Wave


Equation:

c = √( g  * λ / (2 * π) * tanh(2 * π * d / λ))


c = phase speed

λ = wavelength 

d = water depth

g = Earth's gravity = 9.80665 m/s = 32.174049 ft/s


Source:


"Wind Wave"  Wikipedia.  Last Edited January 28, 2023.  Accessed February 14, 2023.  https://en.wikipedia.org/wiki/Wind_wave#:~:text=In%20fluid%20dynamics%2C%20a%20wind,is%20known%20as%20the%20fetch.


TI-74 BASICALC Program:  


400 REM Phase Speed

402 PRINT "U.S. units used": PAUSE .25

404 T=2*PI: G=32.174049

406 INPUT "Depth (ft)? "; D

408 INPUT "Wavelength (ft)? "; L

410 C=SQR(G*L/T*TANH(T*D/L))

412 PRINT "Phase Speed (ft/s)=": PAUSE .25

414 PRINT C: PAUSE

416 STOP


Python (Casio fx-9750GIII): phspeed.py  


# phase speed

from math import *

print("U.S  units used")

t=2*pi

g=32.174049

print("Depth (ft)?")

d=float(input())

print("Wavelength?")

l=float(input())

c=sqrt(g*l/t*tanh(t*d/l))

print("Phase speed (ft/s)=")

print(c)


Example:


Depth:  2.26 ft

Wavelength:  18.9 ft


Result:  

Phase Speed:  7.845145563 ft/s



Horsepower Generated by a Wave


p = 0.0329 * h^2 * √( l * (1 - 4.935 * (h^2/l^2))


h = wave height (ft)

l = length of a wave (ft)

p = horsepower


Source: 


Rosenstein, Morton.  Strategies for Scientific Calculating Casio.  January 1982.


TI-74 BASICALC Program:


500 REM horsepower of a wave

502 PRINT "U.S. units used": PAUSE .25

504 INPUT "Wave height (ft)? ";H

506 INPUT "Length of wave (ft)? "; L

508 P=.0329*H^2*SQR(L*(1-4.935*(H^2/L^2)))

510 PRINT "Horsepower = ": PAUSE .25

512 PRINT P: PAUSE

514 STOP


Python (Casio fx-9750GIII):  hpwave.py


# horsepower of a wave

from math import *

print("U.S. units used")

print("Wave height (ft)?")

h=float(input())

print("Length of a wave?")

l=float(input())

p=.0329*h**2*sqrt(l*(1-4.935*(h**2/l**2)))

print("horsepower = ")

print(p)


Example:


Wave height:  1.26 ft

Length of wave: 24.04 ft


Result:

Horsepower = .2543549811 hp



Until next time,


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, April 29, 2023

TI-74 and Python (Casio fx-9750GIII): Probability

TI-74 and Python (Casio fx-9750GIII):  Probability



Today's program illustrates an approach from both the BASIC and Python programming languages. 


The program offers the user to calculate common probability problems:



1)  n!:  factorial of an integer

2)  nCr:  combinations without replacement

3)  nPr:  permutations

4)  nHr:  combinations with replacement

5) bday:  probability that n people do not share a category in common from a size of categories (i.e. birthday problem:  How many people in a room don't share a birthday?)



TI-74 BASICALC Program


200 REM probability

202 INPUT "1)n! 2)nCr 3)nPr 4)nHr 5)bday "; I

204 IF I<5 THEN INPUT "n? "; N

206 IF I>1 AND I<5 THEN INPUT "r? "; R

208 ON I GOTO 220,240,260,280,300


220 X=N

222 GOSUB 320

224 PRINT "n! = "; F: PAUSE

226 GOTO 340


240 Z=1: FOR K=(N-R+1) TO N: Z=Z*K: NEXT K

242 X=R: GOSUB 320

244 Z=Z/F

246 PRINT "nCr = "; Z: PAUSE

248 GOTO 340


260 Z=1: FOR K=(N-R+1) TO N: Z=Z*K: NEXT K

262 PRINT "nPr = "; Z: PAUSE

264 GOTO 340


280 Z=1: FOR K=N TO (N+R-1): Z=Z*K: NEXT K

282 X=R: GOSUB 320: Z=Z/F

284 PRINT "nHr = "; Z: PAUSE

286 GOTO 340


300 INPUT "# categories? "; R

302 INPUT "Sample size? "; N

304 Z=1: FOR K=1 TO (N-1): Z=Z*(1-K/R): NEXT K

306 PRINT "P(all unique) = "; Z: PAUSE

308 GOTO 340


320 F=1

322 FOR K=1 TO X: F=F*K: NEXT K

324 RETURN


340 INPUT "Again? 0:No, 1:Yes "; A

342 IF A=1 THEN 202

344 IF A=0 THEN STOP

346 GOTO 340



Python Program:  prob.py 


Program completed with the Casio fx-9750GIII.


# probability

from math import *


def fact(x):

  f=1

  for k in range(1,x+1):

    f*=k    

  return f



fc=1


while fc!=0:

  print("1)n! 2)nCr")

  print("3)nPr 4)nHr")

  print("5)bday")

  ch=int(input())

  if ch<5:

    n=int(input("n? "))

  if ch>1 and ch<5:

    r=int(input("r? "))

  if ch==1:

    f=fact(n)

    print("n! = "+str(f))

  if ch==2:

    f=fact(n)/(fact(n-r)*fact(r))

    print("nCr = "+str(f))

  if ch==3:

    f=fact(n)/fact(n-r)

    print("nPr = "+str(f))

  if ch==4:

    f=fact(n+r-1)/(fact(r)*fact(n-1))

    print("nHr = "+str(f))

  if ch==5:

    r=int(input("# categories? "))

    n=int(input("Sample size? "))

    f=1

    for k in range(n):

      f*=1-k/r

    print("p(all unique)=")

    print(str(f))

  print("Again? 0)no 1)yes")

  fc=int(input())    


Examples


1) n!  

13! = 6227020800


2) nCr

n = 13, r = 6; result:  1716


3) nPr

n = 13, r = 6; result:  1235520


4) nHr

n = 13, r = 6; result:  18564


5) bday

# categories: 13

Sample size: 6

Result:  P(all unique) = .2559703523



Wishing you an excellent day.   More BASIC and Python programming coming your way tomorrow!


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. 


HP 42S/DM42/Free 42: Two Functions Programmed Three Different Ways

HP 42S/DM42/Free 42: Two Functions Programmed Three Different Ways Introduction In 1979, Electro Scientific Industries (esi)...