Technical Article

Sine/Cosine Approximations Simplify Controller Operation

October 06, 2022 by Harry Trietley

Programming languages often include sine and cosine functions. They simplify programming, but consume valuable memory space and computation time. This can be overcome using simple approximations.

In a recent design, we needed to drive X and Y positioners to create circular and spiral motions. Our client for that project, Island e-Beam, manufactures vacuum deposition equipment. The material to be deposited is evaporated by bombarding it with an electron beam. They needed to sweep the beam in a controlled fashion. More specifically, they need a controller to modulate currents to the X and Y beam-positioning coils. Figure 1 illustrates circular motion.

 

using x and y positioners to drive circular motion

Figure 1. Using X and Y positioners to drive circular motion. Image used courtesy of SRQ Technology

 

The system was to be controlled from a small microcontroller. Operating speed requirements and limited memory ruled out using full sine and cosine functions. This application did not need high accuracy, so we mathematically studied three approximations. The third, and simplest, met our requirements, so we used it.

 

Taylor Series Approximation

Sine and cosine can be expressed by infinitely-long Taylor series equations:

 

$$sin(\theta)= \theta - \frac{ \theta ^3}{3!} + \frac{ \theta ^5}{5!} - \frac{ \theta ^7}{7!} + …$$

$$cos(\theta)= 1 - \frac{ \theta ^2}{2!} + \frac{ \theta ^4}{4!} - \frac{ \theta ^6}{6!} + …$$

 

In this example, as with many trigonometric computers, θ is in radians, not degrees. One radian equals (180/π) or about 57.296 degrees. 

“!” stands for factorial. 2! = (2 x 1), 3! = (3 x 2 x 1), 4! = (4 x 3 x 2 x 1), etc.

When the angles are small, the higher-order terms often can be ignored. The simple approximation involves limiting the angle to between plus and minus 45 degrees and dropping the higher terms. For angles higher than 45, sine/cosine relationships can be used to find an equivalent function. We’ll get to that shortly.

 

First Approximation - Drop All Terms Higher Than Sixth Order

$$sin(\theta)= \theta - \frac{ \theta ^3}{6} + \frac{ \theta ^5}{120}$$

$$cos(\theta)= 1 - \frac{ \theta ^2}{2} + \frac{ \theta ^4}{24} - \frac{ \theta ^6}{720}$$

 

Remember, these are for radians, not degrees. Assuming you’re using degrees, you’ll first need to divide degrees by 57.296 to convert to radians.

Accuracy? For angles between plus and minus 45 degrees, sine is \( \pm \)0.0004, cosine is \( \pm \)0.00004. 

 

Second Approximation - Drop All Terms Higher Than Third Order

In addition to dropping the higher-order terms, we varied the denominators experimentally for improved accuracy.

$$sin(\theta)= \theta - \frac{ \theta ^3}{6.16}$$

$$cos(\theta)= 1 - \frac{ \theta ^2}{2.085}$$

 

The result? Still pretty good. Up to \( \pm \)45 degrees sine is within \( \pm \)0.004, cosine, \( \pm \)0.003.

 

Angles Beyond 45 Degrees

Going back to high school geometry, using degree notation, remember: 

 

$$sin( \theta) = cos(90- \theta)$$

$$cos( \theta) = sin(90- \theta)$$

 

sine-cosine relationships

Figure 2. Illustration of sine-cosine relationships. Image used courtesy of the author
 

Figure 2 illustrates sine-cosine relationships. You can use this between 45 and 135 degrees. Above 135 degrees, looking at the figure:

  

From 135 to 225 degrees:

$$sin( \theta) = -sin(180- \theta)$$

$$cos( \theta) = -cos(180- \theta)$$

*Note that these are negative.


 

From 225 to to 315 degrees:

$$sin( \theta) = -cos(270- \theta)$$

$$cos( \theta) = -sin(270- \theta)$$

*Again, these are negative.


 

Remember that, for higher angles:

$$sin( \theta) = sin( \theta -360)$$

$$cos( \theta) = cos( \theta -360)$$

 

For angles over 315 degrees or below -45, simply add or subtract 360 until they are between -45 and +315. Once they are, you can use the above equations.

 

Even Simpler - Straight Line Approximation

Our application did not need high accuracy: anything better than \( \pm \)2% would be fine. It was more important to keep the computation time as low as possible, so we investigated straight line approximations. This eliminated computing squares and higher powers: only addition and multiplication were needed. 

Using a spreadsheet, we experimented with various straight-line equations. For sine, we settled on four line segment approximations between 0 and 90 degrees. We did not do cosine but, instead, simply used \(cos( \theta)=sin(90 - \theta) \). 

Here are the results. Note—these equations are in degrees, not radians.

 

From 0 to 30 degrees:

$$sin( \theta) =0.01667( \theta)$$


 

From 30 to 50 degrees:

$$sin( \theta) =0.1009+0.0133( \theta)$$


 

From 50 to 70 degrees:

$$sin( \theta) =0.3319+0.008682( \theta)$$


 

From 70 to 90 degrees:

$$sin( \theta) =0.7286+0.003015( \theta)$$

 

The worst-case accuracy is \( \pm \)0.015 (1.5%), better than needed in this application.

 

For Angles Beyond 90 Degrees

From 90 to 180 degrees:

$$sin( \theta) = sin(180- \theta)$$


 

From 180 to 270 degrees:

$$sin( \theta) = -sin( \theta-180)$$

*Note that this is negative.


 

From 270 to 360 degrees:

$$sin( \theta) = -sin(360- \theta)$$

*Again, note that this is negative.

 

For negative angles, or angles over 360 degrees, add or subtract 360 until they are between zero and 360 degrees.

 

In summary, if the angle is not between zero and 360 degrees, add or subtract 360 until it is. Then:

 

If (θ) is between zero and 30 degrees:

$$sin( \theta) =0.01667( \theta)$$ 


 

If (θ) is between 30 and 50 degrees:

$$sin( \theta) =0.1009+0.0133( \theta)$$


 

If (θ) is between 50 and 70 degrees:

$$sin( \theta) =0.3319+0.008682( \theta)$$


 

If (θ) is between 70 and 90 degrees:

$$sin( \theta) =0.7286+0.003015( \theta)$$

 

To find the cosine, simply calculate \( sin(90 - \theta) \).

 

Applying Sine/Cosine Approximations

The work presented here was done to simplify the programming and increase the operating speed of an electron beam positioning system. It can be applied to many other applications as well. You might use it to drive a mechanical positioning system, a cutter, or a printer drawing circles. In our application, we created spirals using \(X=Rcos( \theta) \), \(Y=Rsin( \theta) \) and varying \( R \). You also could create ovals by increasing and decreasing the radius depending on the angle. I’m sure you can think of others.

 

Featured image used courtesy of Canva
2 Comments
  • pnachtwey October 07, 2022

    Back in the dark ages I used a 16 bit micro controller and a 256 element look up table that represent the angles from 0 to 90 degrees.  I used the upper 8 bits to index into this table and the lower 8 bits to interpolate between two entries.  My angles went from 0 to 1 or 1 to 0 depending whether I needed a sin() or cosin() function.

    Like. Reply
  • E
    Edward Mulder October 14, 2022

    More than 20 years ago, I worked on a project where we had to use a resolver to measure position. That position had to be used by a PLC, which had no trigonometric functions on board. Since a PLC with sine/cosine functions was $300 more expensive and I had to use 40 PLC’s, I chose the same approach as mentioned in the article. Linear interpolation in 4 or 5 sections. I ended up having the same 1…2% error, which was acceptable.

    Like. Reply