Post Reply 
(12C) SIN COS TAN
12-23-2023, 05:24 AM (This post was last modified: 05-31-2024 12:51 AM by Gamo.)
Post: #1
(12C) SIN COS TAN
When you need to use the SIN COS TAN with your HP-12C

This program is not meant to be super accurate it is good enough

at 4 decimal digits with the recommended angle between 0º - 45º

Usage:

Input angle [R/S] display answer for TAN
For SIN [RCL] 1
For COS [RCL] 2

Program: RPN
Quote:
355 ENT↑ 113 ÷ x 180 ÷ STO 0

ENT↑ x 20 ÷ 1 + 1/x 10 x 7 - RCL 0 3 ÷ x STO 1

RCL 0 ENT↑ x 30 ÷ 1 + 1/x 5 x 3 - RCL 0 RCL 0 x 4 CHS ÷ x 1 + STO 2

RCL 1 X<>Y ÷

Gamo 12/23/2023
Find all posts by this user
Quote this message in a reply
12-23-2023, 10:01 AM (This post was last modified: 12-23-2023 10:03 AM by Gamo.)
Post: #2
RE: (12C) SIN COS TAN
Example Problem.

John wants to measure the height of a tree.
He walk exactly 100 feet from the base of the tree and
look up. The angle from the ground to the top of the tree is 33º
To the nearest foot, how tall is the tree?

Formular for this problem is TAN(33º) = X / 100

100[ TAN(33º) ] = X

Using this program:

33 [R/S] display 0.6494
100 [x] display answer 64.9402 ≈ 65 feet

The tree is 65 feet tall.

Gamo.
Find all posts by this user
Quote this message in a reply
12-23-2023, 12:00 PM
Post: #3
RE: (12C) SIN COS TAN
I've used the Code Analyzer for HP Calculators with the following Python program:
Code:
def sin_cos_tan(x):
    global X, Y, Z, T, L
    X = x
    # sin
    STO(0)
    DUP()
    MUL()
    number(20)
    DIV()
    number(1)
    ADD()
    RECIPROCAL()
    number(10)
    MUL()
    number(7)
    SUB()
    RCL(0)
    number(3)
    DIV()
    MUL()
    STO(1)
    # cos
    RCL(0)
    DUP()
    MUL()
    number(30)
    DIV()
    number(1)
    ADD()
    RECIPROCAL()
    number(5)
    MUL()
    number(3)
    SUB()
    RCL(0)
    DUP()
    MUL()
    number(4)
    CHS()
    DIV()
    MUL()
    number(1)
    ADD()
    STO(2)
    # tan
    RCL(1)
    SWAP()
    DIV()

x = symbols("x")
sin_cos_tan(x)

This is mostly a verbatim translation of your program.
Make sure to import the sympy library block before running it.

We can then use the series function to compare the trigonometric functions with their approximations:
Code:
from sympy import series, sin, cos, tan

# sin
print(latex(R[1]))
print(latex(series(R[1], x, 0, 11)))
print(latex(series(sin(x), x, 0, 11)))

# cos
print(latex(R[2]))
print(latex(series(R[2], x, 0, 11)))
print(latex(series(cos(x), x, 0, 11)))

# tan
print(latex(X))
print(latex(series(X, x, 0, 11)))
print(latex(series(tan(x), x, 0, 11)))

This results in the following output:

\frac{x \left(-7 + \frac{10}{\frac{x^{2}}{20} + 1}\right)}{3}
x - \frac{x^{3}}{6} + \frac{x^{5}}{120} - \frac{x^{7}}{2400} + \frac{x^{9}}{48000} + O\left(x^{11}\right)
x - \frac{x^{3}}{6} + \frac{x^{5}}{120} - \frac{x^{7}}{5040} + \frac{x^{9}}{362880} + O\left(x^{11}\right)

- \frac{x^{2} \left(-3 + \frac{5}{\frac{x^{2}}{30} + 1}\right)}{4} + 1
1 - \frac{x^{2}}{2} + \frac{x^{4}}{24} - \frac{x^{6}}{720} + \frac{x^{8}}{21600} - \frac{x^{10}}{648000} + O\left(x^{11}\right)
1 - \frac{x^{2}}{2} + \frac{x^{4}}{24} - \frac{x^{6}}{720} + \frac{x^{8}}{40320} - \frac{x^{10}}{3628800} + O\left(x^{11}\right)

