📄 six.cpp
字号:
#include <iostream.h>
#include<math.h>
#include<iomanip.h>
double Funtion (double c);
double Funtion1(double x);
main()
{
//二分法
double a=0,b=1;
double x=0.3604217;
double h=0;
int n=0;
do{
h=(a+b)/2;
if(Funtion(a)*Funtion(h)<0)
b=h;
if(Funtion(h)*Funtion(b)<0)
a=h;
n++;
}while(fabs(h-x)>0.00001);
cout<<setprecision(12)
<<"the answer is:"<<h<<endl<<"the count is:"<<n<<endl;
//牛顿法
double x1[40];
x1[0]=1;
int n1=0;
do{
x1[n1+1]=x1[n1]-Funtion(x1[n1])/Funtion1(x1[n1]);
n1++;
}while(fabs(x1[n1]-x1[n1-1])>0.00001);
cout <<setprecision(12)
<<"the answer is:"<<x1[n1]<<endl<<"the count is:"<<n1<<endl;
//弦截法
double x2[40];
x2[0]=0;
x2[1]=1;
int n2=0;
do{
x2[n2+2]=x2[n2]*Funtion(x2[n2+1])
/(Funtion(x2[n2+1])-Funtion(x2[n2]))
+x2[n2+1]*Funtion(x2[n2])
/(Funtion(x2[n2])-Funtion(x2[n2+1]));
n2++;
}while(fabs(x2[n2+1]-x2[n2])>0.00001);
cout <<setprecision(12)
<<"the answer is:"<<x2[n2+1]<<endl<<"the count is:"<<n2<<endl;
return 0;
}
//函数
double Funtion (double x)
{
return 3*x+sin(x)-exp(x);
}
//函数的导数
double Funtion1(double x)
{
return 3+cos(x)-exp(x);
}
//
/*
the answer is:0.360412597656
the count is:14
the answer is:0.36042170296
the count is:5
the answer is:0.360421702961
the count is:6
Press any key to continue
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -