简体   繁体   English

使用MATLAB查找三角函数的Minimax多项式逼近

[英]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. 我正在尝试使用MATLAB中的remez交换算法找到正弦和余弦的极小多项式逼近。 The need precision out to 23 bits because I am implementing the sine and cosine functions for IEEE-754 floating point. 因为我要为IEEE-754浮点实现正弦和余弦函数,所以需要精确到23位。

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. 使用此处的链接(请参阅第8至15页),给出了使用Mathematica和Maple查找多项式的指令,但是,我不确定如何将这些方法外推到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. 根据表3,我需要使用5或6阶多项式来获得〜23位(小数点后)的精度。

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). 我计划首先执行将所有传入theta的范围缩小到-pi / 4到+ pi / 4之间,然后根据需要执行正弦或余弦函数(最终目标是实现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. 我也许可以自己按照本文的说明进行操作,但是在这里我不知道如何使用remez函数。 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). 另外,我既不理解为什么作者使用方程式(6)(第9页),也不理解k的方程式(第11页)是如何确定的(2796021来自何处?)以及为什么要进行定义我们想以多项式的形式最终以sin9x)= x + kx ^ 3 + x ^ 5 * P(x ^ 2)结束。

Would it be better to use the firpm function instead (since remez is deprecated)? 改用firpm函数会更好吗(因为不推荐使用remez )?

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. 更简单的是拿起《计算机近似》的副本,Hart等人。 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. 23位大约是7个十进制数字,因此只需选择一个近似值即可为您提供所需的精度。 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. 缩小范围确实是有道理的,实际上,我在自己的工具中选择了相同的范围(+/- pi / 4),因为这种选择范围特别容易使用。

Edit: (An example of the use of the approximations one can find in Hart.) 编辑:(一个示例的用法可以在Hart中找到。)

Here I'll find an approximation for sin(x), where x lies in the interval [0,pi/4]. 在这里,我将找到sin(x)的近似值,其中x位于间隔[0,pi / 4]中。 My goal will be to choose an approximation with absolute accuracy of at least 1.e-7 over that interval. 我的目标是在该间隔内选择一个绝对精度至少为1.e-7的近似值。 Of course, if you have a negative value for x, we know that sin(x) is an odd function, so this is trivial. 当然,如果x的值为负,我们知道sin(x)是一个奇数函数,因此这是微不足道的。

I note that the approximations in Hart tend to be of the form sin(alpha pi x), where x lies in the interval [0,1]. 我注意到,Hart中的近似值趋于采用sin(alpha pi x)的形式,其中x处于区间[0,1]中。 If I then choose an approximation for alpha = 1/2, I'd get an approximation that is valid over the chosen interval. 如果然后选择alpha = 1/2的近似值,那么我得到的近似值在选定的时间间隔内有效。 So for an approximation over the interval [0,pi/4] we look for alpha = 1/4. 因此,对于区间[0,pi / 4]的近似值,我们寻找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. 接下来,我将寻找一个表示其绝对精度至少为7位数左右的近似值,并且我将更喜欢使用有理多项式近似值,因为它们往往会更有效率。 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. 向下浏览第118页的表格(我的Hart副本来自1978年),我发现α= 1/4的近似值适合该要求:索引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. 因此,现在我转到提供SIN 3060系数的页面。在我的副本中,它位于199-200页上。 There are 5 coefficients, P00, P01, P02, Q00, Q01. 有5个系数,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. (请注意此处使用的某种非标准科学符号。)因此,P(分子多项式)中有3个项,而Q分母有2个项。 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. 现在让我们在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),'-')

正弦逼近[0,pi / 4]的误差

Note the 1e-9 attached to the y-axis. 请注意1e-9附加到y轴。

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. 看起来这是在该特定时间间隔内对sin(x)逼近的最合理选择,尽管这提供了大约29位的精度,而不是您要求的23位。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM