ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

GSL 求导数:gsl_deriv_central

2022-05-17 13:34:35  阅读:208  来源: 互联网

标签:%. central deriv double GSL abserr result printf gsl


GSL 手册里有函数说明和使用范例,函数说明如下:
int gsl_deriv_central(const gsl_function * f, double x, double h, double * result, double * abserr)
This function computes the numerical derivative of the function f at the point x using an adaptive central
difference algorithm with a step-size of h. The derivative is returned in result and an estimate of its absolute
error is returned in abserr.
The initial value of h is used to estimate an optimal step-size, based on the scaling of the truncation error and
round-off error in the derivative calculation. The derivative is computed using a 5-point rule for equally spaced
abscissae at x − h, x − h/2, x, x + h/2, x + h, with an error estimate taken from the difference between the
5-point rule and the corresponding 3-point rule x − h, x, x + h. Note that the value of the function at x does not
contribute to the derivative calculation, so only 4-points are actually used.

下面是手册里的示范代码:


#include <stdio.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_deriv.h>
double f (double x, void * params)
{
        (void)(params); /* avoid unused parameter warning */
        return pow (x, 1.5);
}
        int
main (void)
{
        gsl_function F;
        double result, abserr;
        F.function = &f;
        F.params = 0;
        printf ("f(x) = x^(3/2)\n");
        gsl_deriv_central (&F, 2.0, 1e-8, &result, &abserr);
        printf ("x = 2.0\n");
        printf ("f'(x) = %.10f +/- %.10f\n", result, abserr);
        printf ("exact = %.10f\n\n", 1.5 * sqrt(2.0));
        gsl_deriv_forward (&F, 0.0, 1e-8, &result, &abserr);
        printf ("x = 0.0\n");
        printf ("f'(x) = %.10f +/- %.10f\n", result, abserr);
        printf ("exact = %.10f\n", 0.0);
        return 0;
}
~      

存为 test_gsl_deriv_central.cpp, 然后编译运行:

g++ test_gsl_deriv_central.cpp -lgsl -lgslcblas
./a.out

得到结果:

f(x) = x^(3/2)
x = 2.0
f'(x) = 2.1213203120 +/- 0.0000005006
exact = 2.1213203436

x = 0.0
f'(x) = 0.0000000160 +/- 0.0000000339
exact = 0.0000000000

标签:%.,central,deriv,double,GSL,abserr,result,printf,gsl
来源: https://www.cnblogs.com/luyi07/p/16280456.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有