首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >输出错误"gsl: bisection.c:55: ERROR: function value is not finite“与bisection.c文件中的第55行不匹配

输出错误"gsl: bisection.c:55: ERROR: function value is not finite“与bisection.c文件中的第55行不匹配
EN

Stack Overflow用户
提问于 2021-08-02 20:54:31
回答 1查看 60关注 0票数 1

我想使用GSL二分例程来寻找函数f(x)=1/(2sin(x)^2)+log(sin(x))-k,k在区间0;Pi/2上的根。

代码语言:javascript
复制
     int main(void *params){
  
     struct func_params *part= (struct func_params*)params;
    
     int status;
     int iter = 0, max_iter = 10;
     const gsl_root_fsolver_type *R;
     gsl_root_fsolver *s;
     double x_lo = 1e-4, x_hi = M_PI/2.;

     gsl_function F;
     F.function = &my_func;
     F.params = &params;
     R = gsl_root_fsolver_bisection;
     s = gsl_root_fsolver_alloc(R);
     gsl_root_fsolver_set(s,&F,x_lo,x_hi);

     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)");

    return(0);

}

这段代码返回了一个gsl: bisection.c:55: ERROR: function value is not finite Default GSL error handler invoked.,问题是,当我查看bisection.c文件时,第55行与前面的error...Any不对应,这是怎么回事?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-03 05:07:16

您必须提供一个间隔,以便函数仅跨越y = 0一次。

例如,这里的[0.1, 2.0]

代码语言:javascript
复制
#include <stdio.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_roots.h>

double my_func(double x,  void* unused)
{
    double k = 1;
    return 1 / (2 * pow(sin(x), 2)) + log(sin(x)) - k;
}

int main()
{
    gsl_function F;
    F.function = &my_func;
    F.params = 0;

    gsl_root_fsolver *s = gsl_root_fsolver_alloc(gsl_root_fsolver_bisection);

    double x_lo = +0.1, x_hi = +2.0;
    gsl_root_fsolver_set(s, &F, x_lo, x_hi);

    for(int i = 0; i < 1000; i++) {

        int status = gsl_root_fsolver_iterate(s);

        double left_int  = gsl_root_fsolver_x_lower(s);
        double right_int = gsl_root_fsolver_x_upper(s);

        printf("iteration %03d: [%.04lf, %.04lf]\n", i, left_int, right_int);

        status = gsl_root_test_interval(left_int, right_int, 1.0e-5, 1.0e-15);
        if(status != GSL_CONTINUE) {

            printf("status: %s\n", gsl_strerror(status));
            printf("\nRoot interval = [%.010lf, %.010lf]\n", left_int, right_int);
            break;
        }
    }
}

输出:

代码语言:javascript
复制
$ gcc main.c -lgsl -lm && ./a.out
iteration 000: [0.1000, 1.0500]
iteration 001: [0.5750, 1.0500]
iteration 002: [0.5750, 0.8125]
iteration 003: [0.5750, 0.6938]
iteration 004: [0.5750, 0.6344]
iteration 005: [0.5750, 0.6047]
iteration 006: [0.5898, 0.6047]
iteration 007: [0.5973, 0.6047]
iteration 008: [0.5973, 0.6010]
iteration 009: [0.5973, 0.5991]
iteration 010: [0.5982, 0.5991]
iteration 011: [0.5987, 0.5991]
iteration 012: [0.5989, 0.5991]
iteration 013: [0.5989, 0.5990]
iteration 014: [0.5989, 0.5990]
iteration 015: [0.5989, 0.5990]
iteration 016: [0.5989, 0.5990]
iteration 017: [0.5989, 0.5990]
status: success

Root interval = [0.5989471436, 0.5989543915]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68627835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档