Showing posts with label weather. Show all posts
Showing posts with label weather. Show all posts

Saturday, May 18, 2024

HP 15C and Python: Calculating The Speed of Sound in Air

 HP 15C and Python: Calculating The Speed of Sound in Air



How fast does Sound Travel?


We can calculate the speed of sound in air which depends on temperature.


The speed of sound in air can be approximated by:

c ≈ √( γ * R * T / M)


where:


R = molar gas constant = 8.314 4625 618 153 J/(K * mol)

R = 8.314 4625 618 153 (kg * m^2)/(s^2 * K * mol)


M = molar mass of air = 0.02897 kg/mol


γ = adiabatic index of air

The index of air varies between 1.3991 and 1.403. For calculations, Wikipedia uses an average value of γ = 1.4. (see Source). For reference, an adiabatic process is a thermodynamic process that does not use heat.


A joule is a composite unit, 1 J = 1 (kg * m^2)/s^2


T = temperature in Kelvin.


Convert temperatures to Kelvin:


Fahrenheit to Kelvin:

T = 5/9 * (°F – 32) + 273.15


Celsius to Kelvin:

T = °C + 273.15


A formula to calculate the speed of sound in air is:


C = √( γ * R * T / M)


Entering the three numerical constants for γ, R, M:


γ * R / M ≈ 401.8035093 m^2/(s^2 * K)


Then:


C ≈ √(401.8035093 * T)

≈ 20.04503702 * √T


A popular version of the formula can be achieved by multiplying by 273.15/273.15 (essentially multiplying by 1):


C ≈ √(401.8035093 * 273.15 * T / 273.15)

≈ √(401.8035093 * 273.15) * √(T / 273.15)

≈ 331.2893427 * √(T / 273.15)


Let °C be the temperature in degrees Celsius. Then:

≈ 331.2893427 * √((°C + 273.15) / 273.15)

≈ 331.2893427 * √(°C / 273.15 + 1)


The Wikipedia formula rounds the constant to 331.3. (see Source) Other formulas use 331.


However, we can use the formula


C ≈ 20.04503702 * √T


and be in the ballpark. Note we’ll have to convert the temperature to Kelvin before using this formula.




HP 15C (Collector’s Edition) Program: Speed of Sound


Instructions:


To calculate the speed of sound when the temperature is in Fahrenheit (°F), press [ f ] { A } or [ GSB ] { A } [ R/S ].


To calculate the speed of sound when the temperature is in Celsius (°C), press [ f ] { B } or [ GSB ] { B } [ R/S ].


To calculate the speed of sound when the temperature is in Kevin (K), press [ f ] { C } or [ GSB ] { C } [ R/S ].


Code


Step

Key Code

Key

Notes

001

42, 21, 11

LBL A

Enter °F, convert to °C

002

3

3


003

2

2


004

30

-


005

5

5


006

20

×


007

9

9


008

10

÷


009

42, 22, 12

LBL B

Enter °C, convert to K

010

2

2


011

7

7


012

3

3


013

48

.


014

1

1


015

5

5


016

40

+


017

42, 22, 13

LBL C

Enter K

018

11

Calculate Speed of Sound

019

2

2


020

0

0


021

48

.


022

0

0


023

4

4


024

5

5


025

0

0


026

3

3


027

7

7


028

0

0


029

2

2


030

20

×


031

36

ENTER


032

36

ENTER


033

2

2


034

48

.


035

2

2


036

3

3


037

6

6


038

9

9


039

3

3


040

6

6


041

20

×


042

34

X<>Y


043

43, 32

RTN

End of the program


Python Code: spsound.py


Programmed on a TI-84 Plus CE Python.


print("Speed Of Sound\n")

print("1. Fahrenheit")

print("2. Celsius")

print("3. Kelvin")


# type of temperature

# choice var must be an integer

# ch is used in an element call

ch=int(input("? "))

if ch==1:

t0=eval(input("deg F? "))

t=5/9*(t0-32)+273.15

elif ch==2:

t0=eval(input("deg C? "))

t=t0+273.15

elif ch==3:

t0=eval(input("K? "))

t=t0

else:

print("Not a valid choice")

# force an error to stop the script

1/0


# calculation

s1=20.04503702*t**0.5

s2=s1*2.236936

