Easter Sunday Basic Trigs (HP12C)  Printable Version + HP Forums (https://archived.hpcalc.org/museumforum) + Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum1.html) + Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum2.html) + Thread: Easter Sunday Basic Trigs (HP12C) (/thread241312.html) 
Easter Sunday Basic Trigs (HP12C)  Gerson W. Barbosa  03312013 _{Edited by mistake. No changes.}
Edited: 1 Apr 2013, 2:48 p.m. after one or more responses were posted
Re: Easter Sunday Basic Trigs (HP12C)  Namir  03312013 Gerson, Thank you soooo much for the listing and its algorithm. This has come at a time when I am also studying the algorithms than can be used to calculate the sine function. So I have added your method to the list. The accuracy results of your method are very impressive. So far your algorithm is at the top of the hit parade, so to speak! Using the trig identities for cos(3x) and sin(2x) I can use your method to cover a wide range of angles WHILE STILL maintaining very low errors! Namir
Edited: 31 Mar 2013, 8:54 a.m.
Re: Easter Sunday Basic Trigs (HP12C)  Namir  03312013 It seems the presence of the cosh(x) function in your formula for calculating cos(x) is unusual and yet probably renders the formula stable enough to yield good results. My approach has been to find and use polynomials (either regular ones or rational ones like with the Pade approximants) and avoid as much as possible relying on functions like exp(x), ln(x), and so on.
The Pade approximants yield rational polynomials (of a while variety of orders to pick and choose from) that generally offer stable results, especially when the approximated function has a Taylor series with consecutive terms that alternate in signs causing the series to converge very slowly. Such slow convergence also requires a large number of terms to achieve small errors over a significant range of x values. Edited: 31 Mar 2013, 10:19 a.m.
Re: Easter Sunday Basic Trigs (HP12C)  Gerson W. Barbosa  03312013 Hello Namir, I am glad you have liked this approximation. I find it particularly suitable for the HP12C, since it has e^x and divide by 12 is available as a single instruction. Even with only the term x^4/12 it is good enough for practical purposes in the range [pi/4..pi/4]: plot cos(x)  (x^4/12  cosh(x) + 2), x=pi/4..pi/4 (WolframAlpha) Adding a second term significantly improves the approximation: plot cos(x)  (x^4/12 + (x^4/12)^2/140  cosh(x) + 2), x=pi/4..pi/4 (WolframAlpha) One additional term would perhaps allow for the evaluation of the function in the range [pi/2..pi/2] to 10 places without range reduction. Best regards, Gerson. P.S.: plot cos(x)  (x^4/12 + (x^4/12)^2/140 + (x^4/12)^3/138600  cosh(x) + 2), x=pi/2..pi/2
Edited: 31 Mar 2013, 1:40 p.m.
Re: Easter Sunday Basic Trigs (HP12C)  Namir  03312013 Adding the cubic term does improve accuracy a bit, and I welcome that!!! Thanks for adding the plots!!!! I have also tested other approximations for sin(x) that I found on the web. You methods puts them all to shame!! Namir
Edited: 31 Mar 2013, 1:44 p.m.
Re: Easter Sunday Basic Trigs (HP12C)  Gerson W. Barbosa  03312013 Thanks, but please keep in mind that this only works when we have e^{x} readily available, otherwise the computation of e^{x} alone would not compensate at all.
Gerson.
Re: Easter Sunday Basic Trigs (HP12C)  Thomas Klemm  03312013 May I suggest the following change: 16 x<>y The rationale behind this change is that 2  cosh(x) < 1 for x # 0, thus we gain one digit. For small values of x this makes a difference:
x  sin(x)  cos(x)  tan(x) 
Kind regards
Edited: 31 Mar 2013, 4:00 p.m.
Re: Easter Sunday Basic Trigs (HP12C)  Thomas Klemm  03312013 Quote: Maybe I do not understand you correctly, but why should an alternating series converge slowly? On the contrary: in alternating series, the Euler transformation can be used for convergence acceleration.
Kind regards Edited: 1 Apr 2013, 9:15 a.m. after one or more responses were posted
Re: Easter Sunday Basic Trigs (HP12C)  Gerson W. Barbosa  03312013 Thanks for the improvement! I've just tried the following on the fast HP12C+:
01 STO 0 With 20 in lines 14 and 15 (6 terms of the Taylor expansion) the valid range for cos(x) appears to go from pi to pi without range reduction (this have to be checked). The hp 12 Platinum might give more accurate results, but I don't know if it will be fast enough. There are many rounding errors involved, but perhaps your method should apply here. I have to leave now, so I'll see this later. Cheers, Gerson. P.S.: There was a typo in line 19 (RCL 1 instead of RCL 2);
It takes about four seconds on the hp 12 Platinum: x  sin(x)  cos(x)  tan(x)  Formula:
Edited: 31 Mar 2013, 7:36 p.m.
Re: Easter Sunday Basic Trigs (HP12C)  BShoring  03312013 Thank you for this nice program. It also works on the HP38E and HP38C which happens to be my favorite calculator, and which was the predeccessor to the HP12C.
Re: Easter Sunday Basic Trigs (HP12C)  Gerson W. Barbosa  03312013 Which do you have, the HP38E or HP38C? There is another trigs program on the software library, using another approach (minimax polynomial approximation). It's very inconvenient on the HP38E however (99 steps and 7 constants in the registers). http://www.hpmuseum.org/software/38ctrig.htm Regards,
Gerson.
Re: Easter Sunday Basic Trigs (HP12C)  Namir  03312013 Ah! I was going to ask you for the equation that generates the polynomial part.
Namir
Re: Easter Sunday Basic Trigs (HP12C)  Gerson W. Barbosa  04012013 This is better explained in this thread, message #5: http://www.hpmuseum.org/cgisys/cgiwrap/hpmuseum/archv020.cgi?read=179837
For testing purpose, the following should give at least 10 correct digits on the HP50g, in the range [pi..pi]. %%HP: T(3)A(R)F(.);
Gerson.
Re: Easter Sunday Basic Trigs (HP12C)  BShoring  04012013 Gerson, I had the C, for 20 great years of heavy use. Died about 10 years ago as has my HP41CV. Fortunately I have a 38C emulator (and many others) that works perfectly.
Thanks, Re: Easter Sunday Basic Trigs (HP12C)  Namir  04012013 I am thinking about expanding cosh(x) into a Taylor series polynomial (or even better yet a Pade approximant rattional polynomials) so that the whole evaluation of cos(x) (and then sin(x)) relies on basic operations only.
Namir
Re: Easter Sunday Basic Trigs (HP12C)  Namir  04012013 Using the following Pade approximation for cosh(x) works well:
cosh(x) = (39251520 + 18471600 x^2 + 1075032 x^4 + 14615 x^6)/(39251520  The calculated cos and sin have a very good accuracy from 0 to 89 degrees. At 90 degrees this (and many other) approximations suffer! Namir
Edited: 1 Apr 2013, 1:53 p.m.
Re: Easter Sunday Basic Trigs (HP12C)  Namir  04012013 Maybe you are correct. I will check it.
Namir
Question about COSH(X) term  Namir  04012013 Gerson, How did the cosh(x) term end up in the approximation for cos(x)????
A very curious Namir
Re: Question about COSH(X) term  Gerson W. Barbosa  04012013 Just sum the respective Taylor series together and then isolate cos x:
Regards, Gerson. _{Edited to fix a typo and to correct a mistake in the fourth paragraph, per Thomas Klemm's observation below}
Edited: 4 Apr 2013, 1:29 p.m. after one or more responses were posted
Re: Question about COSH(X) term  Namir  04012013 Very elegant and very simple!!! :)
Namir
Re: Question about COSH(X) term  Namir  04012013 One can use a similar approach for sin(x):
sin(x) = x (2 + y/60 + y^2/181440 + y^3/3113510400 +
Namir Edited: 1 Apr 2013, 4:21 p.m.
Re: Question about COSH(X) term  Les Koller  04012013 That's the way I like my math!
Re: Easter Sunday Basic Trigs (HP12C)  Gerson W. Barbosa  04012013 Thomas, I'll accept your suggestion. Listings and examples below. The results comparisons have been made against the HP15C. Cheers, Gerson.
Re: Question about COSH(X) term  Gerson W. Barbosa  04012013 I fixed a missing multiplication by 2 in the fourth line soon after posting, but forgot to do the same on the third line. Sorry for the mistake! _{It's been corrected now!}
Edited: 4 Apr 2013, 1:42 p.m.
Re: Question about COSH(X) term  Gerson W. Barbosa  04022013 Which of course works nicely :)
Re: Easter Sunday Basic Trigs (HP17BII version)  Gerson W. Barbosa  04032013 The following gives about 10 to 11 significant digits in the range [0..+/pi], in case anyone wants to try. They should work in other HP17B models as well. SIN=2*SIGMA(K:1:21:4:X^K/FACT(K))(EXP(X)EXP(X))/2 Re: Easter Sunday Basic Trigs (HP12C)  Eddie W. Shore  04042013 Gerson, Simple, elegant, and accurate (to at least 6 digits). Beautiful algorithm!
Eddie
Re: Question about COSH(X) term  Thomas Klemm  04042013 Quote: The factor 2 is missing:
But of course we still get the idea.
Kind regards Re: Question about COSH(X) term  Gerson W. Barbosa  04042013 The last line was right, nonetheless. The previous line has now been corrected, thanks! Cheers,
Gerson.
Re: Easter Sunday Basic Trigs (HP12C)  Gerson W. Barbosa  04042013 Thanks, Eddie!
Quote: The next term in the Taylor series, 2*(x)^16/16!, would contribute with only 2e13, at most (in the extremes of the [pi..pi] range, when x = pi/3). The lost of accuracy is greater for the sine function (and tangent), because it is computed as sqrt(1  cos^{2}x). The lack of at least two guard digits is also responsible for many accumulated errors. Anyway, 6 digits are quite an improvement after almost seven centuries :) "In 1342, Levi (ben Gerson) wrote On Sines, Chords and Arcs, which examined trigonometry, in particular proving the sine law for plane triangles and giving fivefigure sine tables." (From Wikipedia) Cheers, Gerson.
