-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1b-secant.cpp
51 lines (41 loc) · 1.22 KB
/
1b-secant.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
#include<iostream>
#include<iomanip> /* setprecision */
#include<math.h> /* fabs */
// Fungsi f(x): x^3 + 2x^2 + 10 x = 20
#define f(x) pow(x, 3) + pow(2*x, 2) + 10*x - 20
using namespace std;
int main(){
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;
cout << setprecision(6) << fixed;
cout << "Masukkan x0 atau x(i-1): "; cin >> x0;
cout << "Masukkan x1 atau x(i): "; cin >> x1;
cout << "Masukkan error: "; cin >> e;
cout << "Masukkan maksimum iterasi: "; cin >> N;
cout << endl << "================" << endl;
cout << "Metode Secant" << endl;
cout << "================" << endl;
do {
f0 = f(x0); // f(x())
f1 = f(x1);
if (f0 == f1) {
cout << "Error";
exit(0);
}
x2 = x1 - ((x1 - x0) * f1)/(f1-f0);
f2 = f(x2);
cout << "Iterasi ke-" << step << ":\tx2 = " << setw(10) << x2 << " dan f(x2) = " << setw(10) << f(x2) << endl;
// jadikan baru
x0 = x1;
f0 = f1;
x1 = x2;
f1 = f2;
step++;
if (step > N){
cout << "Tidak konvergen.";
exit(0);
}
} while (fabs(f2) > e);
cout << endl << "Akarnya adalah: " << x2;
return 0;
}