-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
55 lines (41 loc) · 995 Bytes
/
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
#include <iostream>
#include <math.h>
#include "main.h"
using std::cin;
using std::cout;
int main() {
double x0, x1;
cout << "Enter x0: ";
cin >> x0;
cout << "Enter x1: ";
cin >> x1;
secant(x0, x1);
return 0;
}
double equation(double x) {
return(pow(x, 4) - 2 * pow(x, 3) + 3 * pow(x, 2) - 1);
}
void secant(double x0, double x1) {
int i = 1;
double fx1, fx0, xnew;
cout << "Iteration #" << i << ": x0 = " << x0 << "\n";
i++;
while (modulus(x1-x0) > pow(10, -10)) {
fx0 = equation(x0);
fx1 = equation(x1);
if ((modulus(x1-x0) < pow(10, -10)) || (modulus(fx1-fx0) < pow(10, -10))) {
cout << "\n" << "Sorry, Root cant be reached from this point." << "\n";
return;
}
xnew = x1 - fx1 * ((x1 - x0) / (fx1 - fx0));
x0 = x1;
x1 = xnew;
cout << "Iteration #" << i << ": x0 = " << x0 << "\n";
i++;
}
cout << "Final Result = " << x0 << "\n";
}
inline double modulus(double x) {
if (x < 0) x = -x;
return x;
}