簡體   English   中英

遞歸函數。 使用牛頓-萊布尼茨公式計算定積分。 C++

[英]Recursive functions. Calculate the definite integral using the Newton-Leibniz formula. C++

給定:兩個實數a,b(a < b)和一個正數integer

名詞使用 Newton-Leibniz 公式計算定積分: a(top) ∫ b(bottom) (f(x,n)dx = F(b,n)-F(a,n)其中 F(x,n) 是一階(反導數、逆導數、原語 function、原語積分) function f(x,n): F(x, n) = integral(f(x,n)dx)

實現F(x,n)的計算有兩種方式:

  1. 作為遞歸。
  2. 作為非遞歸 function。

這就是需要解決的問題。 單擊鏈接打開照片。

F(x,n) = ∫(ctg^n*xdx)

我使用pow function解決了這個問題。我的老師讓我在不使用pow function的情況下解決這個問題。幫助我在不使用pow function的情況下解決這個問題。

遞歸解決方案

// Recursion
double F(double x, int n)
{
    if (n == 0)
        return x;
    else if (n == 1)
        return log(fabs(sin(x))); 
    else
        return -pow(cos(x) / sin(x), n - 1) / (n - 1) - F(x, n - 2);
}

用循環解決

// For loop
double F_for(double x, int n)
{
    if (n == 0)
        return x;
    else if (n == 1)
        return log(fabs(sin(x))); 
    else
    {
        double F1 = x;
        double F2 = log(fabs(sin(x)));
        double res = 0.0;
        for (int i = 2; i <= n; i++)
        {
            res = -pow(cos(x) / sin(x), i - 1) / (i - 1) - F1;
            F1 = F2;
            F2 = res;
        }
        return res;
    }
}

主 function

int main()
{
    double a, b;

    cout << "Enter a: ";
    cin >> a;

    cout << "Enter b: ";
    cin >> b;

    int n;

    cout << "Enter n: ";
    cin >> n;

    cout << "Recursion: " << F(b, n) - F(a, n) << std::endl;

    cout << "A loop: " << F_for(b, n) - F_for(a, n);

    return 0;
}

PS請幫助我解決它而不使用 pow function。 我已經寫了我的 function “my_power”。 他說:“你也不能用。”

double power(double x, int y)
{
    double temp;
    if (y == 0)
        return 1;
    temp = power(x, y / 2);
    if ((y % 2) == 0) {
        return temp * temp;
    }
    else {
        if (y > 0)
            return x * temp * temp;
        else
            return (temp * temp) / x;
    }
}

I also tested this function


void test_my_power()
{
    cout << "pow" << pow(2, -3) << "0" << endl;
    cout << "power" << power(2, -3) << "0" << endl;
    cout << endl;

    cout << "pow" << pow(0, -3) << "0" << endl;
    cout << "power" << power(0, -3) << "0" << endl;
    cout << endl;

    cout << "pow" << pow(0, 1) << "0" << endl;
    cout << "power" << power(0, 1) << "0" << endl;
    cout << endl;

    cout << "pow" << pow(0, -1) << "0" << endl;
    cout << "power" << power(0, -1) << "0" << endl;
    cout << endl;

    cout << "pow" << pow(8, 4) << "0" << endl;
    cout << "power" << power(8, 4) << "0" << endl;
    cout << endl;

    cout << "pow" << pow(11, 3) << "0" << endl;
    cout << "power" << power(11, 3) << "0" << endl;
    cout << endl;

    cout << "pow" << pow(5, 6) << "0" << endl;
    cout << "power" << power(5, 6) << "0" << endl;
    cout << endl;
}

您的老師可能希望您在迭代循環時即時計算pow

這段代碼:

const auto t = cos(x) / sin(x);

for(int i = 2; i <= n; ++i) {
   const auto val = pow(t, i - 1);
   ...
}

相當於:

auto power = t;

for(int i = 2; i <= n; ++i) {
   const auto val = power;
   ...
   power *= t;
}

並且不使用pow

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM