Trigonometric functions

Discussion in 'Programmer's Corner' started by Vicktor, Nov 20, 2007.

  1. Vicktor

    Thread Starter Member

    Nov 20, 2007
    13
    0
    Hello there, I'm designing a calculator using a PIC16F628 and I would like to add to it the trigonometric functions (i.e. sin, cos, tan, cot). The question is, is there a possible way to do so, without using tables. Some kind of algorithm I mean in Asm or C for example. I would appreciate it!
    ~Vicktor
    P.S. First post ;)
     
  2. recca02

    Senior Member

    Apr 2, 2007
    1,211
    0
    trigonometric functions can be converted using series definitions

    maybe u can use those to get values.
     
  3. Salgat

    Active Member

    Dec 23, 2006
    215
    1
  4. Vicktor

    Thread Starter Member

    Nov 20, 2007
    13
    0
    Wow, thank you both for your support! To be honest, I'll definitely use the CORDIC method.
    ~Vicktor
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    Wait. The Taylor series mentioned in the article converges way too slowly to be of much use. What you want is the Chebyshev Polynomial approximation. These polynomial approximations bound the absolute magnitude of the errors over the interval of interest.
     
  6. Vicktor

    Thread Starter Member

    Nov 20, 2007
    13
    0
    I'm sorry mate, but I didn't understand anything you said, can you please explain? And if possible show an example :p Sorry I'm a little stupid at math.
    ~Vicktor
     
  7. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    The Taylor series is an infinite series of terms that will eventually converge to any function you care to name, like sine, cosine, and so forth. The big problem with using them for preactical calculations is that they converge to the correct value slowly, require a large number of terms, and the errors increase as you get further away from the point of expansion. The article that recca02 pointed you too is useless for your purposes.

    The Chebyshev polynmoials on the other hand converge quickly with a small number of terms and the magnitude of the error over the interval of interest is bounded. That means that over the whole interval where you want to employ the polynomial approximation the errors can't get bigger than some small number which depends on the number of terms chosen.

    Try the following links:
    http://en.wikipedia.org/wiki/Taylor_series
    http://math.fullerton.edu/mathews/n2003/ChebyshevPolyMod.html

    How many bits do you plan to use for representing your numbers?
     
  8. agentofdarkness

    Active Member

    Oct 9, 2007
    42
    0
    I'm writing a sine function in Microsoft ASM right now actually. Almost done. Cosine is similar except you change n from odd to even in the Taylor series. The way I am doing it is by getting an accurate approximation between -2pi and +2pi (9th order Taylor series for sine). Then I take any number and convert it to the equivalent value between -2pi and 2pi. It works pretty well, I still need to add the 9th term to the series to get a better approximation. It does use the FPU though. You could do tan by sin/cos and the same for other functions.
     
  9. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    You'll be amazed at the difference in error behavior between a 9th order Taylor Series and a 9th order Chebyshev Polynomial. What fixed point are you using for the Taylor Series Expansion?
     
  10. agentofdarkness

    Active Member

    Oct 9, 2007
    42
    0
    Zero, I'm not shifting the polynomial at all. The assignment is to use a Taylor expansion and I don't know what a Chebyshev Polynomial is. I'm not too concerned with the accuracy, its just for an assignment but I will probably end up going to the 11th order.
     
  11. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    More's the pity
     
  12. recca02

    Senior Member

    Apr 2, 2007
    1,211
    0
    Is it possible to use the relation between exponential and trigonometric function in this case?
     
  13. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    I don't see how that will be helpful. In the Math libraries of all C Compilers and Matlab and `R', Chebyshev Polynomials are used for evaluating those functions. In fact ALL trancendental functions are evaluated this way. The key feature of all of these libraries is to minimize the effort involved. This means selecting a polynomial that minimizes the number of operations like multiplications and additions, AND (this is the really big one) controlling the absolute magnitude of the errors over the interval of evaluation.

    In their classic work, Abramoitz and Stegun, give you many useful polynomial approximations. You should check them out. Even if you don't use one of their polynomial approximations, their tables of function values can be used to verify your own calculations.

    http://www.amazon.com/Handbook-Math...d_bbs_2?ie=UTF8&s=books&qid=1195915938&sr=1-2
     
  14. recca02

    Senior Member

    Apr 2, 2007
    1,211
    0
    i dont have much idea about what is used in programming:(
    nor do i remember learning about Chebyshev Polynomials in engg.

    What i wanted to know was:
    The relation between trigonometric function and exponentials which is not an approximation at all, can that be used for calculation of the functions?
    wud that not be more accurate?:confused:
     
  15. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    In digital computers the exponential and logarithm functions are evaluated in the same way as the sine and the cosine and the tangent. They are all transcendental functions and they are all evaluated with polynomials of one sort or another. Using an exact relationship between trigonometric functions and exponential functions is not helpful since you trade the valuation of one polynomial for another. You might as well evaluate the polynomial with the best performance. We already know what polynomial that is. If you don't want to exert the mental effort to research and learn something new then by all means continue on the path you have chosen. I really could not care less.
     
  16. recca02

    Senior Member

    Apr 2, 2007
    1,211
    0
    somehow i did not make myself clear.

    i m not concerned about Taylor's series,or polynomials.

    i want to know(since my knowledge in this field is limited) whether an operation
    of the type: e^(ix) possible?
    if at all it is, is the answer not accurate enough to be straight away used for finding values of trigonometric functions?
     
  17. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    NO! For the third and last time, an exponential function like e^(ix) would be evaluated with a polynomial like ALL TRANSCENTATAL FUNCTIONS ARE EVALUATED. It is not helpful to substitute one type of polynomial evaluation for another one. If you aready have a calculation engine such as a TI-89 or Excel and you want to evaluate the sine of an angle you do it directly. Those engines will of course use a polynomial approximation to evaluate the function for you.

    If you want to write a program for a microcontroller that does not have such a library then you have to go back to first principles. That requires the use of a polynomial approximation. It is the only tool that we have in the tool bag to evaluate (here's that word again) TRANSCENDENTAL FUNCTIONS. There simply is NO OTHER WAY.

    Done, Fini, & Basta cosi
     
  18. recca02

    Senior Member

    Apr 2, 2007
    1,211
    0
    i see it now,
    i didnt read the previous reply carefully,
    thanks.
     
  19. Vicktor

    Thread Starter Member

    Nov 20, 2007
    13
    0
    Wow, I didn't realise that the topic will gain such popularity! :) Anyways, I'm still looking for a nice, juicy PIC asm sourcecode :D Help me with that and I won't try to rip off the math library of the Hi-tech PICC compiler! :cool:
    ~Vicktor
     
  20. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    It will be a two step process.
    1. Find a polynomial approximation of interest
    2. Write an efficient polynomial evaluator
    For example on p. 76 Abramowitz and Stegun we find the following:

    Code ( (Unknown Language)):
    1.  
    2. cos(x) = 1 +a2*x^2+a4*x^4
    3. a2 = -0.49670
    4. a4= 0.03705
    5. for 0 <= x <= pi/2
    6. with an error less than or equal to 9e-4
    7.  
     
Loading...