1. 程式人生 > >用二分法和迭代法求e^x+10*x-2=0方程的解

用二分法和迭代法求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);
}
在這裡插入程式碼片

在這裡插入圖片描述