print(str(s1)+" m/s")

print(str(s2)+" mi/hr")




Example Calculations (rounded to 5 decimal places)


Temperature

Speed of Sound (m/s)

Speed of Sound (mi/hr)

68 °F

343.20358

767.72445

35 °C

351.87462

787.83024

280 K

335.41762

750.30776

0 °C

331.28934

741.07306

96 °F

352.19167

787.83024



Source


“Speed of Sound” Wikipedia. Last Edited March 27, 2024. https://en.wikipedia.org/wiki/Speed_of_sound Retrieved April 7, 2024



Until next time,


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.

Sunday, October 8, 2023

TI-84 Plus CE: Using Matrices for "Tax Table" Calculations

TI-84 Plus CE:   Using Matrices for "Tax Table" Calculations


How The Matrix is Set Up


Each row represents a tier or a bracket.  


First column:  Lower Limit.  This is the test variable.  The first tier often will have the lower tier of 0.


Second and subsequent columns:    Variables that are associated with that tier.


Let's demonstrate this with an example.




Example Tax Bracket






This matrix has four brackets (four rows).  The algorithm starts with the last (bottom) row and tests whether the input is greater or equal to the test variable.  


According to the table, the tax rate changes at income level at $30,000, $90,000, and $270,000, with the top tax rate of 9% effective for all income excess of $270,000.


If the income is $29,999.99, the first tier of 3% is used.  If the income goes to $30,000.00, the next tier is activated.


The program ITAX, which uses this type of setup, goes "backwards".  It tests from the highest tier down.  


For example:  Income = $50,000


Start at tier 4 (bottom row). 

Is 50,000 > 270,000?  No, go to tier 3. (move one row up)

Is 50,000 > 90,000?  No, go to tier 2.  (move one row up) 

Is 50,000 > 30,000?  Yes, use the variables from tier 2.


Tax:  (50,000 - 30,000) × 5% + 900 = 20,000 × 5% + 900 = 1,900


Matrix wise:   (income - M[2,1]) * M[2,2] ÷ 100 + M[2,3]



Programs



There are two programs that illustrate this method:  


ITAX:  Income Tax Bracket.  Bracket is stored in Matrix [ A ] and has three columns:


Column 1:  lower limit of each bracket

Column 2:  tax rate for that bracket

Column 3:  additional "minimum" tax.   



BAROMET:  Calculates the air pressure and density based on height.  Scientific information is stored in Matrix [ J ].  


Column 1:  height in meters

Column 2:  mass density in kg/m^3

Column 3:  standard temperature in K

Column 4:  static pressure in Pa


If you are working in US Units, the height is converted to meters first.  After the calculation, the results are converted back into US units. 



You can download both programs here (zip file):  

https://drive.google.com/file/d/1ZCtMcJgZDLUGnrAHE6cPAkWBQQj1aHKc/view?usp=sharing



Source


"Barometric formula."  Wikipedia.  Last edited July 13, 2023.  Retrieved July 16, 2023.   https://en.wikipedia.org/wiki/Barometric_formula



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, November 20, 2022

HP 32SII: Some Algorithms For RPN Calculators

HP 32SII:  Some Algorithms For RPN Calculators



Four programs ported to the HP 32SII calculator from algorithms designated for the 1973 HP 45 calculator.  



HP 32SII: Euclid Algorithm - Greatest Common Divisor (GCD)


The HP 45 algorithm is found on page 228 in the Algorithms For RPN Calculators book. (see source below)


This algorithm takes up three labels.


E01 LBL E

E02 INPUT M

E03 ENTER

E04 ENTER

E05 ENTER

E06 INPUT N

E07 x<>y


K01 LBL 1

K02 ÷

K03 FP

K04 ×

K05 1

K06 x>y?

K07 GTO L

K08 R↓

K09 ENTER

K10 ENTER

K11 R↓

K12 R↓

K13 GTO K


L01 LBL L

L02 R↓

L03 R↓

L04 RTN


Sizes and Checksums:

E:  10.5 bytes, 9D4D

K:  19.5 bytes, F8AD

L:  6.0 bytes, C304

Total:  36.0 bytes


Instructions:

Press [ XEQ ] E, enter M and N.   


Examples:


Input:  M = 36, N = 28.  Result:  4

