快速弦截法.cpp
来自「计算方法中的五种算法: 二分法、简单迭代法、快速弦截法、龙贝格算法、弦截法。 」· C++ 代码 · 共 43 行
CPP
43 行
#include<iostream.h>
#include<math.h>
double f(double x)
{
return x*x*x - 3*x - 1;
}
void KuaiSuXieJie(double x, double y, double e)
{
cout<<'\t'<<"k\t"<<"x\t"<<"x前 - x后\n";
double t1, t2, z;
cout<<'\t'<<"0\t"<<x<<'\n';
for(int k = 1; ; k++)
{
cout<<'\t'<<k<<'\t'<<y<<'\t'<<y-x<<'\n';
if( fabs(y - x) < e )
{
cout<<"结果为:"<<y<<'\n';
break;
}
else
{
t1 = f(x);
t2 = f(y);
z = y - (y - x) * t2 / (t2 - t1);
x = y;
y = z;
}
}
}
void main(void)
{
double x, y, e;
cout<<"输入初始值:";
cin>>x;
cout<<"输入另一个初始值:";
cin>>y;
cout<<"输入精度:";
cin>>e;
KuaiSuXieJie(x, y, e);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?