牛顿迭代法算法的实现

//牛顿迭代法
#include<iostream>
#include<cmath>
#define Length 100
using namespace std;
class NewtonIterationMethod {
public:
	void input();
	double function(double x);
	double derivative(double x);
	double newtonmethod(double Xo);
	void analyse();
	void display();
private:
	double Xo;
	int n;
	double root;
}newton;
void NewtonIterationMethod::input() {
	cout << "**************************第一题**************************\n" << endl;
	cout << "  请输入初始值:Xo=";
	cin >> this->Xo;
	cout << "  请输入迭代次数:n=";
	cin >> this->n;
}
double NewtonIterationMethod::function(double x) {
	return (pow(x, 3) - x - 1);
}
double NewtonIterationMethod::derivative(double x) {
	return (3 * pow(x, 2) - 1);
}
double NewtonIterationMethod::newtonmethod(double Xo) {
	double x[Length];
	x[0] = Xo;
	for (int i = 0; i < n; i++) {
		x[i + 1] = x[i] - function(x[i]) / derivative(x[i]);
		root = x[i + 1];
	}
	return root;
}
void NewtonIterationMethod::analyse() {
	cout << "\n**************************第二题**************************\n" << endl;
	cout << "  迭代" << n << "次求得方程X^3-x-1=0在x=" << 0 << "附近的近似根X=" << newtonmethod(0) << endl;
	cout << "\n  原因分析:" << "在Xo=" << 0 << "处迭代,在精度相同的前提下,迭代次数超过给定\n"
		<< "  的最大值" << n << ",由此可见牛顿迭代法在初始值接近近似根处的迭代\n"
		<< "  速度要比远离近似根的迭代速度快很多,而且近似值的函数近似\n"<<"  值要精确很多\n" << endl;
}
void NewtonIterationMethod::display() {
	cout << "  迭代" << n << "次求得方程X^3-x-1=0在x=" << Xo << "附近的近似根X=" << newtonmethod(Xo) << endl;
}
int main() {
	newton.input();
	newton.display();
	newton.analyse();
	system("pause");
	return 0;
}

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值