借助计算器或计算机,用二分法求方程

[复制链接]
查看11 | 回复0 | 2009-11-13 15:57:53 | 显示全部楼层 |阅读模式
方程有问题,但程序如下应该是对的:#include#includefloat f(float x){ float y; y=pow(0.8,x)-1-log(x);//自定义方程 return(y);}float xpoint(float x1,float x2){ float y; y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));//求 x轴点 交点坐标 return(y);}float root(float x1,float x2){ float x,y,y1; y1=f(x1); do {x=xpoint(x1,x2);y=f(x);if(y*y1>0){ y1=y; x1=x;}else x2=x; } while(fabs(y)>=1e-1); return(x);}void main(){ float x1,x2,f1,f2,x; do {printf("请输入方程解得范围x1,x2(注意中间用逗号隔开):\n"); scanf("%f,%f",&x1,&x2);f1=f(x1);f2=f(x2); } while(f1*f2>=0); x=root(x1,x2); printf("A root of equation is %.1f\n",x);}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行