-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfp.py
executable file
·106 lines (87 loc) · 2.2 KB
/
fp.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
from sympy import var
from sympy import sympify
from math import *
import sys
from prettytable import PrettyTable
import time
# Value from function
def f(value):
res = expr.subs(x, value)
return res
# False Position Method
def false_position(xu, xl):
atas = xu*f(xl)-xl*f(xu)
bawah = f(xl)-f(xu)
xm = atas/bawah
return xm
# Input
x = var('x')
try:
user_input = input("Enter the function (3*x**2+2*x+2) = ")
expr = sympify(user_input)
except:
print("input yang anda masukkan tidak sesuai")
sys.exit()
try:
xl = float(input("Enter the value of Xl (-10) = "))
except:
print("input yang anda masukkan tidak sesuai")
sys.exit()
try:
xu = float(input("enter the value of xu (15) = "))
except:
print("input yang anda masukkan tidak sesuai")
sys.exit()
if(f(xl)*f(xu) < 0):
success = 1
else:
print("Pastikan nilai F(xl) dan F(xu) berlawanan tanda")
sys.exit()
try:
RAE = float(input("Enter the value of Aproximation Error (0.0001) = "))
except:
print("input yang anda masukkan tidak sesuai")
sys.exit()
RAE_now = 100
counter = 0
x_old = 0
# Definition Table
t = PrettyTable()
t.field_names = ["i", "xi", "Aproximation Error", 'f(xl)', 'f(xu)', 'f(xm)']
start = time.time()
try:
# looping
while RAE < RAE_now:
xm = false_position(xl, xu)
diff = f(xl)*f(xm)
xu_old = xu
xl_old = xl
if(diff < 0):
xu = xm
elif(diff > 0):
xl = xm
elif(diff == 0):
print("True Value = "+str(xm)+".")
sys.exit()
else:
print("Program Error")
sys.exit()
if(counter == 0):
x_new = xm
t.add_row([counter, x_new, "-", f(xl_old), f(xu_old), f(xm)])
counter = counter+1
x_old = x_new
continue
x_new = xm
selisih = abs(x_new-x_old)
RAE_now = abs(selisih/x_new)
t.add_row([counter, x_new, RAE_now, f(xl_old), f(xu_old), f(xm)])
x_old = x_new
counter = counter+1
except:
print("Program Error")
sys.exit()
end = time.time()
print(t)
print("Waktu yang dibutuhkan adalah "+str(end-start)+" detik")