用二分法和迭代法求e^x+10*x-2=0方程的解

主要运用循环while

#include<stdio.h>
#include<math.h>
double erfenfa() 
{double a=1,b=0,c;
while(fabs(a-b)>=5e-4)
{c=(a+b)/2.0;
if((exp(c)+10*c-2)==0)
return c;
if((exp(a)+10*a-2)*(exp(c)+10*c-2)<0)
      b=c;
	  else
	  a=c;}
	  c=(a+b)/2.0;
	  return c;
}//二分法
double  diedaifa()
{double a=0,b;
b=(2-exp(a))/10.0;
while(fabs(a-b)>=5e-4)
{a=b;
b=(2-exp(a))/10.0;
}
return b;
 } //迭代法 
int main()
{double c;
c=erfenfa();
printf("二分法算e^x+10*x-2=0方程的解是%lf",c);
c=diedaifa();
printf("\n迭代法算e^x+10*x-2=0方程的解是%lf",c);
}
在这里插入代码片

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/feiqipengcheng/article/details/84035079
今日推荐