Showing posts with label estimation. Show all posts
Showing posts with label estimation. Show all posts

Saturday, July 4, 2026

Swiss Micros DM32: Estimating Earth’s Acceleration at Latitude

Swiss Micros DM32: Estimating Earth’s Acceleration at Latitude



Introduction



Earth’s gravitational force is usually set a constant of 9.80665 m/s², usually shortened to 9.8 m/s² or 9.81 m/s² in publications such as physics text books. However, in reality gravity on Earth is not constant. There are many ways to calculate (estimate) the gravitational acceleration depending where you are on Earth. Gravity depends on many factors including latitude (degrees North or South) and the elevation. The blog focuses on the effect of latitude on Earth’s gravity.



The is part of the Acceleration Due to Gravity table from the Desk Ref book (see the Source section). The column for m/s² is added.



Degrees Latitude (North or South)

Gravity Acceleration (cm/s²)

Gravity Acceleration (m/s²)

0 (Equator)

978.0327

9.780327

15

978.3786

9.783786

30

979.3249

9.793249

45

980.6199

9.806199

60

981.9178

9.819178

75

982.8698

9.828698

90

983.2186

9.832186

[Glover, Young, pg. 587]



There are many ways to estimate the gravitational acceleration depending where you are on Earth. Gravity depends on many factors including latitude (degrees North or South) and the elevation.



Earth’s gravity tends to be at the strongest at the poles. However, gravity weakens at higher elevations, where we are further away from the center of the planet.





Gravity Estimate – (Univ. of Illinois)



The formula that is presented by The Grainger College of Engineering Physics Van [Univ. of Illinois] is a simple but pretty accurate estimation of gravity:



g = g_45 – 1 / 2 * (g_poles – g_equator) * cos(2 * latitude * π ÷ 180)

where:

g_poles = 9.832 m/s²

g_45 = 9.806 m/s²

g_equator = 9.78 m/s²

lat = latitude, north or south

2 * latitude is converted to radians. (as it is multiplied by π ÷ 180)



Simplifying the equation leads to:

1 / 2 * (g_poles – g_equator) = 1 / 2 * (9.832 – 9.78) = 0.026

2 * latitude * π ÷ 180 = latitude * π ÷ 90 (in radians)



Then:

g = 9.806 – 0.026 * cos(latitude * π ÷ 90)

(in radians)



DM32 Program: Gravity Estimate



Input L as D.MS (degrees/minutes/seconds) format.



E01 LBL E
E02 RAD
E03 INPUT L
E04 →HR
E05 90
E06 ÷
E07 π
E08 ×
E09 COS
E10 0.026
E11 ×
E12 +/-
E13 9.806
E14 +
E15 STO G
E16 RTN



World Geodetic System 84 Ellipsoidal Gravity Formula



The formula is presented by the World Geodetic System (WGS): [Wikipedia]



g = Ge * ((1 + k * sin² L) ÷ √(1 – e² * sin² L))

L: latitude in decimal degrees

with the constants:

Ge = 9.7803253359 m/s²

k = 0.001931852652

e² = 0.0066943799901



Input L as D.MS (degrees/minutes/seconds) format.



DM32: WEG ‘84



G01 LBL G
G02 DEG
G03 INPUT L
G04 →HR
G05 SIN
G06 x²
G07 STO T
G08 0.001931852652
G09 ×
G10 1
G11 +
G12 1
G13 RCL T
G14 0.0066943799901
G15 ×
G16 -
G17 SQRT
G18 ÷
G19 9.7803253359
G20 ×
G21 STO G
G22 RTN



Table of Values



Sources

“Gravity of Earth” Wikipedia. (2026, January 31).

https://en.wikipedia.org/wiki/Gravity_of_Earth Retrieved March 9, 2026.



Grainger Engineering Office of Marketing and Communications. (answer written by Rebecca H.) (2016, November 21). “How gravitational force varies at different locations on Earth.” Illinois. https://van.physics.illinois.edu/ask/listing/64061. Retrieved March 10, 2026.



Glover, Thomas J. and Richard A. Young. Desk Ref. Sequoia Publishing, Inc. Anchorage, AK 4th Edition. 2022 pg. 587


Eddie


All original content copyright, © 2011-2026. 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, October 18, 2025

Numworks: Estimating the Speed of Sound in Water

 Numworks: Estimating the Speed of Sound in Water


Generally, the speed of sound in water is faster than the speed of sound in air. Is there an easy way to estimate the speed of sound in water using curve fitting? I will use the Numworks’ Regression App. The online simulator of Numworks: https://www.numworks.com/simulator/ .


