Using MATLAB to find Minimax Polynomial Approximation of Trigonometric Functions
I am trying to find the minimax polynomial approximation for sine and cosine using the remez exchange algorithm in MATLAB. The need precision out to 23 bits because I am implementing the sine and cosine functions for IEEE-754 floating point.
Using this link here (refer to pages 8 through 15), the instructions are given for finding the polynomial using Mathematica and Maple, however, I am not sure how to extrapolate these methods for MATLAB.
According to Table 3, I need to use a 5th or 6th order polynomial to get ~23 bits (after the decimal point) of accuracy.
I am planning on first performing a range reduction of all incoming theta to between -pi/4 to +pi/4, then performing the sine or cosine function as necessary (The end goal is to implement exp(i*x) = cos(x) + i*sin(x).
I might be able to follow the instructions of this paper myself, but I don't know how to use the remez function for my purposes here. Also, I don't follow why the author used equation (6) (on page 9), nor do i understand how the equation for k (on page 11) was determined (where does 2796201 come from?) and why does the defining form of the polynomial we want to end up with change to sin9x) = x + kx^3 + x^5*P(x^2).
Would it be better to use the firpm function instead (since remez is deprecated)?
Thank you, all help and guidance are greatly appreciated, as well as edits to ensure the best answer possible to my question.
I'd not bother trying to develop approximations of your own. Simpler is to pick up a copy of "Computer Approximations", Hart, et al. A good university library should have it. 23 bits is about 7 decimal digits, so just pick an approximation that gives you the accuracy you need. You can choose either a simple polynomial approximation, or use a rational polynomial, usually a bit better as long as you can tolerate the divide.
Range reduction does make sense, in fact, I chose the same range (+/-pi/4) in my own tools because this choice of range is particularly easy to work with.
Edit: (An example of the use of the approximations one can find in Hart.)
Here I'll find an approximation for sin(x), where x lies in the interval [0,pi/4]. My goal will be to choose an approximation with absolute accuracy of at least 1.e-7 over that interval. Of course, if you have a negative value for x, we know that sin(x) is an odd function, so this is trivial.
I note that the approximations in Hart tend to be of the form sin(alphapix), where x lies in the interval [0,1]. If I then choose an approximation for alpha = 1/2, I'd get an approximation that is valid over the chosen interval. So for an approximation over the interval [0,pi/4] we look for alpha = 1/4.
Next, I look for an approximation that is indicated to have absolute accuracy of at least 7 digits or so, and I'll prefer to use rational polynomial approximations, since they tend to be a bit more efficient. Scanning down the tables on page 118 (my copy of Hart is from 1978) I find an approximation with alpha = 1/4 that fits the bill: index 3060.
This approximation will be of the form
sin(alpha*pi*x) = x*P(x^2)/Q(x^2)
So now I tab over to the page that gives the coefficients for SIN 3060. In my copy, it falls on pages 199-200. There are 5 coefficients, P00, P01, P02, Q00, Q01. (Watch out for the somewhat non-standard scientific notation used here.) Thus P (the numerator polynomial) has 3 terms in it, while Q, the denominator has 2 terms. Writing it out, I get this:
sin(alpha*pi*x) = (52.81860134812 -4.644800481954*x^3 + 0.0867545069521*x^5)/ ...
(67.250731777791 + x^2)
Lets try it now in MATLAB.
x = linspace(0,pi/4,10001);
xt = x*4/pi; % transform to [0,1]
sine = @(x) (52.81860134812*x -4.644800481954*x.^3 + ...
0.0867545069521*x.^5)./(67.250731777791 + x.^2);
max(abs(sin(x) -sine(xt)))
ans =
1.6424e-09
plot(x,sin(x)- sine(xt),'-')
Note the 1e-9 attached to the y-axis.
It looks like this is the most reasonable choice of approximation for sin(x) over that specific interval, although this gives about 29 bits of accuracy instead of the 23 bits you asked for. If you are willing to choose a different range reduction interval, there are a few choices that might save a term, possibly at a cost of a few bits that you don't need.
log2(max(abs(sin(x) -sine(xt))))
ans =
-29.182
链接地址: http://www.djcxy.com/p/85592.html