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时运行此代码时,我会得到错误的输出。这使我相信我正在错误地计算此功能。
这是我的类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;
}
}
- 在 OpenGL 中计算矩阵时,转换的正确顺序是什么?
- C++ 计算编译时常量,同时防止整数常量溢出
- 特征库:计算倒数时静态和动态大小矩阵之间的不同行为
- 在计算num_cpus时,gutil 的 sysinfo.cc 是否缺少右括号?
- 在尝试为 RSA 实现计算 D 时,D 总是为负数
- 使用c++中的堆栈计算后缀时处理十进制值
- OpenCV 断言在计算时刻时失败
- 在C++中计算暂停时出现未知问题
- 当在计算constexpr时抛出异常时会发生什么
- For 循环结束自身并终止正在运行的程序,但在删除斜率计算语句时正常运行
- if 语句在应该计算 TRUE 时不计算 TRUE
- 计算字符时遇到麻烦
- 使用数组跟踪在计算分数时使用的骰子
- 在计算百分比时具有垃圾数
- 在C++中,是否有内置的方法来计算编译时的功率
- 如何驱动 C#、C++ 或 Java 编译器计算 1+2+3+.编译时为 +1000
- 计算平均值时重置总和
- 为什么pow()计算错误时,Webkit正在运行
- 在C++中计算公式时遇到问题
- 在C++11中计算编译时的斐波那契数(递归方法)(constexpr)