Trigonometric functions

Thread Starter

Vicktor

Joined Nov 20, 2007
13
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 ;)
 

Papabravo

Joined Feb 24, 2006
13,940
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.
 

Thread Starter

Vicktor

Joined Nov 20, 2007
13
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
 

Papabravo

Joined Feb 24, 2006
13,940
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?
 
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.
 

Papabravo

Joined Feb 24, 2006
13,940
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?
 
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.
 

Papabravo

Joined Feb 24, 2006
13,940
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-Mathematical-Functions-Formulas-Graphs/dp/0486612724/ref=pd_bbs_2?ie=UTF8&s=books&qid=1195915938&sr=1-2
 

Papabravo

Joined Feb 24, 2006
13,940
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.
 

recca02

Joined Apr 2, 2007
1,214
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?
 

Papabravo

Joined Feb 24, 2006
13,940
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
 

Thread Starter

Vicktor

Joined Nov 20, 2007
13
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
 

Papabravo

Joined Feb 24, 2006
13,940
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:

Rich (BB code):
cos(x) = 1 +a2*x^2+a4*x^4
a2 = -0.49670
a4= 0.03705
for 0 <= x <= pi/2
with an error less than or equal to 9e-4
 
Top