rtsec.c
来自「可以用于科学计算的库」· C语言 代码 · 共 35 行
C
35 行
#include <math.h>
#define MAXIT 30
float rtsec(float (*func)(float), float x1, float x2, float xacc)
{
void nrerror(char error_text[]);
int j;
float fl,f,dx,swap,xl,rts;
fl=(*func)(x1);
f=(*func)(x2);
if (fabs(fl) < fabs(f)) {
rts=x1;
xl=x2;
swap=fl;
fl=f;
f=swap;
} else {
xl=x1;
rts=x2;
}
for (j=1;j<=MAXIT;j++) {
dx=(xl-rts)*f/(f-fl);
xl=rts;
fl=f;
rts += dx;
f=(*func)(rts);
if (fabs(dx) < xacc || f == 0.0) return rts;
}
nrerror("Maximum number of iterations exceeded in rtsec");
return 0.0;
}
#undef MAXIT
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?