Question about trig functions approximation



#2

Hi All,

In your opinion, among the sin, cos, and tan functions (as you get any two trig functions from the value of the third trig function), which one is easier to calculate using polynomial approximations or special approximation? If you are referring to a special approximation can you please state it or offer a link to that information?

Thanks,

Namir


#3

Quote:
Hi All,

In your opinion, among the sin, cos, and tan functions (as you get any two trig functions from the value of the third trig function), which one is easier to calculate using polynomial approximations or special approximation? If you are referring to a special approximation can you please state it or offer a link to that information?

Thanks,

Namir


Namir,

I tend to lean towards sine. However, I find the convergence for the Taylor Series for sine to be super slow. The approximations listed are stated to be good for eight-digit accuracy for the interval [0 to pi/2].

From "Scientific Analysis on the Pocket Calculator" by Jon M. Smith (published 1975):

Sine and Cosine:  error < 2 x 10^-9 with 0 <= x <= pi/2

sin x = x*(1 + x^2*(a2 + x^2*(a4 + x^2*(a6 + x^2*(a8 + a10*x^2)))))

a2 = -0.16666 66664
a4 = 0.00833 33315
a6 = -0.00019 84090
a8 = 0.00000 27526
a10 = -0.00000 00239

cos x = 1 + x^2*(a2 + x^2*(a4 + x^2*(a6 + x^2*(a8 + a10*x^2)))))

a2 = -0.49999 99963
a4 = 0.04166 66418
a6 = -0.00138 88397
a8 = 0.00002 47609
a10 = -0.00000 26050

The "compacted" approximations are said to give three-digit accuracy for [0, pi/2].

Sine and Cosine:  Error = 2 * 10^-4 and 2 * 10^-9, respectively, for 0 <= x <= pi/2

sin x = x * (1 + x^2*(a2 + a4*x^2))

a2 = -0.16605
a4 = 0.00761

cos x = 1 + x^2*(a2 + a4*x^2))

a2 = -0.49670
a4 = 0.03705

There was also a thread of calculating trig functions with the HP 12C, that was a while ago.

Hope this helps,

Eddie


#4

Thanks Eddie for the nice info!!!

Once I get sin(x) I can then calculate cos(x) and tan(x) as:

cos(x) = sqrt(1 - sin(x)^2)

And,

tan(x) = sin(x)/cos(x)


Edited: 9 Jan 2013, 1:59 p.m.


#5

Quote:
cos(x) = sqrt(1 - sin(x)^2)
For cos(x), why not just use the same sin function, adding 90° to the input first?
#6

Quote:
cos(x) = sqrt(1 - sin(x)^2)

Careful with this one. As sin(x) -> 1, cos(x) -> 0 and you're going to lose precision very quickly. A fused multiply add or using sqrt( (1-sin(x))(1+sin(x))) will assist somewhat.

Likewise cos(x) = sin(x+90 degrees) is also risky, although likely less so since cos(x) approaches 1 quadratically as x -> 0.

The 34S uses a straight Taylor series expansion (after a high precision modulo reduction step) and calculates both sin and cos at the same time. It would converge faster if I reduced the argument into the range 0 .. pi/4 or even -pi/4 .. pi/4 but this doesn't seem necessary. See sincosTaylor() in decn.c for the implementation.


- Pauli

#7

You can easily set up your own polynomial approximation with exactly taylored errors using standard tools like Excel and a few minutes time. I tried something similiar to the sine approximation you mentioned and came up with a result with 10-digit accuracy:

sin x = x*(1 + x^2*(a2 + x^2*(a4 + x^2*(a6 + x^2*(a8 + a10*x^2)))))
 
a2 = -0,16666 66660
a4 = 0,00833 33303
a6 = -0,00019 84078 2
a8 = 0,00000 27521 57
a10 = -0,00000 00238 29
Max. absolute error in [0; pi/2] is approx. +/- 2 E-11

Max. relative error in [0; pi/2] is less than +5 / -2 E-11

A rational approximation should perform even better.

Dieter

#8

Quote:
The "compacted" approximations are said to give three-digit accuracy for [0, pi/2].
Years ago when I was figuring out the functions to do in 16-bit scaled-integer in embedded systems where I didn't want the overhead of floating-point, I found it was better to calculate only 0-45° and get the rest of the circle from there, since when doing only a few terms of the taylor series with tweaked coeffients, the error jumped way up after 45°.

#9

The 0º - 45º approach I used for a >32 bit package. The hard part was strictly increasing and strictly decreasing -- and testing for that.

#10

Hi, Namir:

Quote:
In your opinion, among the sin, cos, and tan functions (as you get any two trig functions from the value of the third trig function), which one is easier to calculate using polynomial approximations or special approximation?

Let's see:

  • Using polynomial approximations, sin(x) and cos(x) are the same and the easiest, while tan(x) is slightly worse because tan(x) has poles, which polynomial approximations cannot cope with. Even if the pole is well outside the approximation's intended range after range reduction it still negatively affects accuracy.

  • Using rational approximations (the ratio of two polynomials) all three of sin(x), cos(x), tan(x) are about the same, as rational approximations can adequately cope with the rather well-behaved tan(x) poles so accuracy won't be affected.
That being so, I'd compute sin(x) using some suitable range reduction and the corresponding odd-powers minimax polynomial for that range, then derive cos(x) and tan(x) from it.

Some allegedly useful references (PDF documents):

Happy New Year 2013 and

Best regards from V.


Possibly Related Threads...
Thread Author Replies Views Last Post
  Trig vs hyperbolic handling differences in Prime CAS Michael de Estrada 3 518 11-08-2013, 06:26 PM
Last Post: Mark Hardman
  [41CL] New Extra Functions version Monte Dalrymple 0 351 11-08-2013, 04:32 PM
Last Post: Monte Dalrymple
  HP Prime: in need of help with defining functions Alberto Candel 14 1,404 10-27-2013, 10:48 AM
Last Post: Alberto Candel
  Euler's Constant Approximation Namir 0 258 10-19-2013, 11:32 AM
Last Post: Namir
  HP Prime spreadsheet functions SanS 0 775 10-04-2013, 04:23 AM
Last Post: SanS
  Stats functions on the HP34S Nicholas van Stigt 5 653 09-24-2013, 02:45 AM
Last Post: Nick_S
  Trig Functions Howard Owen 11 1,097 09-16-2013, 02:53 PM
Last Post: Fred Lusk
  50g piecewise functions Kurt Fankhauser 6 686 09-15-2013, 08:01 PM
Last Post: Kurt Fankhauser
  trig scales on the Post Versalog slide rule Al 12 1,177 09-15-2013, 06:01 AM
Last Post: John I.
  Missing functions on the HP Prime!!!??? :-( Namir 6 680 08-22-2013, 08:40 AM
Last Post: Gilles Carpentier

Forum Jump: