[数值算法]求根算法系列小结

[数值算法]求根算法系列小结

1二分求根法:

       二分求根法主要用的思想是不断调整并缩小搜索区间的大小,当搜索区间的大小已小于搜索精度要求时,则可说明已找到满足条件的近拟根.

       当然,在这之前,首先是要准确的估计出根所处的区间,否则,是找不到根的。

Type binaryPationMethod(Type x1,Type x2,Type e,Type (*arguF)(Type),FILE* outputFile)

{

       Type x;/*The return answer*/

       Type mid;

       Type down,up;

       int iteratorNum=0;

 

 

       down=x1;

       up=x2;

      

       assertF(x1<=x2,"in twoPation x1>=x2");

       assertF(arguF!=NULL,"in twoPation arguF is NULL");

       assertF(outputFile!=NULL,"in twoPation outputFile is NULL");

       assertF((*arguF)(x1)*(*arguF)(x2)<=0,"in twoPation,f(x1)*f(x2)>0");

      

       fprintf(outputFile,"down/t/tup/t/t/r/n");

       /*two pation is a method that is surely to find root for a formula*/

       while(fabs(up-down)>(float)1/(float)2*e)

       {

              mid=(down+up)/2;

              if ((*arguF)(mid)==0)

                     break;

              else if(((*arguF)(down))*((*arguF)(mid))>0)

                     down=mid;

              else

                     up=mid;

             

              fprintf(outputFile,"% -12f% -12f/r/n",down,up);

              iteratorNum++;

       }

       /*get the answer*/

       x=mid;

      

       /*Output Answer*/

       fprintf(outputFile,"total iterator time is:%d/r/n",iteratorNum);           

       fprintf(outputFile,"a root of equation is :%f/r/n",x);

      

       return x;

}

       测试1:用二分法求:

f(x)=x^3-x^2-2*x+1=0(0,1)附近的根.

精度:0.001.

Output:

down             up          

0.000000    0.500000   

0.250000    0.500000   

0.375000    0.500000   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值