x_0 = 1时尝试计算e^x

Trying to compute e^x when x_0 = 1

本文关键字:计算 1时      更新时间:2023-10-16

我正在尝试计算x_0 = 1的e^x的泰勒系列扩展。我很难理解我真正想要的。我很确定我试图找到x_0 = 1时e^x时的小数近似值。但是,当我在X_0为= 0时运行此代码时,我会得到错误的输出。这使我相信我正在错误地计算此功能。

这是我的类E.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

我在做什么错?我是否错误地实施了泰勒系列?我的逻辑不正确吗?

是的,您的逻辑在某个地方不正确。

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

computeE的返回语句中,您将总和乘以e,您不需要做。总和已经是e^x。

的泰勒近似

taylor系列e^x关于 0是sum _i = 0^i = infinity(x^i/i!),因此x_0确实应该在您的程序中为0。

从技术上讲,您的computeE在您拥有X_0 = 0时计算sum的正确值,但这很奇怪。Taylor系列始于i=0,但您可以使用i=1开始循环。但是,Taylor系列的第一项是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 loop完全更改其值是因为x -x_0 = 0。)

但是,正如我所说的,无论哪种情况,您都不需要在返回语句中乘以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

"事实"必须每次计算阶乘时重置为1。它应该是局部变量而不是类变量。

当"事实"是类变量时,您让"阶乘"将其更改为6,这意味着第二次调用" fortorial"时它将具有Vaule 6。而且这只会变得更糟。删除您对"事实"的声明,然后使用此信息:

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;
}

少写代码。

不要使用阶乘。

在这里,它在Java。您应该毫不费力地将其转换为C :

/**
 * @link https://stackoverflow.com/questions/46148579/trying-to-compute-ex-when-x-0-1
 * @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;
    }
}