Simple Series to calculate tan(x)

09072016, 05:59 PM
(This post was last modified: 09122016 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/[(2k1)^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/[(2k1)^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 

09072016, 07:10 PM
(This post was last modified: 09072016 07:11 PM by Dieter.)
Post: #2




RE: Simple Series to calculate tan(x)
(09072016 05:59 PM)Namir Wrote: I found a simple series to calculate tan(x): This should run faster as is uses less arithmetics and avoids a power function: Code: ... Dieter 

09072016, 09:02 PM
Post: #3




RE: Simple Series to calculate tan(x)
Thanks for the improvements.
Namir 

09122016, 10:09 AM
Post: #4




RE: Simple Series to calculate tan(x)
(09072016 07:10 PM)Dieter Wrote:(09072016 05:59 PM)Namir Wrote: I found a simple series to calculate tan(x): OK, I run this on RPN67 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:
Anyway, here' the result. With tolerance = 1E8, 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:


09122016, 12:17 PM
(This post was last modified: 09122016 01:33 PM by Namir.)
Post: #5




RE: Simple Series to calculate tan(x)
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. 

09122016, 08:28 PM
Post: #6




RE: Simple Series to calculate tan(x)
(09122016 10:09 AM)Willy R. Kunz Wrote: Anyway, here' the result. With tolerance = 1E8, we get 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 

09122016, 11:42 PM
Post: #7




RE: Simple Series to calculate tan(x)
(09122016 08:28 PM)Dieter Wrote:(09122016 10:09 AM)Willy R. Kunz Wrote: Anyway, here' the result. With tolerance = 1E8, we get 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! 

« Next Oldest  Next Newest »

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