Post Reply 
Simple Series to calculate tan(x)
09-12-2016, 10:09 AM
Post: #4
RE: Simple Series to calculate tan(x)
(09-07-2016 07:10 PM)Dieter Wrote:  
(09-07-2016 05:59 PM)Namir Wrote:  I found a simple series to calculate tan(x):

Code:
...
  K = 0
  Do
    K = K + 1
    Term = 1 / ((2 * K - 1) ^ 2 - X2)
    Sum = Sum + Term
  Loop Until Abs(Term) < TOLER
...

This should run faster as is uses less arithmetics and avoids a power function:

Code:
...
  K = 1
  Do
    Term = 1 / (K * K - X2)
    Sum = Sum + Term
    K = K + 2
  Loop Until Abs(Term) < TOLER
...

Dieter

OK, I run this on RPN-67 Pro. It took me a while to figure out that X in Namir's last line refers to the original x argument, not the X calculated in the first line, as the code would suggest.
Code:

  X = 2 * X / Pi
  ....
  MyTan = 4 * X / Pi * Sum

Anyway, here' the result. With tolerance = 1E-8, we get
tan(pi/4) = 0.999968175
Iterations: 5000
Running time on iPad Pro:
7.8 secs (original version)
5.9 secs (optimized version)

Here's the optimized version of the program. Tolerance is expected in register E.
Register I holds K, Sum is in R0, X (from line 1) in R1, X2 in R2.
Enter x (in rad) and press A to calculate:
Code:

     *LBL A:
001:  31 25 11   LBL A
002:  41         ENTER
003:  61         +
004:  35 73      π
005:  81         ÷
006:  33 01      STO 1
007:  32 54      x²
008:  33 02      STO 2
009:  00         0
010:  33 00      STO 0
011:  32 44 07   OP INCR
012:  35 33      ST I

      LBL 0:
013:  31 25 00   LBL 0
014:  35 34      RC I
015:  32 54      x²
016:  34 51 02   RCL - 2
017:  35 62      1/x
018:  33 61 00   STO + 0
019:  31 34      ISZ
020:  31 34      ISZ
021:  35 64      ABS
022:  34 15      RCL E
023:  32 71      x≤y?
024:  22 00      GTO 0   [013]
025:  04         4
026:  34 71 01   RCL × 1
027:  35 73      π
028:  81         ÷
029:  34 71 00   RCL × 0
030:  35 22      RTN
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Simple Series to calculate tan(x) - Namir - 09-07-2016, 05:59 PM
RE: Simple Series to calculate tan(x) - Willy R. Kunz - 09-12-2016 10:09 AM



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