-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum.py
69 lines (68 loc) · 1.35 KB
/
sum.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
from sympy import *
from mpmath import *
x = Symbol('x')
n = input("sum : ")
a = sympify(n)
mp.dps = 6
while(1):
print("enter x value to substitue in equation")
j = input()
j = sympify(j)
jm = a.evalf(subs={x:j})
jm = N(jm,8)
print(jm)
print("1.exit. Any other number continue...")
bn = int(input())
if(bn==1):
break
#k = input()
#k = sympify(k)
#l = a
#l.evalf(subs = {x:k})
print("Enter value of x1")
x1 = input()
x1 = sympify(x1)
print("Enter value of x2")
x2 = input()
x2 = sympify(x2)
x0 = 0
def formu():
global a
h = a
global x1
global x2
k = h.evalf(subs={x:x1})
l = h.evalf(subs={x:x2})
m = k*(x2-x1)/(l-k)
n = x1-m
n = N(n,8)
return n
while(1):
x0 = formu()
x0 = N(x0,8)
print("x0 = "+str(x0))
print("x1 = "+str(x1))
print("x2 = "+str(x2))
g = a.evalf(subs={x:x0})
k = a.evalf(subs={x:x1})
l = a.evalf(subs={x:x2})
g = N(g,8)
k = N(k,8)
l = N(l,8)
print("f(x0) = "+str(g))
print("f(x1) = "+str(k))
print("f(x2) = "+str(l))
jk = g*k
lk = g*l
print("f(x0)*f(x1) = "+str(jk))
print("f(x0)*f(x2) = "+str(lk))
if(jk<0):
x2 = x0
print("x2 = x0")
elif(lk<0):
x1 = x0
print("x1 = x0")
print("2.exit. Any other number to continue...")
bm = int(input())
if(bm==2):
break