2016-06-28 306 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值像下面的循环,你计划,使小变化您终点,扩大或缩小或翻译您的间隔,当他们满足初始条件时,程序将再次运行