-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSteffensen.cpp
43 lines (40 loc) · 978 Bytes
/
Steffensen.cpp
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
41
42
43
#include "Steffensen.h"
#include <iostream>
double Steffensen(double p0, double tol, int n){
int i = 1;
double p1, p2,p;
cout << "Steffense快速收敛算法开始:" << endl;
while (i <= n){
p1 = sqrtf(10/(p0+4));
p2 = sqrtf(10/(p1+4));
p = p0 - powf(p1 - p0, 2) / (p2 - 2 * p1 + p0);
cout << "p:" << p << endl;
if (fabs(p - p0) < tol){
cout << "Steffense快速收敛算法结束:" << endl;
return p;
}
i += 1;
p0 = p;
}
cout << "Stenffense快速收敛算法失败!" << endl;
return NULL;
}
double Steffensen(double p0, double tol, int n, double(*function)(double)){
int i = 1;
double p1, p2, p;
cout << "Steffense快速收敛算法开始:" << endl;
while (i <= n){
p1 = function(p0);
p2 = function(p1);
p = p0 - powf(p1 - p0, 2) / (p2 - 2 * p1 + p0);
cout << "p:" << p << endl;
if (fabsf(p - p0) < tol){
cout << "Steffense快速收敛算法结束:" << endl;
return p;
}
i = i + 1;
p0 = p;
}
cout << "Stenffense快速收敛算法失败!" << endl;
return NULL;
}