\frac{x \left(-7 + \frac{10}{\frac{x^{2}}{20} + 1}\right)}{3 \left(- \frac{x^{2} \left(-3 + \frac{5}{\frac{x^{2}}{30} + 1}\right)}{4} + 1\right)}
x + \frac{x^{3}}{3} + \frac{2 x^{5}}{15} + \frac{43 x^{7}}{800} + \frac{3133 x^{9}}{144000} + O\left(x^{10}\right)
x + \frac{x^{3}}{3} + \frac{2 x^{5}}{15} + \frac{17 x^{7}}{315} + \frac{62 x^{9}}{2835} + O\left(x^{11}\right)




Sine

Formula
\[
s(x) = \frac{x \left(-7 + \frac{10}{\frac{x^{2}}{20} + 1}\right)}{3}
\]

Taylor Series
\[
s(x) = x - \frac{x^{3}}{6} + \frac{x^{5}}{120} - \frac{x^{7}}{2400} + \frac{x^{9}}{48000} + O\left(x^{11}\right)
\]

\[
\sin(x) = x - \frac{x^{3}}{6} + \frac{x^{5}}{120} - \frac{x^{7}}{5040} + \frac{x^{9}}{362880} + O\left(x^{11}\right)
\]

Cosine

Formula

\[
c(x) = - \frac{x^{2} \left(-3 + \frac{5}{\frac{x^{2}}{30} + 1}\right)}{4} + 1
\]

Taylor Series

\[
c(x) = 1 - \frac{x^{2}}{2} + \frac{x^{4}}{24} - \frac{x^{6}}{720} + \frac{x^{8}}{21600} - \frac{x^{10}}{648000} + O\left(x^{11}\right)
\]

\[
\cos(x) = 1 - \frac{x^{2}}{2} + \frac{x^{4}}{24} - \frac{x^{6}}{720} + \frac{x^{8}}{40320} - \frac{x^{10}}{3628800} + O\left(x^{11}\right)
\]

Tangent

Formula

\[
t(x) = \frac{x \left(-7 + \frac{10}{\frac{x^{2}}{20} + 1}\right)}{3 \left(- \frac{x^{2} \left(-3 + \frac{5}{\frac{x^{2}}{30} + 1}\right)}{4} + 1\right)}
\]

Taylor Series

\[
t(x) = x + \frac{x^{3}}{3} + \frac{2 x^{5}}{15} + \frac{43 x^{7}}{800} + \frac{3133 x^{9}}{144000} + O\left(x^{11}\right)
\]

\[
\tan(x) = x + \frac{x^{3}}{3} + \frac{2 x^{5}}{15} + \frac{17 x^{7}}{315} + \frac{62 x^{9}}{2835} + O\left(x^{11}\right)
\]


These are very good approximations.
How did you come up with these?


Somewhat related are Bhaskara's Sine and Cosine Approximations.
Find all posts by this user
Quote this message in a reply
12-23-2023, 02:21 PM
Post: #4
RE: (12C) SIN COS TAN
From here we can go full circle and use the Python to RPN - source code converter with the following programs:
Code:
def s(x):
    u = x**2
    return (10 / (u / 20 + 1) - 7) * x / 3

Code:
def c(x):
    u = x**2
    return -(5 / (u / 30 + 1) - 3) * u / 4 + 1

This generates the following programs for the HP-42S:
Code:
LBL "s"
STO 00          // param: x
RDN
RCL 00          // x
X↑2
STO 01          // u 
10
RCL 01          // u
20
/
1
+
/
7
-
RCL 00          // x
*
3
/
RTN             // return
LBL 50          // PyRPN Support Library of
"-Utility Funcs-"
RTN             // ---------------------------

Code:
LBL "c"
STO 00          // param: x
RDN
RCL 00          // x
X↑2
STO 01          // u 
-5
RCL 01          // u
30
/
1
+
/
3
-
RCL 01          // u
*
4
/
1
+
RTN             // return
LBL 50          // PyRPN Support Library of
"-Utility Funcs-"
RTN             // ---------------------------

We can combine them into a single program:
Code:
00 { 59-Byte Prgm }
01▸LBL "SCT"
02 STO 00          // param: x
03 ENTER
04 ×
05 STO 02          // u = x^2
06 10
07 X<>Y
08 20
09 ÷
10 1
11 +
12 ÷
13 7
14 -
15 RCL 00          // x
16 ×
17 3
18 ÷
19 STO 01          // sin(x)
20 5
21 RCL 02          // u
22 30
23 ÷
24 1
25 +
26 ÷
27 3
28 -
29 RCL 02          // u
30 ×
31 4
32 +/-
33 ÷
34 1
35 +
36 STO 02          // cos(x)
37 RCL 01          // sin(x)
38 X<>Y
39 ÷
40 END             // tan(x)

