如何使用for循环来计算C中的中点规则Riemann Sum

时间:2018-10-07 21:39:34

标签: c

对于一个作业,我应该创建一个函数,该函数使用四分之一圆的中点规则Riemann总和来计算PI。我无法获得正确的输出,因为我认为自己的逻辑有些错误。这是该函数的代码:

RenameJavaElementDescriptor

这是主要功能:#include <math.h> double circle_pi(int rectangles) { int radius = 2; int i; int width = (double)radius / (double)rectangles; double rect_area = 0; for(i = 1; i<= rectangles; i++) { int midpoint = (double)width / 2; int height = sqrt((radius * radius) - (midpoint * midpoint)); midpoint = midpoint + width; rect_area = (double)width * (double)height; } return rect_area; }


这是预期的输出:

#include <stdio.h> /* scanf, printf */

double circle_pi(int rectangles);  /* Calculates PI using a quarter circle */
double leibniz_pi(int iterations); /* Calculates PI using a series         */

int main(void)
{
  int i; /* loop counter */

    /* Print out table header */
  printf("Approximations for pi\n");
  printf("Iterations      Circle Method   Leibniz Method\n");
  printf("----------------------------------------------\n");

    /* Print out values for each set of numbers */
  for (i = 1; i <= 1000000; i *= 10)
  {
      /* Calculate PI with both methods */
    double pi_circle = circle_pi(i);
    double pi_leibniz = leibniz_pi(i);

      /* Print the results of the calculations */
    printf("%10i%20.12f%16.12f\n", i, pi_circle, pi_leibniz);
  }

  return 0; /* Return success to the OS */
}

她是我从代码中得到的输出:

Approximations for pi
Iterations      Circle Method   Leibniz Method
----------------------------------------------
     1      3.464101615138  4.000000000000
    10      3.152411433262  3.041839618929
   100      3.141936857900  3.131592903559
  1000      3.141603544913  3.140592653840
 10000      3.141592998025  3.141492653590
100000      3.141592664482  3.141582653590
1000000      3.141592653934  3.141591653590

(我尚未处理其他功能,这就是为什么它为空的原因)

如果问题很明显,我真的很抱歉,但是我是C语言的新手,并且很难习惯它。任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:0)

正如Iguananaut所述:double中的所有变量都应使用circle_pi数据类型,循环的迭代器除外。 您实际的错误是忘了最后总结所有内容

double circle_pi(int rectangles)
{
  double radius = 2.0;
  int i;
  double width = radius / (double)rectangles;
  double rect_area = 0.0;
  double midpoint, height;
  // set startvalue of mipoint
  midpoint = width / 2.0;

  for(i = 1; i <= rectangles; i++)
  {
    height = sqrt((radius * radius) - (midpoint * midpoint));
    midpoint = midpoint + width;
    rect_area = rect_area + width * height;
   }
  return rect_area;
}

这将产生预期的输出:

Approximations for pi
Iterations      Circle Method   Leibniz Method
----------------------------------------------
         1      3.464101615138
        10      3.152411433262
       100      3.141936857900
      1000      3.141603544913
     10000      3.141592998025
    100000      3.141592664486
   1000000      3.141592653923

答案 1 :(得分:0)

请参阅上面的评论,但例如,我会像这样编写您的函数(注意:我尚未测试过):

#define RADIUS 2.0
#define RADIUS2 4.0

double circle_pi(int rectangles)
{
  int idx;
  double width = RADIUS / rectangles;
  double x = width / 2;
  double total_area = 0;

  for(idx = 0; idx < rectangles; idx++)
  {
    height = sqrt(RADIUS2 - (x * x));
    total_area += width * height;
    x += width;
  }
  return total_area;
}

您还可以使用其他一些技巧来简化操作。例如,您可以意识到“宽度”始终是相同的,并将其排除在外,从而减少了乘法次数。