我正在研究使用泰勒级数近似 e^x 的项目,其中 x 是用户输入的值。我们给出的测试值是 x=.5、x=1.0 和 x=1.5。目标是让输出应该是一个表,其中包含创建系列的循环的每次迭代的一行,第一列包含迭代次数,第二列是理论值(基于 exp(x) ),第三个是总和,第四个是理论值和迭代值的差。
我目前的代码如下。就目前而言,我的逻辑有一些漏洞,因为代码构建和运行,但输出不正确。如果我要解决我的问题,我认为我的求和没有从正确的位置 (1) 开始,并且前两项是错误的 (1+x+(x^2/2!)+(x ^3/3!)...等)。
我应该使用什么逻辑与我拥有什么逻辑?谢谢。
//cs 1325
// Dean Davis
// Dr. Paulk
// series convergence homework.
#include <stdio.h>
#include <float.h> // need it for FLT_EPSILON
#include <math.h>
unsigned long factorial(int); // function will calculate the factorial
int main()
{
int n = 0;
unsigned long fact; // this variable will hold the factorial value
float x; // this will be the value read in from the user
double theoval; // this will hold the theoretical value of e^x
double holder; // will hold the value of the nth term
double total = 0; // will accumulate the total summation
double diff; // will hold the sifferential between theoretical value and the summation
puts("Please enter a numerical value greater than zero: "); // request input
scanf_s("%f", &x); // read it in
theoval=exp(x); // calc the theoretical value
printf("# Iter e^x Sum Diff\n");
printf("------- ------- ------- -------\n"); // set up the output
while ((theoval - total) >= FLT_EPSILON) //the loop to continue to sum the summation
{
fact = factorial(n); // calls the factorial function
holder = (pow(x, n)) / fact; // calculates the term n
total = total + holder; // adds to the sum
diff = theoval - total; // calc the diff
printf(" %-9d%-12.6f%-14.6f%-10.8f\n", n, theoval, total, diff); // output it
if ((theoval - total) >= FLT_EPSILON) // if it is smaller, then we don't wan't to increment n
continue;
else
n++;
}
printf("The number of iterations required for convergence is: %d\n", n); // out put this line
}
unsigned long factorial(int n)
{
unsigned long int fact=n;
if (n == 0) // if n is zero, 0!=1
return 1;
else // so long as it is not, then we can calculate it like this
{
n--; // decrement it
for (n; n > 0; n--)
{
fact = fact*n; // multiply the next number by the product of all the preceding terms
}
return fact;
}
}