c++迭代法

来源:百度知道 编辑:UC知道 时间:2024/09/23 16:55:52
求x的平方加10sinx=0的根 误差在0.00001

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double x0,x1;
cout << "请输入一个初值:" << endl;
cin >> x0;
x1 = x0;
int i=0;
while(i < 100000)
{
x0 = x1;
x1 = x0 - (x0 * x0 + 10 * sin(x0))/(2 * x0 + 10 * cos(x0));
i++;
if(x1 - x0 >= -0.00001 && x1 - x0 <= 0.00001) break;
}
if(i == 100000) cout << "迭代法失败" << endl;
else cout << "解为:" << x1 <<endl;
return 0;
}

解有-2.47948,0
当输入不同初值时得到不同的解。
牛顿迭代法