2016-06-28 78 views
3

大家好我有問題讓我的程序在GSL根目錄下工作。我正在嘗試找到解決方案。我正在尋找64行數據的解決方案,但在某些特定行中,程序無法繼續,可能是因爲沒有好的解決方案。但我希望程序在沒有找到解決方案時跳過線路。但我的程序有時會停止並出現以下消息: gsl:brent.c:74:錯誤:終結點不跨越y = 0 passou1passou2passou3默認調用GSL錯誤處理程序。 中止陷阱:6GSL中的錯誤 - 根目錄查找

所以,我做了一些打印以檢查正是我的程序停止,我發現,在gsl_root_fsolver_set(S,&樓x_lo,x_hi),但我怎麼沒發現打印這個值或者這個函數給我的東西。

我的程序在這裏,謝謝大家!

#include <stdio.h> 
#include <math.h> 
#include <gsl/gsl_errno.h> 
#include <gsl/gsl_math.h> 
#include <gsl/gsl_complex_math.h> 
#include <gsl/gsl_roots.h> 
#include "demo_fn.h" 
#include "demo_fn.c" 

int 
main (void) 
{ 

double NT, c; 
c = 64.0/2.0; 

char url[]="charm.txt"; 
double corr, core, a[64][3]; 
int nt, i = 0; 
FILE *arq; 
arq = fopen(url, "r"); 
if(arq == NULL) 
    printf("Erro, nao foi possivel abrir o arquivo\n"); 
else 
    while((fscanf(arq,"%lf %lf %i\n", &corr, &core, &nt))!=EOF) { 
     a[i][0]=corr; 
     a[i][1]=core; 
     a[i][2]=nt; 
     i++; 
     //printf("%lf, %lf, %i\n",corr, core, nt); 
    } 

fclose(arq); 




for (i= 0; i < 64; i++) 
{ 
    int status; 
    int iter = 0, max_iter = 200; 
    const gsl_root_fsolver_type *T; 
    gsl_root_fsolver *s; 
    double r = 0, r_expected = 4.0; 
    double x_lo = 0.0001, x_hi = 4.0; 
    double ratio1, ratio2; 

    ratio1 = a[i][0]/a[i+1][0]; 
    ratio2 = a[i+1][0]/a[i+2][0]; 
    printf ("ratio1: %lf, ratio2: %lf", ratio1, ratio2); 
    printf ("\n"); 

// if (ratio1*ratio2 > 0) 
//  { 

    printf("C(n_t) : %.15lf -- loop index : %i ----- ratio: %lf \n", a[i][0],i, ratio1); 

    gsl_function F; 
    struct quadratic_params params = {a[i][0], i, c, i+1, a[i+1][0]}; 
    F.function = &quadratic; 
      printf ("passou1"); 
    F.params = &params; 
    T = gsl_root_fsolver_brent; 
      printf ("passou2"); 
    //T = gsl_root_fsolver_bisection; 
    s = gsl_root_fsolver_alloc (T); 
     printf ("passou3"); 
    gsl_root_fsolver_set (s, &F, x_lo, x_hi); 
     printf ("passou4"); 
    printf ("using %s method\n", gsl_root_fsolver_name (s)); 
    printf ("%5s [%9s, %9s] %9s %10s %9s\n", "iter", "lower", "upper", "root", "err", "err(est)"); 


    do 
    { 
     iter++; 
     status = gsl_root_fsolver_iterate (s); 
     r = gsl_root_fsolver_root (s); 
     x_lo = gsl_root_fsolver_x_lower (s); 
     x_hi = gsl_root_fsolver_x_upper (s); 
     status = gsl_root_test_interval (x_lo, x_hi,0, 0.001); 
     if (status == GSL_SUCCESS) 
     { 
      printf ("Converged:\n"); 
     } 
     printf ("%5d [%.7lf, %.7lf] %.7lf %+.7lf %.7lf\n", iter, x_lo, x_hi, r, r - r_expected, x_hi - x_lo); 


    } 
    while (status == GSL_CONTINUE && iter < max_iter); 

    gsl_root_fsolver_free (s); 

//  } 

    printf("\n"); 
} 



return 0; 

} 
+0

'#include「demo_fn.c」'是非常規的,至少可以說。 – EOF

+0

這是因爲我正在寫這部分是我的功能,並演示演示.... – Gabriela

回答

2

Gabriela。輸出已經告訴你爲什麼你的程序是錯誤的。 端點不跨越y = 0。

The root bracketing algorithms described in this section require an initial interval which is guaranteed to contain a root—if a and b are the endpoints of the interval then f(a) must differ in sign from f(b).

以上是從manual of gsl,所以如果端點具有相同的符號,程序會停止,並告訴你這個錯誤。

你有沒有試過gsl中的錯誤處理程序。在手冊的第3章中,他們提供了一個稱爲gsl_set_error_handler_off()的函數,如果您將此函數放置在gsl_root_fsolver_set(s,&F, x_lo, x_hi)之前,並且可以將此函數分配給int類型變量,比如status,如我們可以從(gsl_root_fsolver_set(s,&F, x_lo, x_hi))是int function,那麼如果你輸出status的值,並用gsl_errno.h文件檢查它,你就會知道這個值是什麼意思。

This gsl_set_error_handler_off()可以解除墮胎的執行。

爲你的代碼,你應該做到以下幾點:

  1. 添加#include <gsl/gsl_errno.h>
  2. 添加gsl_set_error_handler_off()status=gsl_root_fsolver_set(s,&F, x_lo, x_hi)
  3. 使用status值像下面的循環,你計劃,使小變化您終點,擴大或縮小或翻譯您的間隔,當他們滿足初始條件時,程序將再次運行
相關問題