我已经实现了 regula falsi 方法。我正在尝试对其进行修改,使其成为割线方法。我读过的一个 pdf 文件提到它基本上是一样的,只是一个变化。未来对我的“m”值的猜测应该有一个稍微不同的公式,而不是:
m = a - f(a) * ( (b-a)/( f(b)-f(a) ) );
它应该是:
m = a - f(a) * ( (m-a)/( f(m)-f(a) ) );
但不幸的是它不起作用(它永远找不到根)。我应该如何解决才能将其纳入割线方法?
源代码如下:
#include <stdio.h>
#include <math.h>
void
secant(double a, double b, double e, double (*f)(double), int maxiter ) {
double m, fm, fa, fb;
int i;
fa=(*f)(a);
fb=(*f)(b);
m = a - fa * ( (b-a)/( fb - fa ) );
fm=(*f)(m);
for(i=0; i<maxiter; i++) {
if ( fabs(fm) <= e ) {
printf("f(%f) = %f\n", m, fm);
return;
} else if ((fa*fm) < 0) {
b=m;
fb=fm;
} else {
a=m;
fa=fm;
}
// the guess below works for regula falsi method:
// m = a - fa * ( (b-a)/(fb - fa));
//this was supposed to be the change to turn this into the secant method
m = a - fa * ( (m-a)/(fm - fa) );
fm=(*f)(m);
}
}
int main(){
secant(1,4,0.0001,sin,500);
return 0;
}
提前致谢
编辑:好吧,在玩了笔和纸之后,我终于明白了,这并不是我最初认为的简单改变:
void secant(double a, double b, double e, double (*f)(double), int maxiter ) {
double m, fm, fa, fb;
int i;
fa=(*f)(a);
fb=(*f)(b);
for(i=0; i<maxiter; i++) {
m = a - fa * ( (b-a)/(fb - fa) );
fm=(*f)(m);
if ( fabs(fm) <= e ) {
printf("f(%f)=%f, iter: %d\n", m,fm,i);
return;
}
a=b;
b=m;
fa=fb;
fb=fm;
}
}