在 C++ 中使用泰勒级数求自然对数

Finding the Natural Logarithm of a number using Taylor Series in C++

好的,所以我花了最后 2 个小时来处理这个问题,对代码进行了一百次调整,但我什么也没得到。没有错误也没有警告,但答案是错误的。这是我的代码:

#include <iostream>
using namespace std;

void main()
{
    /***********Variable Declarations************/

    double count = 1, totalValue = 0, it, x, z=1, powe = 1, y;

    cout << "Iterations=";
    cin >> it;
    cout << "x=";
    cin >> x;
    /***************End User Input***************/

    while (count <= it)
    {
        for (int i = 0; i < powe; i++) {
            z *= (x - 1) / (x + 1);
        }
        y = (1 / powe)*z;

        totalValue = totalValue + y;
        powe = powe + 2;
        count++;
    }

    cout << "The Result is:" << 2*totalValue << endl;
}

我知道是逻辑(数学)题,但我好像找不到。谢谢。

编辑:我们不允许使用任何其他库。

你的方法效率低下,执行错误。

这是错误的,因为计算 2N 次方的内循环已损坏。每次在内循环之前,您都需要将 z 重置为 1。

不过不要这样做,您根本不需要内循环。

为了计算数列的第 N 个成员,您不需要从一开始就计算同一个旧数的 2N 次方。您刚刚在上一步中计算了该数字的 2N-2 次方。使用它。

您忘记在 while 的每次迭代中将 z 设置为 1:

#include <iostream>
using namespace std;


void main()
{
/***********Variable Declarations************/

double count = 1, totalValue = 0, it, x, z=1, powe = 1, y;

cout << "Iterations=";
cin >> it;
cout << "x=";
cin >> x;
/***************End User Input***************/



while (count <= it)
{

    for (int i = 0; i < powe; i++) {
        z *= (x - 1) / (x + 1);
    }
    y = (1 / powe)*z;

    totalValue = totalValue + y;
    powe = powe + 2;
    count++;
    z = 1; //Without this line you will have very high powers
}

cout << "The Result is:" << 2*totalValue << endl;

}

编辑:

您可以通过不必一直从头开始计算功率来优化您的方法:

#include <iostream>
using namespace std;


void main()
{
/***********Variable Declarations************/

double count = 1, totalValue = 0, it, x, z, powe = 1, y;

cout << "Iterations=";
cin >> it;
cout << "x=";
cin >> x;
z = (x + 1) / (x - 1); //We start from power -1, to make sure we get the right power in each iteration;
//Store step to not have to calculate it each time
double step = ((x - 1) * (x - 1)) / ((x + 1) * (x + 1));
/***************End User Input***************/



while (count <= it)
{

    z * = step;
    y = (1 / powe)*z;

    totalValue = totalValue + y;
    powe = powe + 2;
    count++;
    //We no longer need to set z to 1, as the previous value becomes useful
}

cout << "The Result is:" << 2*totalValue << endl;

}

一项改进可能是避免计算 (x - 1) / (x + 1) 在一个内循环中 这是一个使用累加器的解决方案 使用我的编程语言 VRCalc++ 编写 ...

// OK !!!

@func user_log_opt (x) static
{
    ratio = (x - 1) / (x + 1),

    accumul = ratio,

    total = accumul,

    power = 3,
    n = 20,
    @while (power < n) {

        accumul *= ratio,

        accumul *= ratio,

        total += (1 / power) * accumul,

        power += 2
    },
    2.0 * total
},

有关 VRCalc++ 的更多信息,请搜索您喜欢的内容 搜索引擎 ...

以下是上述解决方案的C++版本 (还包括 Exp(x) 函数)...

// C++

namespace VRAxSamples {

long double Exp (long double x)
{
    long double result = 1.0;
    long double power_of_x = 1.0;
    long int fact_of_k = 1;
    int n = 16;
    int k = 0;
    while (k < n) {
        power_of_x *= x;
        fact_of_k *= (k + 1);
        result += (power_of_x / (long double) fact_of_k);
        ++k;
    }
    return result;
}

long double Ln (long double x)
{
    long double ratio = (x - 1) / (x + 1);

    long double accumul = ratio;

    long double total = accumul;

    int power = 3;
    int n = 20;
    while (power < n) {

        accumul *= ratio;

        accumul *= ratio;

        total += (1 / (long double) power) * accumul;

        power += 2;
    }

    return 2.0 * total;
}


} // namespace

这就是所有人...