0

Q 的第二部分:然后使用括号和二分法求解 y 的 (x^2)(e^(-x^2))dx=0.1 的 0 和 y 之间的积分。

这是我到目前为止所做的:

    #include <stdio.h>
#include <math.h>
double f(double x, double y);

int main(void) {
    int i, steps;
    double a, b, y, h, m, lower, upper, x, simp, val;
    /*
     * Integrate (x^2)(e^(-x^2)) from 0 to y
     */
    steps = 20000;
    a = 0;
    b = y;
    h= (b-a)/steps;
    /*
     * now apply Simpson's rule. Note that the steps should be even.
     */
    simp = -f(a, y);
    x = a;
    for (i =0; i < steps; i += 2) {
        simp += 2.0*f(x,y)+4.0*f(x+h, y);
        x += 2*h;
    }
    simp += f(b, y);
    simp *= h/3.0;
    /*
     * print out the answer
     */
    printf("The integral from 0 to y with respect to x by Simpson's Rule is %f\n", simp);
    /*
     * Now we need to bracket and bisect to find y
     */
    lower = 0;
    /*
     * Lower bound is from turning point
     */
    upper = 100;
    /*
     *Upper value given.
     */
    while (upper - lower > 10E-10){
        m = (lower + upper)/2;
        val = f(m, y);
        if (val >=0)
            upper = m;
        if (val <=0)
            lower = m;
    }
    m = (lower + upper)/2;
    printf("The value for y is: %lf\n", m);
    return 0; 
}
double f(double x, double y) {
    return pow(x,2)*exp(pow(-x,2))-0.1;
}

Output: The integral from 0 to y with respect to x by Simpson's Rule is -0.000000
The value for y is: 0.302120

它运行但不完全符合我的需要。一旦我使用 0 和 y 作为限制,我需要能够继续使用积分。我不能这样做。然后继续求解 y。它给了我一个 y 的值,但与我使用在线计算器求解时得到的值不同。此外,即使我将要积分的方程更改为 x^2,积分的输出也为零。任何人都可以帮助用尽可能简单的术语解释吗?

4

0 回答 0