Showing posts with label arctangent. Show all posts
Showing posts with label arctangent. Show all posts

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

Arcsine and Arccosine in terms of Arctangent

Arcsine and Arccosine in terms of Arctangent


Some Motivation


I recall reading about the Sinclair Scientific Programmable, a vintage scientific calculator that was introduced in 1975.  This calculator just had three trigonometric functions:  sine, cosine, and arctangent.  It is the aim of this blog to fill in the blanks.  


You can find more information here:  

http://www.vintagecalculators.com/html/scientific_prog_.html

In this blog entry, angles will have radian measure.  


For tangent, it's pretty easy use of the trig identity:


tan x = sin x ÷ cos x


Determining Arcsine 


Imagine the right triangle shown below:




Then:


sin Θ = x

Θ = arcsin x


By the Pythagorean Theorem:


t^2 + x^2 = 1

t^2 = 1 - x^2

t = √(1 - x^2)


tan Θ = x ÷ t

tan Θ = x ÷ √(1 - x^2)

Θ = arctan(x ÷ √(1 - x^2))


Then:


arcsin x = arctan(x ÷ √(1 - x^2))


Determining Arccosine


Most calculators for the arccos function have the range [0, π].  To accomplish this, we are going to use the identity


cos Θ = sin(π÷2 - Θ).


Let w = cos Θ


Then:  


w = cos Θ

w = sin(π÷2 - Θ)

arcsin w = π÷2 - Θ

Θ = π÷2 - arcsin w

Θ = π÷2 - arctan(w ÷ √(1 - w^2))


For any angle w:


arccos w = π÷2 - arctan(w ÷ √(1 - w^2))


Eddie 


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


Monday, February 8, 2021

An Alternative Way of Finding the Angle in a Rectangular to Polar Conversion

An Alternative Way of Finding the Angle in a Rectangular to Polar Conversion


Welcome to a special Monday edition of Eddie’s Math and Calculator Blog. 


The Traditional Method


Often we are required to find polar coordinates of a given point (x,y).   Finding the radius, r, is fairly simple:


r = √(x^2 + y^2) 


When talking about complex numbers, r represents the absolute value of x + yi where i = √-1.


Finding the angle, θ, often uses the formula:


θ = atan(y/x)


In complex numbers, θ represents the argument (arg) function.


On a scientific calculator the range of the arctangent function is ( -90°, 90° ).  (open interval).   In finding the true angle, adjustments will be required:





Let a = atan(y/x). Then: 


Quadrant I (x and y are both positive):  θ = a

Quadrant II (x is negative, y is positive): θ = a + 180°

Quadrant III (x and y are both negative):  θ = a - 180°

Quadrant IV (x is positive, y is negative):  θ = a 


If you are working with radian angle measures, know that 90° = π/2, and 180° = π.


This does not take into consideration situations where either x or y is 0:


If x > 0 and y = 0:  θ = 0°

If x = 0 and y > 0:  θ = 90°

If x < 0 and y = 0:  θ = 180°

If x = 0 and y < 0:  θ = -90°


Is there a shorter way to calculate θ?  


The Vector Method


Consider the point (x, y) as a vector [x, y].   Now draw another vector [x, 0].  In a regular Cartesian coordinate system, angles are measured from the x-axis counter clockwise.  





Let a and b represent two vectors.  Then the angle between two vectors are:


cos θ = (a ● b) / ( ||a|| ||b|| ) = dot(a,b) / ( norm(a) * norm(b) )


with:

dot(a,b) = a1 * b1 + a2 * b2

norm(a) = √(a1^2 + a2^2)

norm(b) = √(b1^2 + b2^2)


Let a = [x, y] and b = [x, 0].  Then:


cos θ = (x^2) / (√(x^2 + y^2) * √(x^2))

cos θ = (x^2) / (√(x^2 + y^2) * x)

cos θ = x / √(x^2 + y^2)

θ = acos( x / √(x^2 + y^2) )


The range of the arccosine function of a calculator is [ 0°, 180° ].  


If y < 0, the angle would be measured clockwise, and therefore I would make the adjustment:

θ = -acos( x / √(x^2 + y^2) )


In summary:

If y ≥ 0, θ = acos( x / √(x^2 + y^2) )

