HP Forums

Full Version: Simple Series to calculate tan(x)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I found a simple series to calculate tan(x):

tan(pi*x/2) = 4*x/pi * Sum(1/[(2k-1)^2 - x^2] for k=1 to infinity

Or,

tan(pi*x/2) = 4*x/pi * Sum(1/[(k^2 - x^2] for k=1,3,5,7,... infinity

The number of iterations depend on the tolerance used for the term 1/[(2k-1)^2 - x^2] using the first equation, and 1/[(k^2 - x^2] using the second equation. I found an approximate relation between the number of required iterations, Iters, and the tolerance, Tol:

Iters = 0.5/Sqrt(Tol)

Calculating tan(x) to very good accuracy requires a lot of iterations! The somewhat good news is that each iteration does not involve a lot of calculations.

Here is an Excel VBA implementation of the above equation:

Code:
Public Function MyTan(ByVal X As Double) As Double
  Const TOLER As Double = 0.00001 ' you can change this value
  Const Pi As Double = 3.14159265359
  Dim Sum As Double, X As Double, Term As Double
  Dim X2 As Double
  Dim K As Long
  
  X = 2 * X / Pi
  X2 = X * X
  Sum = 0
  K = 0
  Do
    K = K + 1
    Term = 1 / ((2 * K - 1) ^ 2 - X2)
    Sum = Sum + Term
  Loop Until Abs(Term) < TOLER
  MyTan = 4 * X / Pi * Sum
End Function
(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
Thanks for the improvements.

Namir
(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
The series that I mentioned in the opening of this thread is simpler than the common summation series seen in many websites (which involves using Bernoulli numbers). Using the CORDIC algorithm to calculate the trigonometric functions remains the most efficient way. My main point is to single out a simpler (but not necessarily very efficient) series to calculate tan(x). Willy Kunz's result of 5000 iterations demonstrates the inefficiency of the algorithm. Using today's calculators and computers with their fast CPU can make using the series acceptable. However, the purist mathematician/programmer can still argue that the series is not the best way to calculate tan(x). The same can be said for infinite products that calculates sin(x) and cos(x). Their formulations are simple, but they require a high number of iterations.

I found the series for tan(x) on the bottom of page 83 in the book "Handbook of Computing Elementary Functions", 1965, by Lyusternic et al. The book has a lot of series, infinite products, and various approximations for common functions found in calculators. The original authors are Russian.
(09-12-2016 10:09 AM)Willy R. Kunz Wrote: [ -> ]Anyway, here' the result. With tolerance = 1E-8, we get
tan(pi/4) = 0.999968175

This shows very nicely that if the last added term drops below a given tolerance, this does not automatically mean that the sum of all subsequent terms does so either. Here the tolerance was set to 1E–8 but the error in the result is > 3E–5.

In other words: with the given algorithm you cannot be sure that the tolerance level is actually met.

Dieter
(09-12-2016 08:28 PM)Dieter Wrote: [ -> ]
(09-12-2016 10:09 AM)Willy R. Kunz Wrote: [ -> ]Anyway, here' the result. With tolerance = 1E-8, we get
tan(pi/4) = 0.999968175

This shows very nicely that if the last added term drops below a given tolerance, this does not automatically mean that the sum of all subsequent terms does so either. Here the tolerance was set to 1E–8 but the error in the result is > 3E–5.

In other words: with the given algorithm you cannot be sure that the tolerance level is actually met.

Dieter

The use of the tolerance value is in a relative context. It is a rough indicator. Calculating the error assumes we know the exact function value. This works fine in the context of an exercise. How did the programmings in the 50s compare trig function approximations with their exact counterparts? It was not easy!
Reference URL's