-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
81 lines (72 loc) · 2.07 KB
/
main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// fixPointIteration.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cmath>
using namespace std;
float function1(float xn)
{
return (xn = sqrtf(3 * xn - 1));
}
float function2(float xn)
{
return (xn = (-1 / (xn - 3)));
}
float function3(float xn)
{
return (xn = ((3 * xn - 1) / xn));
}
float solveViaIteration(float x)
{
float currentX = x;
cout << "Solving for (xn = sqrtf(3 * xn - 1)) for x_1: " << x << endl;
for (int i = 0;; i++)
{
cout << "Iteration: " << i << " Value: " << currentX << endl;
float nextX = function1(currentX);
if (abs(currentX - nextX) < 1E-5)
{
cout << "Fixed Point Iteration complete at iteration: " << i << endl;
cout << " with a value of " << setprecision(5) << currentX << endl;
break;
}
currentX = nextX;
}
cout << "Solving for (xn = (-1 / (xn - 3))) for x_1: " << x << endl;
currentX = x;
for (int i = 0;; i++)
{
cout << "Iteration: " << i << " Value: " << currentX << endl;
float nextX = function2(currentX);
if (abs(currentX - nextX) < 1E-5)
{
cout << "Fixed Point Iteration complete at iteration: " << i << endl;
cout << " with a value of " << setprecision(5) << currentX << endl;
break;
}
currentX = nextX;
}
cout << "Solving for (xn = ((3 * xn - 1) / xn)) for x_1: " << x << endl;
currentX = x;
for (int i = 0;; i++)
{
cout << "Iteration: " << i << " Value: " << currentX << endl;
float nextX = function3(currentX);
if (abs(currentX - nextX) < 1E-5)
{
cout << "Fixed Point Iteration complete at iteration: " << i << endl;
cout << " with a value of " << setprecision(5) << currentX << endl;
return currentX;
}
currentX = nextX;
}
}
int main()
{
float x = 0;
cout << "Enter x_1: ";
cin >> x;
solveViaIteration(x);
return 0;
}