这是我到目前为止的代码,这有点混乱,因为我仍在试图弄清楚如何设置它,但我不知道如何获得输出。该代码应该采用指数的泰勒级数多项式,并检查获得近似值所需的迭代量。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*Prototype for functions used*/
double factorial (int);
int main()
{
double input = 0;
double exp_val;
double delta = 1;
int f =0;
int n = 0;
double taylor;
int total;
printf("Plese enter the exponent to check for convergence:\n");
scanf("%lf", &input);
exp_val = exp(input);
printf(" # Iter e^X Sum Diff\n");
printf("---- ------ ------- ----- --------");
while(delta > 0.00001)
{
f = factorial(n);
taylor = ((pow(input,n))/ f);
delta = (exp_val - taylor);
printf("%d %f %f %f/n", (n+1), exp_val, taylor, delta);
n++;
}
system("pause");
}
double factorial (int n)
{
int r = 0;
int sum = 1;
int total = 0;
if (n == 0)
return total =1;
else
{
for(r; r<n; r++)
{
sum = sum * r;
total = sum + 1;
}
return total;
}
}