牛顿迭代法(函数求解)
用于任意函数的求解中
优点:
函数任意
缺点:
只能求出一个根
含义:
用于求x^9+…+x*x+x=1的解
#include <iostream>
#include<cmath>
using namespace std;
static double N=9;
double h(double x){//原函数
int i = 0;
double s = x;
for (i = 1; i<N; i++){
s = x + x*s;
}
return s-1;
}
double hh(double x){//导数函数
double s=0;
int i;
for (i = 0; i<N; i++){
s = s + (i + 1)*pow(x, i);
}
return s;
}
double jie(double x){
while (abs(h(x)) > 0.0000001)//abs()绝对值函数
{
x = x - h(x)/hh(x);
}
return x;
}
void main(){
double x = 0.5;
x = jie(x);
cout << "解得x为:"<<x << endl;
}