简体   繁体   English

如何修复我的e ^ x近似C程序?

[英]How to fix my e^x approximation C program?

I have written a C program that should compute and print the approximation of e^x for all values up to n. 我编写了一个C程序,它应该计算并打印所有n值的e ^ x近似值。 I am using this equation to implement my program. 我正在使用这个等式来实现我的程序。

f(x, n) = e^x = the sum of i=0 up to n= x^i/x! f(x,n)= e ^ x = i = 0到n = x ^ i / x之和! = x^0/0! = x ^ 0/0! + x^1/1! + x ^ 1/1! + x^2/2! + x ^ 2/2! +....+ x^n/n! + .... + x ^ n / n!

This is my code: 这是我的代码:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

double factorial(int n){
        double fac =1;
        int i;
        for(i =1; i <= n; i++){ 
                fac *=n;
        }
        return fac;
}
double exponent(double x, int n){
        double sum, i;
        for(i = 0; i <=n; i++){
                sum +=  (pow(x, i)/ factorial(i));
        } 
        return sum;
}

int main(int argc, char *argv[]){
        int n = atoi(argv[1]);
        double x = atof(argv[2]);
        printf("\ti\tApproximantion\n");
        printf("-------------------------------------\n");
        int i;
        for(i =0; i <=n; i++){
               printf("\t%d\t%f\n", i, exponent(x,i));
        }
        printf("Exact Value =\t%12f\n", exp(x));
        return 0;
}//main

I am using iteration instead of recursion. 我使用迭代而不是递归。 My current output using ./aprroximation 10 2.2 is: 我使用./aprroximation 10 2.2当前输出是:

        i       Approximation
-------------------------------------
        0       1.000000
        1       3.200000
        2       4.410000
        3       4.804370
        4       7.895877
        5       8.912368
        6       9.914798
        7       10.915101
        8       11.915134
        9       12.915137
        10      13.915137
Exact Value =       9.025013

the output should be: (ignore the space/tabbing) 输出应该是:(忽略空格/标签)

   i Approximation
--------------------------------
   0   1.0000000000
   1   3.2000000000
   2   5.6200000000
   3   7.3946666667
   4   8.3707333333
   5   8.8002026667
   6   8.9576747556
   7   9.0071659835
   8   9.0207760712
   9   9.0241029815
   10   9.0248349018
Exact Value = 9.0250134994

I can't seem to find the problem in my code. 我似乎无法在我的代码中找到问题。 I have looked at some pseudo-code for factorial and power functions and nothing stands out. 我已经看了一些用于阶乘和幂函数的伪代码,没有什么突出的。 My results continue to be wrong. 我的结果仍然是错误的。 Any thoughts? 有什么想法吗?

There are two problems: 有两个问题:

  1. In the exponent function you don't initialize sum exponent函数中,您不初始化sum
  2. Your factorial function is wrong. 你的factorial功能是错误的。

If you had tested the factorial function separately you would have found out easily by yourself at least that problem. 如果您已经单独测试了factorial函数,那么您至少可以自己找到这个问题。

I see, you didn't initialize the variable sum inside the exponent() function. 我明白了,你没有在exponent()函数中初始化变量sum Initialize it to zero, otherwise the exponent() function looks ok. 将其初始化为零,否则exponent()函数看起来没问题。

double exponent(double x, int n){
    double sum = 0, i;
    for(i = 0; i <=n; i++){
        sum += pow(x, i)/ factorial(i);
    } 
    return sum;
}

Update 更新

I spot another problem in the factorial() function. 我发现了factorial()函数中的另一个问题。 Just think, whether the statement inside the for loop is correct! 试想一下, for循环中的语句是否正确!

for(i=1; i<=n; i++){ 
    fac *= n;
}

What you are computing here is not n! 你在这里计算的不是n! , it is n^n . ,它是n^n You should do the following. 您应该执行以下操作。

for(i=1; i<=n; i++){ 
    fac *= i;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM