半間距法
最近老師出了一個題目~叫半間距法~可以求任何函數的根(近似值)~~請問有高手會用c寫嗎?請大家幫幫忙~~~感謝
double HalfX(double low, double hight, double det, double(*fun)( double ) ) { double result; double lowR; double hightR; double funR; lowR = (*fun)(low); hightR = (*fun)(hight);
if ( lowR ==0 ) return low; if ( hightR ==0 ) return hight; if ( lowR * hightR > 0 ) return 0; result = ( low + hight ) / 2; funR = (*fun)( result ); if ( funR * ( funR >= 0 ? 1 : -1 ) <= det ) { return result; } else { if ( ( funR>=0 ? 1 : -1 ) + ( lowR>=0 ? 1 : -1 ) == 0 ) {result = HalfX( low, result, det, fun );} else {result = HalfX( result, hight, det, fun );} }
return result; } 是不是这个阿