Input:  M = 48, N = 126. Result: 6

Input:  M = 115, N = 300.  Result: 5


HP 32SII:  GCD Using One Label - John Kenney 


The program was provided by Ross Barnes, and the algorithm is from the book ENTER by J. Daniel Dodlin and Keith Jarrett (ISBN 0-9615174-2-1, pg. 84).  This is smart, one label program.


Enter both numbers in the stack before running the program.


G01 LBL G

G02 ENTER

G03 ENTER

G04 -

G05 R↓

G06 x<>y

G07 LASTx

G08 /

G09 LASTx

G10 RDN

G11 IP

G12 x

G13 -

G14 x≠0?

G15 GTO G

G16 +

G17 RTN


Size and Checksum:  25.5 bytes, 4E39


Posted with permission.  


HP 32SII:  Tetens Equation


The HP 45 algorithm is found on page 290 in the Algorithms For RPN Calculators book.    The original algorithm took the temperature in Celsius. 


Find the saturation of water vapor (e_m) in mmHg (millimeters of Mercury) given the temperature in °F.


Determined Formulas:

T (in °C) = (T°F - 32) * 5/9

α = T/(236.87 + T)

e_m = 4.579 * 10^(7.49 * α)


T01 LBL T

T02 INPUT T

T03 →°C

T04 ENTER

T05 ENTER

T06 236.87

T07 +

T08 ÷

T09 7.49

T10 ×

T11 10^x

T12 4.579

T13 ×

T14 RTN


Size and Checksum:

45.0 bytes, 404A


Examples:

T = 68 °F, Result: 17.53658 mmHg

T = 99 °F, Result:  47.63501 mmHg




HP 32SII:  Dew Point Given Relative Humidity and Air Temperature


The HP 45 algorithm is found on page 290 in the Algorithms For RPN Calculators book.    The original algorithm took the temperature in Celsius. 


Relativity humidity (F) is to be entered as a decimal.  For instance, instead of 20%, enter 0.20.


Determined Formulas:

T (in °C) = (T°F - 32) * 5/9

A = T/(T + 236.87)

B = 1/(log F/7.49 + A)

TD = 236.87/(B - 1)

TD = TD * 9/5 + 32


D01 LBL D

D02 INPUT T

D03 →°C

D04 ENTER

D05 ENTER

D06 236.87

D07 STO A

D08 +

D09 ÷

D10 INPUT F

D11 LOG

D12 7.49

D13 ÷

D14 +

D15 1/x

D16 1

D17 -

D18 RCL÷ A

D19 1/x

D20 →°F

D21 RTN


Size and Checksum:

47.5 bytes, 8677


Examples:

T = 80, F = 0.64, Result:  66.725

T = 95, F = 0.32, Result:  60.50684



HP 32SII:  Effective Temperature Due to Wind Velocity



The HP 45 algorithm is found on page 291 in the Algorithms For RPN Calculators book.    The original algorithm took the temperature in Fahrenheit. 


Wind velocity is in miles per hour (mph).  


Determined Formulas:

A = 0.634*(0.634 - log V)

ΔT = A*(T - 90)

Effective T = T - ΔT


E01 LBL E

E02 0.634

E03 ENTER

E04 ENTER

E05 INPUT V

E06 LOG

E07 -

E08 ×

E09 INPUT T

E10 ENTER

E11 90

E12 -

E13 ×

E14 RCL T

E15 x<>y

E16 -

E17 RTN


Size and Checksum:

33.5 bytes, 54F7


Examples:

V = 20 mph, T = 15 °F,  Result: -16.71728

V = 15 mph, T = 86 °F,  Result: 84.62526


Source:


Ball, John A.  Algorithms For RPN Calculators  John Wiley & Sons:  New York, NY.  1978. ISBN 0-471-03070-8

For the second GCD program:

Dodin, J. Daniel and Keith Jarrett. ENTER: Reverse Polish Notation Made Easy   Synthetix:  Berkeley, CA   ISBN 0-9612174-2-1  1984.


Special thanks and gratitude to Ross Barnes.  



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


Trigonometry Reduction Formula and Solving Simple Arcsine and Arccosine Equations

Trigonometry Reduction Formula and Solving Simple Arcsine and Arccosine Equations Some Background and Periodic Reduction Formulas ...