加速迭代.cpp

来自「用C++实现的多个算法」· C++ 代码 · 共 40 行

CPP
40
字号
#include<iostream.h>
#include<math.h>
#include<iomanip.h>

double f(double x);
double fr(double x);

void main()
{
	double x0,x1,e,q;
	cout<<"请输入x0和e的值:"<<endl;
	cout<<"x0=";
	cin>>x0;
	cout<<"e=";
	cin>>e;
	x1=x0;
	do{
		x0=x1;
		q=fr(x0);
		x1=f(x0)+q*(f(x0)-x0)/(1-q);
	}
	while(fabs(x1-x0)>=e);
			cout<<setw(15)<<setprecision(10)<<x1<<endl;
}

double f(double x)
{
	double y;
	x=log(x)+89.62092;
	y=pow(x,1.0/3.0);
	return y;
}
double fr(double x)
{
	double y1;
	x=log(x)+89.62092;
	y1=pow(x,-2.0/3.0)/3.0;
	return y1;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?