I know that the degrees to radians transformation is missing, but this is left as an exercise.

Example:

0.5
XEQ "SCT"

0.546301

RCL 01

0.479424

RCL 02

0.877583
Find all posts by this user
Quote this message in a reply
12-23-2023, 03:30 PM (This post was last modified: 12-23-2023 06:36 PM by Albert Chan.)
Post: #5
RE: (12C) SIN COS TAN
sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ... = x * (1 - x²/6 / (1 + x²/20)) + O(x^7)

\(\displaystyle s(x)
= x \left( 1-\frac{20}{6}\left(\frac{\frac{x^2}{20}}{1+\frac{x^2}{20}} \right) \right)
= \frac{x}{3} \left( 3-10\left(1 - \frac{1}{1+\frac{x^2}{20}} \right) \right)
= \frac{x}{3} \left(-7 + \frac{10}{1+\frac{x^{2}}{20}}\right)
\)


cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ... = 1 - x²/2 * (1 - x²/12 / (1 + x²/30)) + O(x^8)

\(\displaystyle c(x)
= 1 - \frac{x^2}{2} \left(1 - \frac{30}{12} \left(\frac{\frac{x^2}{30}}{1+\frac{x^2}{30}} \right)\right)
= 1 - \frac{x^2}{4} \left(2 - 5 \left(1 - \frac{1}{1+\frac{x^2}{30}} \right)\right)
= 1 - \frac{x^2}{4} \left(-3 + \frac{5}{1+\frac{x^2}{30}} \right)
\)

We may also reuse code: versin(x) = 1 - cos(x) = 2*sin(x/2)^2

\(\cos(x) ≈ 1 - 2\, s(\frac{x}{2})^2 \)
Find all posts by this user
Quote this message in a reply
12-23-2023, 05:54 PM
Post: #6
RE: (12C) SIN COS TAN
I always appreciate your answers but I'm often reminded of this old meme:
[Image: d6d.jpg]
Find all posts by this user
Quote this message in a reply
06-09-2024, 07:29 AM
Post: #7
RE: (12C) SIN COS TAN
Program Update from Post #1

This update make it easier to run the SIN COS TAN routine.

Procedure: FIX 4

For first use store ¶ to register R1 and 180 to register R2

355 [ENTER] 113 [÷] [STO] 1

180 [STO] 2
-------------------------------------------------------------
To run [SIN]

Make sure to run from beginning of program line by

Pressing [f] [PRGM]

SIN(x) [R/S] display answer

Continue same problem no need to start from beginning again.
-------------------------------------------------------------
To run [COS]

[GTO] 25 COS(x) [R/S] display answer

Continue same problem no need to use GTO 25 again
-------------------------------------------------------------
To run [TAN]

[GTO] 53 TAN(x) [R/S] display answer

Continue same problem no need to use GTO 53 again
-------------------------------------------------------------
Example:

SIN(15) + SIN(25)

[f] [PRGM]

15 [R/S] display 0.2588
25 [R/S] display 0.4226
[+] display answer 0.6814

COS(40) - TAN(10)

[GTO] 25
40 [R/S] display 0.7660

[GTO] 53
10 [R/S] display 0.1763

[-] display answer 0.5897
-----------------------------------------------------------
Program:
Quote:01 RCL 1
02 x
03 RCL 2
04 ÷
05 STO 0
06 ENTER
07 x
08 2
09 0
10 ÷
11 1
12 +
13 1/x
14 1
15 0
16 x
17 7
18 -
19 RCL 0
20 3
21 ÷
22 x
23 GTO 00 // Finish SIN routine
24 R/S
25 RCL 1
26 x
27 RCL 2
28 ÷
29 STO 0
30 ENTER
31 x
32 3
33 0
34 ÷
35 1
36 +
37 1/x
38 5
39 x
40 3
41 -
42 RCL 0
43 RCL 0
44 x
45 4
46 CHS
47 ÷
48 x
49 1
50 +
51 GTO 24 // Finish COS routine
52 R/S
53 RCL 1
54 x
55 RCL 2
56 ÷
57 STO 0
58 ENTER
59 x
60 . // decimal
61 4
62 CHS
63 x
64 1
65 +
66 1/x
67 5
68 x
69 1
70 +
71 RCL 0
72 6
73 ÷
74 x
75 GTO 52 // Finish TAN routine

Gamo
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)