#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
using std::vector;
//测试函数是xe^x
void fixedpoint()
{
vector<double> x1;
double num0 = 0.5, num1 = 0, err = 0;
do
{
num1 = exp(-num0);
x1.push_back(num1);
err = abs(num0 - num1);
num0 = num1;
} while (err > 0.0001);
auto it = x1.begin();
int i = 1;
while (it != x1.end())
{
cout << "第" << i++ << "次迭代"
<< ":" << (*it) << endl;
++it;
}
}
//需要手动进行求导xe^x-1=0
void newton()
{
vector<double> x1;
double num0 = 0.5, num1 = 0, err = 0;
do
{
num1 = (pow(num0, 2) + exp(-num0)) / (num0 + 1);
x1.push_back(num1);
err = abs(num0 - num1);
num0 = num1;
} while (err > 0.0001);
auto it = x1.begin();
int i = 1;
while (it != x1.end())
{
cout << "第" << i++ << "次迭代"
<< ":" << (*it) << endl;
++it;
}
}
int main()
{
fixedpoint();
newton();
cin.get();
return 0;
}
比较简单仅供参考