-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFixedIterator.cpp
More file actions
40 lines (35 loc) · 894 Bytes
/
Copy pathFixedIterator.cpp
File metadata and controls
40 lines (35 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "FixedIterator.h"
#include <iostream>
#include <iomanip>
using namespace std;
double fixedIterator(double p0, double tol,int N){
int i = 0;
double p;
cout << "不动点迭代开始:" << endl;
while (i++ < (N+1)){
p = p0 - (powf(p0, 3) + 4 * powf(p0, 2) - 10.0) / (3 * powf(p0, 2) + 8 * p0);
cout << setprecision(16)<<"p:" << p << endl << endl;
if (fabs(p - p0) < tol){
cout << "不动点迭代结束:" << endl;
return p;
}
p0 = p;
}
cout << "不动点迭代结束:迭代失败!" << endl;
return NULL;
}
double fixedIterator(double p0, double tol,int N, double(*function)(double p)){
int i = 0;
double p;
cout << "不动点迭代开始:" << endl;
while (i++ < (N + 1)){
p = function(p0);
cout << setprecision(16)<<"p:" << endl << endl;
if (fabs(p - p0) < tol){
cout << "不动点迭代结束:" << endl;
}
p0 = p;
}
cout << "不动点迭代结束:迭代失败!" << endl;
return NULL;
}