当 x_0 = 1 时尝试计算 e^x

Trying to compute e^x when x_0 = 1

我正在尝试计算 x_0 = 1 处 e^x 的泰勒级数展开。我很难理解我真正在寻找什么。我很确定我正在尝试找到当 x_0 = 1 时的 e^x 的小数近似值。但是,当我在 运行 这段代码中 x_0 为 = 0 时,我得到了错误的输出。这让我相信我计算错误。

这是我的classe.hpp

#ifndef E_HPP
#define E_HPP

class E
{
    public:
        int factorial(int n);
        double computeE();

    private:
        int fact = 1;
        int x_0 = 1;
        int x = 1;
        int N = 10;
        double e = 2.718;
        double sum = 0.0;

};

这是我的 e.cpp

#include "e.hpp"
#include <cmath>
#include <iostream>

int E::factorial(int n)
{
    if(n == 0) return 1;
    for(int i = 1; i <= n; ++i)
    {
        fact = fact * i;
    }

    return fact;
}

double E::computeE()
{
    sum = std::pow(e,x_0);

    for(int i = 1; i < N; ++i)
    {
        sum += ((std::pow(x-x_0,i))/factorial(i));
    }
    return e * sum;
}

在main.cpp

#include "e.hpp"
#include <iostream>
#include <cmath>

int main()
{
    E a;
    std::cout << "E calculated at x_0 = 1: " << a.computeE() << std::endl;
    std::cout << "E Calculated with std::exp: " << std::exp(1) << std::endl;
}

输出:
E calculated at x_0 = 1: 7.38752
E calculated with std::exp: 2.71828

当我改成x_0 = 0.
E calculated at x_0 = 0: 7.03102
E calculated with std::exp: 2.71828

我做错了什么?我是否错误地实施了泰勒级数?我的逻辑在某处不正确吗?

每次计算阶乘时,

"fact" 必须重置为 1。它应该是局部变量而不是 class 变量。

当 "fact" 是一个 class 变量时,您让 "factorial" 将其更改为 6,这意味着当您调用 [=17 时它将具有值 6 =]第二次。这只会变得更糟。删除 "fact" 的声明并改用它:

int E::factorial(int n)
{
    int fact = 1;
    if(n == 0) return 1;
    for(int i = 1; i <= n; ++i)
    {
        fact = fact * i;
    }

    return fact;
}

是啊,你的逻辑哪里不对。

正如 Dan 所说,每次计算阶乘时都必须将 fact 重置为 1。您甚至可以将其设为 factorial 函数的本地。

computeE 的 return 语句中,您将总和乘以 e,您不需要这样做。总和已经是e^x的泰勒近似。

关于 0 的 e^x 的泰勒级数是 sum _i=0 ^i=infinity (x^i / i!),所以 x_0 在你的程序中确实应该是 0。

从技术上讲,当 x_0=0 时,您的 computeE 会计算 sum 的正确值,但这有点奇怪。泰勒级数从 i=0 开始,但您从 i=1 开始循环。但是,泰勒级数的第一项是 x^0 / 0! = 1 并且您将 sum 初始化为 std::pow(e, x_0) = std::pow(e, 0) = 1 所以它在数学上可以计算出来。

(你的 computeE 函数 计算了 sum 的正确值,而你有 x_0 = 1。你初始化了 sum到 std::pow(e, 1) = e,然后 for 循环根本没有改变它的值,因为 x - x_0 = 0.)

然而,正如我所说,在任何一种情况下,您都不需要在 return 语句中将它乘以 e

我会将 computeE 代码更改为:

double E::computeE()
{
    sum = 0;
    for(int i = 0; i < N; ++i)
    {
        sum += ((std::pow(x-x_0,i))/factorial(i));
        cout << sum << endl;
    }
    return sum;
}

并设置x_0 = 0.

少写代码。

不要使用阶乘。

这里是 Java。将其转换为 C++ 应该没有问题:

/**
 * @link 
 * @link https://en.wikipedia.org/wiki/Taylor_series
 */
public class TaylorSeries {

    private static final int DEFAULT_NUM_TERMS = 50;

    public static void main(String[] args) {
        int xmax = (args.length > 0) ? Integer.valueOf(args[0]) : 10;
        for (int i = 0; i < xmax; ++i) {
            System.out.println(String.format("x: %10.5f series exp(x): %10.5f function exp(x): %10.5f", (double)i, exp(i), Math.exp(i)));
        }
    }

    public static double exp(double x) {
        return exp(DEFAULT_NUM_TERMS, x);
    }

    // This is the Taylor series for exp that you want to port to C++
    public static double exp(int n, double x) {
        double value = 1.0;
        double term = 1.0;
        for (int i = 1; i <= n; ++i) {
            term *= x/i;
            value += term;
        }
        return value;
    }
}