-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
56 lines (47 loc) · 1.49 KB
/
app.py
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
def fungsi(x):
# Definisikan fungsi yang ingin dihitung di sini
return x**2 - 4
def biseksi(a, b, toleransi):
# Metode Biseksi
if fungsi(a) * fungsi(b) >= 0:
print("Tidak terdapat akar pada rentang ini.")
return None
iterasi = 0
while (b - a) >= toleransi:
iterasi += 1
c = (a + b) / 2
if fungsi(c) == 0.0:
break
if fungsi(c) * fungsi(a) < 0:
b = c
else:
a = c
print("Metode Biseksi:")
print("Jumlah iterasi:", iterasi)
print("Akar yang ditemukan:", c)
def regulafalsi(a, b, toleransi):
# Metode Regula Falsi
if fungsi(a) * fungsi(b) >= 0:
print("Tidak terdapat akar pada rentang ini.")
return None
iterasi = 0
while abs(b - a) >= toleransi:
iterasi += 1
c = (a * fungsi(b) - b * fungsi(a)) / (fungsi(b) - fungsi(a))
if fungsi(c) == 0.0:
break
if fungsi(c) * fungsi(a) < 0:
b = c
else:
a = c
print("Metode Regula Falsi:")
print("Jumlah iterasi:", iterasi)
print("Akar yang ditemukan:", c)
# Input rentang dan toleransi dari user
a = float(input("Masukkan batas bawah rentang: "))
b = float(input("Masukkan batas atas rentang: "))
toleransi = float(input("Masukkan toleransi: "))
# Hitung menggunakan metode Biseksi
biseksi(a, b, toleransi)
# Hitung menggunakan metode Regula Falsi
regulafalsi(a, b, toleransi)