【牛顿迭代法求函数的一个零点】模板

见代码:注释也很明了

/*
牛顿迭代法求方程的一个零点
设f2(x)为f(x)的导数
任取x,方程的零点即为x=x-f(x)/f2(x)
循环几百次就可以得出一个零点。 
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
double f(double x)
{
	return x*x+2*x+1;
}
double f2(double x)
{
	return 2*x+2;
}
int main()
{
	double x = 233;
	for(int i=0;i<maxn;i++)
	{
		x = x-f(x)/f2(x);
	}
	cout<<x<<endl;
	return 0;
} 
发布了159 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/KIKO_caoyue/article/details/99762946