If y < 0, then θ = -acos( x / √(x^2 + y^2) )


Examples:


Find the angle, in degrees, in a rectangular to polar conversions:


Quadrant I  (2, 4):  y ≥ 0:   θ = acos( 2 / √(2^2 + 4^2) ) ≈ 63.43494882°


Quadrant II (-2, 4):  y ≥ 0:    θ = acos( -2 / √((-2)^2 + 4^2) ) ≈ 116.5650512°


Quadrant II (-2, -4):  y < 0:  θ = -acos( (-2) / √((-2)^2 + (-4)^2) ) ≈ -116.5650512°


Quadrant IV (2, -4):  y < 0:  θ = -acos( 2) / √(2^2 + (-4)^2) ) ≈ -63.43494882°


Vector Method for Navigation


In navigation, angles start from true North (up) and rotate clockwise towards East (right).  Angles are measured from 0° to 360°.


Use the vectors [0, N] and [E, N], then the angle between these vectors are:

If E ≥ 0,  θ = acos( N / √(E^2 + N^2))

If E < 0, θ = 360° - acos( N / √(E^2 + N^2))


Note:  This is the first blog entry that I have typed on Google Docs.  I have been using Windows app Wordpad for the last three years.   I am testing Google apps as I am considering buying a Chromebook.


Eddie


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

HP Prime: Psuedo Ordering of Complex Numbers

HP Prime: Psuedo Ordering of Complex Numbers 


Can Complex Numbers be Ordered?

Not really, not if you consider what is required to be an ordered field.  The complex numbers can not be an ordered field, one counter example is illustrating the nonzero complex numbers i and 1, when squared and added together, you get zero.

i^2 + 1^2 = -1 + 1 = 0

If you order complex numbers by their modulus (absolute values) only, you run into the problem of having many complex numbers becoming equivalent.  Example:

2 + 3i  and 3 + 2i,  each with radius √13

One way to "psuedo" order complex numbers is to use the numbers looking at both their modulus and angle.   

z1  "<"  z2 if and only if one of the two conditions are met:

| z1 | < | z2 | or

| z1 | = | z2 | and atan(imag(z1)/real(z1)) < atan(imag(z2)/real(z2))

I believe we do not have about the unit of angle  (degrees vs. radians vs. grads) for this psuedo-ordering.

Please see the sources listed at the end of this article for more details. 


HP Prime Program:  CLESS

EXPORT CLESS(z1,z2)

BEGIN

// 2020-08-06 EWS

// psuedo ordering of 

// complex numbers

LOCAL a1,a2,t1,t2;

a1:=ABS(z1);

a2:=ABS(z2);

t1:=ATAN(IM(z1)/RE(z1));

t2:=ATAN(IM(z2)/RE(z2));

IF (a1<a2) OR ((a1==a2) AND (t1<t2)) THEN

RETURN "TRUE";

ELSE 

RETURN "FALSE";

END;

END;


Example (angle shown in radians, rounded to five decimal places):


These complex numbers are listed in order:

1 + i    (1.41421 ∠ 0.78540)

2 + i    (2.23067 ∠ 0.46365)

1+ 2i  (2.23607 ∠ 1.10715)

2 + 2i  (2.82843 ∠ 0.78540)

3 + i  (3.16228 ∠ 0.32175)

1 + 3i (3.16228 ∠ 1.24905)

3 + 2i (3.60555 ∠ 0.58800)

2 + 3i (3.60555 ∠ 0.98279)

4 + i (4.12311 ∠ 0.24498)


Sources:

Weimer, Richard C.   "Can the Complex Numbers Be Ordered?"  The Two-Year College Mathematics Journal, Dec. 1976, Vol. 7, No. 4.   Taylor & Francis, Ltd on behalf of the Mathematical Association of America.  https://www.jstor.org/stable/3027050?seq=1


Yada, Dharmendra Kumar.  "A New Approach to Ordering Complex Numbers"  International Journal of Mathematical Sciences and Engineering Applications (IJMSEA), Vol 2.  No. III (2008), pp. 221-223.   Article downloaded from ResearchGate:  https://www.researchgate.net/publication/267465398_A_new_approach_to_ordering_complex_numbers

Eddie

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