* For readability, I consolidated some screen shots.


The data, from The Engineering Toolbox (see Source below) is shown in below:


SI Units

Temperature in °C

Speed of sound in m/s (meters/second)

0

1403

5

1427

10

1447

20

1481

30

1507

40

1526

50

1541

60

1552

70

1555

80

1555

90

1550

100

1543


https://www.engineeringtoolbox.com/sound-speed-water-d_598.html





Of the curve fits available, I found the best fits were to be with polynomial regression.


Quadratic Regression: y = a2 * x^2 + a1 * x + a0



y = -0.02736013 * x^2 + 4.059449 * x + 1407.3398


where y is the speed of sound in water (m/s) and x is the temperature (°C). The r^2 parameter is 0.9980768, which is pretty good given the raw data is only rounded the nearest integer. The Numworks also offers residual plot, which is the difference between predicted and given data. From the residual plot, the biggest difference was at x = 0.


Quartic Regression: y = a4 * x^4 + a3 * x^3 + a2 * x^2 + a1 * x + a0



If we are looking for better accuracy, we could use the quartic regression, where r^2 parameter is 0.99981. From the residual plot, the biggest difference was located at x = 60.


y = -7.955665 * 10^-7 * x^4 + 2.523644 * 10^-4 * x^3 – 0.05122094 * x^2 + 4.782147 * x + 1403.692


For a quick calculation, the quadratic equation could be sufficient enough.



Source


The Engineering ToolBox (2004). Water - Speed of Sound vs. Temperature. [online] Available at: https://www.engineeringtoolbox.com/sound-speed-water-d_598.html. Accessed May, 2025.


Eddie

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


The author does not use AI engines and never will.


Sunday, February 18, 2024

Casio fx-CG 50 CORDIC Simulation: Approximating Sine and Cosine of Angles

Casio fx-CG 50 CORDIC Simulation:  Approximating Sine and Cosine of Angles




Introduction - How Computers Calculate Mathematical Functions



First developed by Jack E. Volder, the Coordinate Rotation Digital Computer, better known as CORDIC, is an algorithm used to calculate many mathematical functions, including trigonometric functions, logarithms, exponentials, and hyperbolic functions.     CORDIC is a fundamental algorithm, which variants of CORDIC are used in computers and calculators.


Today's focus will be a calculating sines and cosines of angles.  The steps will be detailed in the next sections.  



CORDIC1:  Series of Arctangents


Let Θ be the angle.   The first step is to build Θ as additions and subtractions of terms of arctan(1 ÷ (2^n)), starting at n = 0 and stopping at the required accuracy.  Let A be the approximation.  


Examples: 


Θ = 45° requires only one term:

A = 45° = arctan 1


Θ ≈ 71.56505118°

 A = 71.56505118° = arctan 1 + arctan 1/2


Θ ≈ 32.47119229°

A = 32.47119229° = arctan 1 - arctan 1/2 + arctan 1/4


A series to recreate Θ = 30° takes 32 terms of ±arctan (1 ÷ 2^n) from n = 0 to n = 31.   (8 decimal places)  This is known as coordinate rotation.


The program CORDIC1 determines the number of terms need to created to obtain Θ.   Accuracy in this code is set to 5 decimal places, but can be adjusted (see the line in blue).  


Casio fx-CG 50 Program Code:  CORDIC1


Deg

"ANGLE"?->Θ

0->I

0->A


Lbl 0

tan^-1 (2^(-I))->T

If A<Θ

Then 

A+T->A

Else 

A-T->A

IfEnd


I+1->I


Abs (A-Θ)>1×10^-5=>Goto 0


ClrText

Blue Locate 1,3,"Θ: "

Blue Locate 5,3,Θ


Red Locate 1,4,"A: "

Red Locate 5,4,A


Green Locate 1,7,I


Notes:

-> is the store arrow →

=> is the jump command ⇒


For reference:


arctan 1 = 45°

arctan 1/2 ≈ 26.56505118°

arctan 1/4 ≈ 14.03624347°

arctan 1/8 ≈ 7.125016349°

arctan 1/16 ≈ 3.576334375°

arctan 1/32 ≈ 1.789910608°


CORDIC2:  Calculating Sine and Cosine


Imagine the coordinate (cos Θ, sin Θ) on a unit circle.  A unit circle is a circle with radius of length 1 and center located at the origin (0,0).   


Set the initial angle at 0°.  Then x = cos 0° = 1 and y = sin 0° = 0.   The initial vector is set to be [ [ cos 0° ] [ sin 0° ] ] = [ [ 1 ] [ 0 ] ].


