見代碼:註釋也很明瞭
/*
牛頓迭代法求方程的一個零點
設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;
}