2012-10-16 17 views
3

我在用C++我自適應梯形規則算法的問題 - 基本上,無論指定的寬容,我得到完全相同的近似。遞歸應該在很大的公差範圍內很早就停止(因爲abs(粗 - 細)將小於3.0 *大公差,並且遞歸的minLevel約爲5)。自適應正交(C++)

然而,這是什麼功能呢運行的最大次數,無論公差的選擇。我在哪裏搞砸了? 編輯:也許有我的幫助函數中的問題?

double trap_rule(double a, double b, double (*f)(double),double tolerance, int count) 
{ 
    double coarse = coarse_helper(a,b, f); //getting the coarse and fine approximations from the helper functions 
    double fine = fine_helper(a,b,f); 

    if ((abs(coarse - fine) <= 3.0*tolerance) && (count >= minLevel)) 
     //return fine if |c-f| <3*tol, (fine is "good") and if count above 
     //required minimum level 
    { 
     return fine; 
    } 
    else if (count >= maxLevel) 
     //maxLevel is the maximum number of recursion we can go through 
    { 
     return fine; 

    } 
    else 
    { 
     //if none of these conditions are satisfied, split [a,b] into [a,c] and [c,b] performing trap_rule 
     //on these intervals -- using recursion to adaptively approach a tolerable |coarse-fine| level 
     //here, (a+b)/2 = c 
     ++count; 
     return (trap_rule(a, (a+b)/2.0, f, tolerance/2.0, count) + trap_rule((a+b)/2.0, b, f, tolerance/2.0, count)); 
    } 
} 


EDIT: Helper and test functions: 

//test function 
double function_1(double a) 
{ 
    return pow(a,2); 
} 

//"true" integral for comparison and tolerances 




//helper functions 

double coarse_helper(double a, double b, double (*f)(double)) 
{ 
    return 0.5*(b - a)*(f(a) + f(b)); //by definition of coarse approx 
} 


double fine_helper(double a, double b, double (*f)(double)) 
{ 
    double c = (a+b)/2.0; 
    return 0.25*(b - a)*(f(a) + 2*f(c) + f(b)); //by definition of fine approx 
} 

double helper(double a, double b, double (*f)(double x), double tol) 
{ 
    return trap_rule(a, b, f, tol, 1); 
} 

這裏還有什麼是在main():

std::cout << "First we approximate the integral of f(x) = x^2 on [0,2]" << std::endl; 

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

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

    true_value1 = analytic_first(a,b); 

    for (int i = 0; i<=8; i++) 
    { 
     result1 [i] = helper(a, b, function_1, tolerance[i]); 
     error1 [i] = fabs(true_value1 - result1 [i]); 
    } 

    std::cout << "(Approximate integral of x^2, tolerance, error)" << std::endl; 
    for (int i = 0; i<=8; i++) 
    { 
     std::cout << "(" << result1 [i] << "," << tolerance[i] << "," << error1[i] << ")" << std::endl; 
    } 
+2

第一眼,我看不出有任何問題。你有沒有在每一步驗證過程和罰款值?如果其中一個不好,你會遇到這個問題。你能提供助手和測試功能和初始值,以便我們可以嘗試嗎? – walrii

+0

確定 - 已編輯。 – Emir

+0

任何想法?我仍然無法弄清楚:/ – Emir

回答

4

我發現,你的建議是什麼發生了完全相反---該算法後僅minLevel步驟終止---和原因是由於您在容差測試中使用了abs而不是fabsabs正在將其參數轉換爲int,因此小於1的任何錯誤都將四捨五入爲零。

隨着地方abs我得到一個非常類似的程序輸出:

(0.333496,0.001,0.00016276) 
(0.333496,0.0001,0.00016276) 
(0.333496,1e-05,0.00016276) 
(0.333496,1e-06,0.00016276) 
(0.333496,1e-07,0.00016276) 
(0.333496,1e-08,0.00016276) 
(0.333496,1e-09,0.00016276) 
(0.333496,1e-10,0.00016276) 
(0.333496,1e-11,0.00016276) 

fabs更換我得到這個:

(0.333496,0.001,0.00016276) 
(0.333374,0.0001,4.06901e-05) 
(0.333336,1e-05,2.54313e-06) 
(0.333334,1e-06,6.35783e-07) 
(0.333333,1e-07,3.97364e-08) 
(0.333333,1e-08,9.93411e-09) 
(0.333333,1e-09,6.20882e-10) 
(0.333333,1e-10,3.88051e-11) 
(0.333333,1e-11,9.7013e-12) 
+1

啊,傻了。謝謝! – Emir