Approximate Θ in terms of arctangent (1 ÷ (2^n)), starting at n = 0.  



Direction of Rotation


Set σ as the direction of rotation.    


A rotation is positive if we add arctan(1 ÷ (2^n)) to A.    For a positive rotation, set σ_i = +1.   


A rotation is negative if we subtract arctan(1 ÷ (2^n)) to A.    For a negative rotation, set σ_i = -1.   


For example:


 Θ ≈ 32.47119229°

A = 32.47119229° = arctan 1 - arctan 1/2 + arctan 1/4


Then:  σ_0 = 1 (positive rotation), σ_1 = -1 (negative rotation), σ_2 =  1 (positive rotation).  



Multiplying Factor


The number of iterations is also used to determine the required multiplication factor:


n = Π( 1 ÷ √(1 + 2^(-2 × I)), I = 0 to I = terms needed - 1)


For the example:


 Θ ≈ 32.47119229°

Needed 3 iterations, i_0 to i_2.  


Then:

n = 1 ÷ √(1 + 2^(-2 × 0)) × 1 ÷ √(1 + 2^(-2 × 1)) × 1 ÷ √(1 + 2^(-2 × 2))

= 4 × √170 ÷ 85

≈ 0.6135719911



Calculating the Next Iteration


The next iteration for x and y are:


x_i+1 = x_i - 2^(-i) × σ_i × y_i


y_i+1 = 2^(-i) × σ_i × x_i + y_i


When A is sufficiently near or equal to Θ, the cosine and sine are approximated as:


cos(A) ≈ n × x_final


sin(A) ≈ n × y_final


For the example:

 Θ ≈ 32.47119229°


The approximated angle, in this case, happens to be exact angle, A = Θ.  And,

cos(A) ≈ 0.8436614877 

sin(A) ≈ 0.5368754922


For more details, please check out resources in the Sources section.   I particularly like Oxford's A Very Short Introduction series.  



Casio fx-CG 50 Program Code:  CORDIC2


This algorithm is adopted from the Python example (see Wikipedia article).  


Deg

"ANGLE"?->Θ


0->I

0->A

1->X

0->Y

1->N


Lbl 0

tan^-1 (2^(-I))->T

If A<Θ

Then 

A+T->A

1->S

Else 

A-T->A

-1->S

IfEnd

N÷√(1+2^(-2*I))->N

X-S*2^(-I)*Y->P

Y+X*S*2^(-I)->Q

P->X

Q->Y


I+1->I


Abs (A-Θ)>1×10^-5=>Goto 0

X*N->X

Y*N->Y


ClrText

Blue Locate 1,3,"Θ: "

Blue Locate 5,3,Θ


Red Locate 1,4,"A: "

Red Locate 5,4,A


Black Locate 1,5,"cos :"

Black Locate 7,5,X

Black Locate 1,6,"sin :"

Black Locate 7,6,Y


Green Locate 1,7,I



CORDIC3:  Doing it without a Arctangent function


In reality, most of the time the arctangent function also has to be approximated.   There are many ways to approximate, which varying accuracy.   I used the fx-CG50's statistics mode to come up with a regression equation with the following lists:


x_list = sequence of 1÷(2^i) from i = 0 to i = 39 


y_list = sequence of arctan(1÷(2^i)) from i = 0 to i = 39


Of the regression models the fx-CG50 offers, the best regression model is quartic regression (4th-order polynomial):


y ≈ 9.43597784 × x^4 - 22.116232 × x^3 + 0.40116676 × x^2 + 57.2790727 × x + 1.4558 × 10^-5


Remember that I am working with degree angle measurement.  





Casio fx-CG 50 Program Code:  CORDIC3


Deg

"ANGLE"?->Θ


0->I

0->A

1->X

0->Y

1->N


Lbl 0

2^(-I)->K

9.43597784917955K^(4)-22.1162323028173K^(3)+

0.401166766193516K^2+57.2790727477367K+

1.45584715586876×10^-5->T


If A<Θ

Then 

A+T->A

1->S

Else 

A-T->A

-1->S

IfEnd

N÷√(1+2^(-2*I))->N

X-S*2^(-I)*Y->P

Y+X*S*2^(-I)->Q

P->X

Q->Y


I+1->I


Abs (A-Θ)>1×10^-5=>Goto 0

X*N->X

Y*N->Y


ClrText

Blue Locate 1,3,"Θ: "

Blue Locate 5,3,Θ


Red Locate 1,4,"A: "

Red Locate 5,4,A


Black Locate 1,5,"cos :"

