【问题标题】:Trapezoidal Riemann Sum in CC中的梯形黎曼和
【发布时间】:2018-02-21 12:30:30
【问题描述】:

我一直在尝试用黎曼和来逼近 C 中的积分。在下面的代码中,我试图通过梯形方式和矩形方式来逼近(显然,梯形方式应该更好)。

我尝试在纸上为此制定一个算法,结果如下: 注意:N 是矩形(或梯形)的数量,dx 是使用 a、b 和 N (dx = (b-a)/N) 计算的。 f(x) = x^2

矩形法:

<a href="http://www.codecogs.com/eqnedit.php?latex=\int_a^b&space;x^2&space;dx&space;\approx&space;\sum_{i=1}^N&space;f(a&space;+&space;(i-1)dx)dx" target="_blank"><img src="http://latex.codecogs.com/png.latex?\int_a^b&space;x^2&space;dx&space;\approx&space;\sum_{i=1}^N&space;f(a&space;+&space;(i-1)dx)dx" title="\int_a^b x^2 dx \approx \sum_{i=1}^N f(a + (i-1)dx)dx" /></a>

梯形法:

<a href="http://www.codecogs.com/eqnedit.php?latex=\int_a^b&space;x^2&space;dx&space;\approx&space;\sum_{i=1}^N&space;[f(a&space;+&space;(i-1)dx)&space;+&space;f(a&space;+&space;i\cdot&space;dx)]dx" target="_blank"><img src="http://latex.codecogs.com/png.latex?\int_a^b&space;x^2&space;dx&space;\approx&space;\sum_{i=1}^N&space;[f(a&space;+&space;(i-1)dx)&space;+&space;f(a&space;+&space;i\cdot&space;dx)]dx" title="\int_a^b x^2 dx \approx \sum_{i=1}^N [f(a + (i-1)dx) + f(a + i\cdot dx)]dx" /></a>

代码(在下面的代码中,f(x)=x^2 和 F(x) 是它的反导数 (x^3/3):

int main() {
    int no_of_rects;
    double  a, b;

    printf("Number of subdivisions = ");
    scanf("%d", &no_of_rects);

    printf("a = ");
    scanf("%lf", &a);

    printf("b = ");
    scanf("%lf", &b);

    double dx = (b-a)/no_of_rects;

    double rectangular_riemann_sum = 0;

    int i;
    for (i=1;i<=no_of_rects;i++) {
        rectangular_riemann_sum +=  (f(a + (i-1)*dx)*dx);
    }

    double trapezoidal_riemann_sum = 0;

    int j;
    for (j=1;j<=no_of_rects;j++) {
        trapezoidal_riemann_sum += (1/2)*(dx)*(f(a + (j-1)*dx) + f(a + j*dx));
        printf("trapezoidal_riemann_sum: %lf\n", trapezoidal_riemann_sum);
    }

    double exact_integral = F(b) - F(a);
    double rect_error = exact_integral - rectangular_riemann_sum;
    double trap_error = exact_integral - trapezoidal_riemann_sum;

    printf("\n\nExact Integral: %lf", exact_integral);
    printf("\nRectangular Riemann Sum: %lf", rectangular_riemann_sum);
    printf("\nTrapezoidal Riemann Sum: %lf", trapezoidal_riemann_sum);
    printf("\n\nRectangular Error: %lf", rect_error);
    printf("\nTrapezoidal Error: %lf\n", trap_error);

    return 0;
}

地点:

double f(double x) {
    return x*x;
}

double F(double x) {
    return x*x*x/3;
}

我已经包含了数学和 stdio 头文件。发生的事情是矩形的黎曼和是可以的,但是梯形的黎曼和由于某种原因总是为0。

有什么问题?我的公式中有什么吗?还是我的代码? (顺便说一句,我是 C 的新手)

提前致谢。

【问题讨论】:

  • 1/2 始终为零。

标签: c integral calculus


【解决方案1】:

在此声明中:

trapezoidal_riemann_sum += (1/2)*(dx)*(f(a + (j-1)*dx) + f(a + j*dx));

1/2 == 零,所以整个语句为零。至少将分子或分母更改为双精度形式以获得双精度值。即1/2.01.0/21.0/2.0 都可以工作。

【讨论】:

  • 其实我是在主要使用 Python 之后才开始使用 C 语言的,其中 1/2 会返回一个浮点值。不管怎样,谢谢! (有点讽刺的是,因为我没有 15 个代表,所以我不能对你的评论投赞成票。)
  • @lil'mathematician - 我在 Python 中发现了同样的事情。它的类型是动态的,根据上下文,C 绝对不是。谢谢。无需担心向上点击。 (但多 2 分会让你到达那里:))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-26
  • 1970-01-01
相关资源
最近更新 更多