C中的反正弦近似 [英] Approximation of arcsin in C

查看:121
本文介绍了C中的反正弦近似的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以根据泰勒级数计算反正弦值.

I've got a program that calculates the approximation of an arcsin value based on Taylor's series.

我和我的朋友想出了一种算法,该算法能够返回几乎正确"的值,但是我认为我们做得并不那么清晰.看看:

My friend and I have come up with an algorithm which has been able to return the almost "right" values, but I don't think we've done it very crisply. Take a look:

double my_asin(double x)
{
    double a = 0;
    int i = 0;
    double sum = 0;
    a = x;
    for(i = 1; i < 23500; i++)
    {
        sum += a;
        a = next(a, x, i);
    }
}

double next(double a, double x, int i)
{
    return a*((my_pow(2*i-1, 2)) / ((2*i)*(2*i+1)*my_pow(x, 2)));
}

我检查了my_pow是否正常工作,因此也不需要在这里发布它.基本上,我希望循环在当前项与下一项之间的差大于或等于我的EPSILON(0.00001)时结束,这是我在计算平方根时使用的精度.

I checked if my_pow works correctly so there's no need for me to post it here as well. Basically I want the loop to end once the difference between the current and next term is more or equal to my EPSILON (0.00001), which is the precision I'm using when calculating a square root.

这就是我希望的工作方式:

This is how I would like it to work:

while(my_abs(prev_term - next_term) >= EPSILON)

但是函数 double next 依赖于 i ,所以我想我也必须在while语句中增加它.有什么想法我应该怎么做吗?

But the function double next is dependent on i, so I guess I'd have to increment it in the while statement too. Any ideas how I should go about doing this?

-1的示例输出:

$ -1.5675516116e+00

代替:

$ -1.5707963268e+00


非常感谢你们.


Thanks so much guys.

推荐答案

与您的代码和问题有关的问题包括:

Issues with your code and question include:

  1. 您的显示arcsin泰勒级数的图像文件有两个错误: x 5 项上有一个负号,而不是加号,并且 x 显示为 x n ,但应为 x 2 n +1 .
  2. 泰勒级数中的
  3. x 因子在每一项中反义词增加 x 2 ,但您的公式a*((my_pow(2*i-1, 2)) / ((2*i)*(2*i+1)*my_pow(x, 2))) 除以 x 2 .这与您要求的特定值-1无关紧要,但对于其他值(除1之外),它会产生错误的结果.
  4. 您询问一旦术语差异大于或等于"ε时如何结束循环,但是,对于大多数 x 值,您实际上希望小于(或者相反,您想要继续,而不是结束,而差异大于或等于(如代码所示).
  5. 泰勒级数是评估函数的一种较差的方法,因为随着距该级数中心的点越远,其误差就会增加.大多数此类函数的数学库实现都使用minimax系列或与其相关的东西.
  6. 评估从低阶项到高阶项的序列会导致您先添加较大的值,然后再添加较小的值.由于浮点运算的性质,这意味着从较小的术语中丢失了精度,因为较大的值将其赶出"了浮点格式的宽度.这种效果将限制任何结果的准确性.
  7. 最后,要直接查询您的代码结构方式,请直接更新a,因此您永远不会同时拥有上一个术语和下一个术语.相反,请创建另一个double b,以便为上一个术语创建一个对象b,为当前术语创建一个对象a,如下所示.
  1. Your image file showing the Taylor series for arcsin has two errors: There is a minus sign on the x5 term instead of a plus sign, and the power of x is shown as xn but should be x2n+1.
  2. The x factor in the terms of the Taylor series for arcsin increases by x2 in each term, but your formula a*((my_pow(2*i-1, 2)) / ((2*i)*(2*i+1)*my_pow(x, 2))) divides by x2 in each term. This does not matter for the particular value -1 you ask about, but it will produce wrong results for other values, except 1.
  3. You ask how to end the loop once the difference in terms is "more or equal to" your epsilon, but, for most values of x, you actually want less than (or, conversely, you want to continue, not end, while the difference is greater than or equal to, as you show in code).
  4. The Taylor series is a poor way to evaluate functions because its error increases as you get farther from the point around which the series is centered. Most math library implementations of functions like this use a minimax series or something related to it.
  5. Evaluating the series from low-order terms to high-order terms causes you to add larger values first, then smaller values later. Due to the nature of floating-point arithmetic, this means that accuracy from the smaller terms is lost, because it is "pushed out" of the width of the floating-point format by the larger values. This effect will limit how accurate any result can be.
  6. Finally, to get directly to your question, the way you have structured the code, you directly update a, so you never have both the previous term and the next term at the same time. Instead, create another double b so that you have an object b for a previous term and an object a for the current term, as shown below.

示例:

double a = x, b, sum = a;
int i = 0;
do
{
    b = a;
    a = next(a, x, ++i);
    sum += a;
} while (abs(b-a) > threshold);

这篇关于C中的反正弦近似的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