Black Locate 7,5,X

Black Locate 1,6,"sin :"

Black Locate 7,6,Y


Green Locate 1,7,I


For the example:

 Θ ≈ 32.47119229°


Approximating Θ within 8 decimal places (10^-5) yields these results:

A:  32.47118598

cos A:  0.8436683456

sin A:  0.5368647154

24 terms used



Sources


"CORDIC"  Wikipedia.   Last Edited January 11, 2024.   Accessed January 12, 2024.  https://en.wikipedia.org/wiki/CORDIC


Brummelen, Glen Van.  Trigonometry:  A Very Short Introduction  Oxford University Press: Oxford, United Kingdom.  2020.  ISBN 978-0-19-881431-3



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, August 12, 2023

HP Prime: Monte Carlo Integration

HP Prime:  Monte Carlo Integration



Integration by Random Points



There are many ways to calculate the area under the curve f(x) in the interval [a, b].     Two of the common are calculating the antiderivative and using the Simpson's Rule.  Another method is the Monte Carlo method.   Unlike Simpson's Rule, where the intervals are fixed, the Monte Carlo method picks random points in interval.  The integral is calculated using the simple formula:


∫ f(x) dx ≈ (b - a) ÷ (n - 1) * Σ( f(x_i), from i = 0 to n)


x_i is a random point in the interval [a, b]

n = number of points, n > 2


How are the random numbers are picked are determined by various methods: pseudo-random generators, using a uniform distribution, using a normal distribution, etc.  


The program MONTE for the HP Prime uses the RANDOM function.  The HP Prime allows to pick a random (real) number in the interval [a , b] by the syntax RANDOM(a,b). 


Note:  I use the Function app function AREA to calculate the integral using any of the system variables F0 through F9.  In trade, the program uses global variables instead of local variables.  



HP Prime Program:  MONTE


Code: 


EXPORT MONTE()

BEGIN

// 2023-06-03 EWS

STARTAPP("Function");

// use input box for global variables

INPUT({A,B,E},"Monte Carlo of F1(X)",

{"lower:","upper:","# places:"}); 

// set radians

HAngle:=0;

// input check

IF A≥B THEN

MSGBOX("Lower limit must be

less than upper limit.");

KILL;

END;

// AREA must be stored to a global var.

I:=Function.AREA(F1(X),A,B);

// initialize variables

N:=1;

S:=0;

// repeat loop

REPEAT 

X:=RANDOM(A,B);

S:=S+F1(X);

N:=N+1;

IF N>1 THEN

J:=(B-A)*S/(N-1);

END;

UNTIL (N>1) AND (ABS(I-J)<ALOG(−E));

// results

PRINT();

PRINT("Results");

PRINT("Actual: "+STRING(I));

PRINT("Approx: "+STRING(J));

PRINT("# terms used: "+STRING(N)); 

// return home

STARTVIEW(−1);  

END;



Examples


Estimate the integral of F1(X) to three decimal places.  Your mileage may vary. 


∫ F1(X) dX from X = A to X = B


Example 1:

F1(X) = X^2 + 1

A = 0,  B  = 3


Exact Integral = 12


Trial 1:  12.0004750664,  64 points

Trial 2:  11.9996447323,  272 points

Trial 3:  12.0001030297,  9 points


Example 2: 

F1(X) = COS(X - 1) 

A = 1,  B = π/2 + 1


Exact Integral = 1


Trial 1:  1.00079319497, 411 points

Trial 2:  .999705900656, 187 points

Trial 3:  1.00071067474, 737 points



Example 3:

F1(X) = SIN X/X

A = 0, B = 4


Exact Integral = Si(4) ≈ 1.75820313895


Trial 1:  1.75789723089, 205 points

Trial 2:  1.75729880586, 133 points

Trial 3:  1.75832553216, 223 points



Notes


While the Monte Carlo method is easy to calculate, it is difficult to get an accurate answer.  The method requires a lot of calculation points, and how many really depends on what random numbers are picked.  It's really the luck of a draw.   This is good for a short approximation but I recommend the Simpson's Rule, Trapezoid Rule, or when possible and feasible, finding the antiderivative instead.  



Source


Cumer, Victor.  "The basics of Monte Carlo integration"  Towards Data Science.   Medium.  October 26, 2020.  Last Retrieved June 4, 2023.  https://towardsdatascience.com/the-basics-of-monte-carlo-integration-5fe16b40482d


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. 


Python – Earth’s Radius and Gravity in US Units

Python – Earth’s Radius and Gravity in US Units Introduction The following script, gravus2.py, estimates the Earth’s gravity i...