[数值算法]截弦法 方程求根

相关的理论请参考数值计算相关书籍,我这里只给出关键的函数及主程序段,其余相关的细节就不再一一罗列了.

/*Core  Function*/

#include "FindRoot.h"

#include "MyAssert.h"

#include "Ulti.h"

#include <stdlib.h>

#include <stdio.h>

#include <math.h>

 

void jieXianMethod(Type x1,Type x2,double e,Type (*arguF)(Type),FILE* outputFile)

{

       Type x;/*The return answer*/

       Type y,y1,y2;/*The tween answer*/

       int iteratorNum=0;

      

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

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

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

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

      

       /*Core Program*/

       do

       {

              x=(x1*(*arguF)(x2)-x2*(*arguF)(x1))/((*arguF)(x2)-(*arguF)(x1));

              y=(*arguF)(x);

              y1=(*arguF)(x1);

              if(y*y1>=0)

              {

                     x1=x;y1=y;

              }

              else

              {

                     x2=x;y2=y;

              }

              fprintf(outputFile,"x1:%-12f x2:%-12f y:%-12f/r/n",x1,x2,y);

              iteratorNum++;

       }

       while(fabs(y)>e);

      

       /*Output Answer*/

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

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

}

 

 

 

 

/*Main Test Program*/

#include "MyAssert.h"

#include "FindRoot.h"

#include <time.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

Type testF(Type argu);

 

char *inFileName="inputData.txt";

/*

       input data specification

       x1,x2 means the root is located about in [x1,x2]

       e is the limitation of the control answer

*/

 

char *outFileName="outputData.txt";

#define DEBUG 1

 

void main(int argc,char* argv[])

{

       FILE *inputFile;/*input file*/

       FILE *outputFile;/*output file*/

 

       double startTime,endTime,tweenTime;/*time callopsed info*/

       float x1,x2,e;

       /*input file open*/

       if(argc>1)strcpy(inFileName,argv[1]);

       assertF((inputFile=fopen(inFileName,"rb"))!=NULL,"input file error");

       printf("input file open success/n");

      

       /*outpout file open*/

       if(argc>2)strcpy(outFileName,argv[2]);

       assertF((outputFile=fopen(outFileName,"wb"))!=NULL,"output file error");

       printf("output file open success/n");

      

       /*Read info data*/

       fscanf(inputFile,"%f,%f,%f,",&x1,&x2,&e);

       printf("read in data info:%f,%f,%f./n",x1,x2,e);

      

#if  DEBUG

       printf("/n*******start of test program******/n");

       printf("now is runnig,please wait.../n");

       startTime=(double)clock()/(double)CLOCKS_PER_SEC;

       /******************Core program code*************/

              jieXianMethod(x1,x2,e,&testF,outputFile);

       /******************End of Core program**********/

       endTime=(double)clock()/(double)CLOCKS_PER_SEC;

       tweenTime=endTime-startTime;/*Get the time collapsed*/

       /*Time collapsed output*/

       printf("the collapsed time in this algorithm implement is:%f/n",tweenTime);

       fprintf(outputFile,"the collapsed time in this algorithm implement is:%f/r/n",tweenTime);    

       printf("/n*******end of test program******/n");

#endif

 

       printf("program end successfully,/n you have to preess any key to clean the buffer area to output,otherwise,you wiil not get the total answer./n");

       getchar();/*Screen Delay Control*/

       return;

}

 

Type testF(Type x)

{

       return x*x*x-5*x*x+16*x-80;

}

 

测试数据:

输入inputData.txt:

2,6,0.0001,

 

输出:outputData.txt:

x1:4.142857     x2:6.000000     y:-28.425657 

x1:4.799246     x2:6.000000     y:-7.835989  

x1:4.956494     x2:6.000000     y:-1.764906  

x1:4.990748     x2:6.000000     y:-0.378460  

x1:4.998041     x2:6.000000     y:-0.080294  

x1:4.999585     x2:6.000000     y:-0.017007  

x1:4.999912     x2:6.000000     y:-0.003597  

x1:4.999981     x2:6.000000     y:-0.000782  

x1:4.999996     x2:6.000000     y:-0.000156  

x1:5.000000     x2:6.000000     y:-0.000020  

total iterator time is:10

a root of equation is :5.000000

the collapsed time in this algorithm implement is:0.000000
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值