Post Reply 
Simple Series to calculate tan(x)
09-07-2016, 05:59 PM (This post was last modified: 09-12-2016 01:36 PM by Namir.)
Post: #1
Simple Series to calculate tan(x)
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
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



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