# Trigonometric functions

#### 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

#### recca02

Joined Apr 2, 2007
1,214
trigonometric functions can be converted using series definitions

maybe u can use those to get values.

#### Vicktor

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

#### Papabravo

Joined Feb 24, 2006
14,864
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.

#### 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 Sorry I'm a little stupid at math.
~Vicktor

#### Papabravo

Joined Feb 24, 2006
14,864
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.

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?

#### agentofdarkness

Joined Oct 9, 2007
42
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
14,864
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?

#### agentofdarkness

Joined Oct 9, 2007
42
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
14,864
More's the pity

#### recca02

Joined Apr 2, 2007
1,214
Is it possible to use the relation between exponential and trigonometric function in this case?

#### Papabravo

Joined Feb 24, 2006
14,864
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

#### recca02

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

#### Papabravo

Joined Feb 24, 2006
14,864
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
14,864
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

#### recca02

Joined Apr 2, 2007
1,214
i see it now,
thanks.

#### 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 Help me with that and I won't try to rip off the math library of the Hi-tech PICC compiler!
~Vicktor

#### Papabravo

Joined Feb 24, 2006
14,864
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`