【问题标题】:Exponential Taylor Series指数泰勒级数
【发布时间】:2014-04-03 02:08:42
【问题描述】:

这是我到目前为止的代码,有点乱,因为我仍在试图弄清楚如何设置它,但我不知道如何获得输出。该代码应该采用指数的泰勒级数多项式,并检查获得近似值所需的迭代量。

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

}

【问题讨论】:

  • 大概你追求的价值只是n?
  • 另外,您计算泰勒和的算法非常非常令人反感。你为什么一次又一次地做这些工作?再懒一点!保留运行术语,并考虑一个术语与前一个术语有何不同。
  • 当你知道n-1的阶乘时如何计算n的阶乘(只需乘以n!当你有x^(n-1)时如何计算x^n的值- 多个x!然后保持数字可管理,以免溢出。它们就像公共汽车,喜欢在一起
  • 你的factorial函数很奇怪...sum最初设置为1,但前提是n != 0,它会在第一个乘以0循环,并在其余时间保持为0;这意味着变量total 将始终具有相同的值0 + 1 = 1,如果不是初始值0。长话短说,该函数的返回值将始终为 1.0

标签: c factorial taylor-series


【解决方案1】:

在这里,我已经修复了它,没有改变你的方法,除了我真正不得不做的部分。在编写代码之前我们必须澄清的一件事是泰勒多项式是如何产生的。它不是第一项加上第n项,而是从第一项到第项的所有项的总和。因此,您肯定必须将taylor 变量增加当前的nth term,而不是其他方式。

这是代码,里面有简短的cmets作为解释:

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

/*Prototype for functions used*/
unsigned long long factorial( int );    // <-- made it return unsigned long long

int main( )
{
    double input = 0;
    double exp_val;
    double delta = 1;
    unsigned long long f = 0;   // <-- changed its type
    int n = 0;
    double taylor = 0;  // <-- initialized with 0
    printf( "Plese enter the exponent to check for convergence:\n" );
    scanf( "%lf", &input );
    exp_val = exp( input );
    printf( " #          e^X            Sum           Diff\n" );        // <-- made some cosmetic changes
    printf( "---      ---------      ---------      ---------\n" );     // <-- added \n

    while ( delta > 0.00001 )
    {
        f = factorial( n );
        taylor += ( ( pow( input, n ) ) / f );  // += instead of =
        delta = ( exp_val - taylor );
        printf( "%2d    %12f   %12f   %12f\n", ( n + 1 ), exp_val, taylor, delta ); // <-- replaced / with \ before the n
        n++;                                                                        // and made some edits to make it look better
    }
    system( "pause" );
    return 0;           // <-- better add this
}

unsigned long long factorial( int n )   // <-- made it return unsigned long long
{
    int r = 0;
    unsigned long long sum = 1; // <-- changed its type
    if ( n == 0 )
        return sum; // <-- this
    else
    {
        for ( r; r<n; r++ )
        {
            sum *= r + 1;   // <-- changed this
        }

        return sum; // <-- and this
    }
}

您必须记住,您不能为其输入过高的值。任何高于 input == 4 的东西都会破坏它,因为,你看,即使是 4,它也可以将错误 delta 降低到阈值以下,只有在第 19 个周期。当n 达到21 时,由于pow( 5, 21 ) / factorial( 21 ) 的计算不准确,该程序似乎以n == 5 失败:

0.000034    // the result this programme finds
0.0000093331055943447405008542892329719 // the result Calculator finds

所以,是的...如果您希望该程序使用更大的input 值,您将需要一种更好的方法。正如其他人所说,不从头开始计算第 n 项 并从第 (n - 1) 项 计算它可能会有所帮助,直到 input 值更大一些。

【讨论】:

  • 谢谢你,现在我正在看它,这很有意义。
【解决方案2】:

几个问题:

  1. int r = 0; ... for(r; r&lt;n; r++) 更改为int r; ... for(r=1; r&lt;=n; r++)int r = 1; ... for(; r&lt;=n; r++)

  2. printf("%d %f %f %f/n"更改为printf("%d %f %f %f\n"添加\n

  3. "... --------" 更改为"... --------\n"

  4. delta = (exp_val - taylor); 更改为delta = fabs(exp_val - taylor);

  5. 改成double taylor = 0.0;初始化吧。

  6. 更改为taylor += ((pow(input,n))/ f); 注意:+=

  7. 次要:“请”而不是“请”。

  8. 次要:丢弃int total;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2017-08-21
    • 2011-11-04
    相关资源
    最近更新 更多