include iostream include vector include algorithm using namespace std

 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
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef vector<double> Vec;
typedef vector<Vec> Matrix;
double eps = 1e-10;
double phi(double x)
{
return (x*x - 5 * cos(x) + 3.25) / 3;
}
double f(double x)
{
return x*x - 3 * x + 3.25 - 5 * cos(x);
}
void simpleIteration(double &x)
{
double norm = 0;
double xNext;
do
{
xNext = phi(x);
norm = 0;
norm = max(fabs(xNext - x), norm);
x = xNext;
} while (norm > eps);
}
double chordMethod(double a, double b)
{
while (fabs(f(b - a)) > eps)
{
a = b - ((b - a) * f(b)) / (f(b) - f(a));
b = a - ((a - b) * f(a)) / (f(a) - f(b));
}
return b;
}
int main()
{
double x = -0.4;
simpleIteration(x);
cout << x << endl;
cout << f(x) << endl;
chordMethod(1, 2);
cout << x << endl;
cout << f(x) << endl;
}