-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumerical Solver.py
300 lines (259 loc) · 8.74 KB
/
Numerical Solver.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import math
from sympy import *
import numpy as np
x = Symbol('x')
def Newton_Raphson():
f = eval(input("enter your function here>>\n"))
f_prime = diff(f,x)
a = float (input ("enter the value of xi"))
for i in range (6):
print("_________________________\n")
print ("no. of iterations =" , i)
print ("xi = " ,a)
print("f(x)= \n" ,f.subs(x,a))
print("f'(x) = \n" ,f_prime.subs(x,a))
a = a - (f.subs(x,a)/f_prime.subs(x,a))
print("xi+1 = \n", a)
def Secant():
f = eval(input("enter your function here>>\n"))
f_prime = f.diff(x)
a = float (input ("enter the value of xi-1 \t"))
b = float (input ("enter the value of xi \t"))
for i in range (6):
print("_________________________\n")
print ("no. of iterations =" , i)
print ("xi-1 = \t " ,a)
print("f(xi-1)= \n" ,f.evalf(subs= {x:a}))
print ("xi = \t " ,b)
print("f(xi) = \n" ,f.evalf(subs= {x:b}))
n = float (b - (f.evalf(subs= {x:b})* ((b-a)/(f.evalf(subs= {x:b})-f.evalf(subs= {x:a})))))
a=b
b=n
print("xi+1 = \n", n)
def Flase_position():
f = eval(input("enter your function here>>\n"))
f_prime = f.diff(x)
a = float (input ("enter the value of xi-1 \t"))
b = float (input ("enter the value of xi \t"))
r = int(input("enter the no. of iterations"))
for i in range (r):
print("_________________________\n")
print ("no. of iterations =" , i)
print ("xi-1 = \t " ,a)
print("f(xi-1)= \n" ,f.subs(x,a))
print ("xi = \t " ,b)
print("f(xi) = \n" ,f.subs(x,b))
n = float (a - (f.subs(x,a)* ((b-a)/((f.subs(x,b))-f.subs(x,a)))))
print ("rel. error",abs(b-n))
a=b
b=n
print("xi+1 = \n", n)
def Muller():
f = eval(input("enter your function here>>\n"))
a = float (input ("enter the value of x0 \t"))
b = float (input ("enter the value of x1 \t"))
c = float (input ("enter the value of x2 \t"))
r = int(input("enter the number of iterations \t"))
for i in range (r):
print("_________________________\n")
print ("no. of iterations =" , i+1)
h0 = b-a
h1 = c-b
gamma0 = (f.evalf(subs= {x:b})-f.evalf(subs= {x:a}))/(b-a)
gamma1 = (f.evalf(subs= {x:c})-f.evalf(subs= {x:b}))/(c-b)
v= (gamma1-gamma0)/(h1+h0)
j= v*h1+gamma1
d= f.evalf(subs= {x:c})
print ("x0 = \t " ,a)
print("f(x0)= \n" ,f.evalf(subs= {x:a}))
print ("x1 = \t " ,b)
print("f(x1) = \n" ,f.evalf(subs= {x:b}))
print ("x2 = \t " ,c)
print("f(x2) = \n" ,f.evalf(subs= {x:c}))
print("h0 == \t", h0)
print("h1== \t",h1)
print("gamma0== \t", gamma0)
print("gamma1== \t", gamma1)
print("a== \t", v)
print("b== \t", j)
print("c== \t", d)
n = float (c + ((-2*d)/(j+math.sqrt(j**2-4*v*d))))
a=b
b=c
c=n
h0=h1
gamma0=gamma1
print("x3 = \t", n)
def Bisection():
f = eval(input("enter your function here>>\n"))
a = float (input ("enter the value of a>> \t"))
b = float (input ("enter the value of b>> \t"))
sigma = float (input ("enter the value of sigma>> \t"))
c= (a+b)/2
i=1
n =(math.log(b-a)- math.log(sigma))/math.log(2)
while(f.evalf(subs= {x:c})!=0 or i+1!=n or (b-a)>sigma ):
print("____________________\n")
print("a=",a)
print("b=",b)
print("f(a) = \n" ,f.evalf(subs= {x:a}))
print("f(b) = \n" ,f.evalf(subs= {x:b}))
c= (a+b)/2
print("c=",c)
print("f(c) = \n" ,f.evalf(subs= {x:c}))
if ((f.evalf(subs= {x:a})*f.evalf(subs= {x:c})) <0):
b=c
else:
a=c
i=i+1
if (f.evalf(subs= {x:c})==0):
break
elif (i+1==n):
break
elif ((b-a)<sigma):
break
def proterm(i, value, x):
pro = 1;
for j in range(i):
pro = pro * (value - x[j]);
return pro;
def dividedDiffTable(x, y, n):
for i in range(1, n):
for j in range(n - i):
y[j][i] = ((y[j][i - 1] - y[j + 1][i - 1]) /
(x[j] - x[i + j]));
return y;
def applyFormula(value, x, y, n):
sum = y[0][0];
for i in range(1, n):
sum = sum + (proterm(i, value, x) * y[0][i]);
return sum;
def printDiffTable(y, n):
for i in range(n):
for j in range(n - i):
print(round(y[i][j], 4), "\t",
end = " ");
print("");
def newton_int ():
n =int(input("enter the no. of columns>> \t"))
x = np.zeros((n))
y = [[0 for i in range(10)]
for j in range(10)]
for i in range(n):
x[i] = float(input( 'x['+str(i)+']='))
y[i][0] = float(input( 'y['+str(i)+']='))
y=dividedDiffTable(x, y, n)
printDiffTable(y, n)
value = float(input("enter the value of x>> \t"))
print("\nValue at", value, "is", round(applyFormula(value, x, y, n), 2))
def Lagrange ():
n = int(input('Enter the no. of columns \n'))
x = np.zeros((n))
y = np.zeros((n))
print('Enter data for x and y >> \n')
for i in range(n):
x[i] = float(input( 'x['+str(i)+']='))
y[i] = float(input( 'y['+str(i)+']='))
xp = float(input('Enter the value of x >> \t '))
yp = 0
for i in range(n):
p = 1
for j in range(n):
if i != j:
p = p * (xp - x[j])/(x[i] - x[j])
yp = yp + p * y[i]
l= p*y[i]
print('l(%d)=%f\t'% (i,l))
print('Interpolated value at %.3f is %.4f.' % (xp, yp))
def Euler():
y = Symbol ('y')
f = eval(input("enter your function here>>\n"))
n = int(input("enter no. of steps>> \t"))
h = float (input("enter the width(h)>> \t"))
x1 = float(input("enter x initial \t"))
y1 = float(input("enter y initial \t"))
for i in range ((n)):
print("_____________________\n")
print("Step %d "% (n-1))
x2 = x1+h
y2 = y1 + h*f.subs([(x,x1),(y,y1)])
print("x2 =\t ",x2)
print("y2 =\t ",y2)
x1 = x2
y1 = y2
n=n+1
def RK4():
y = Symbol ('y')
f = eval(input("enter your function here>>\n"))
n = int(input("enter no. of steps>> \t"))
h = float (input("enter the width(h)>> \t"))
x1 = float(input("enter x initial \t"))
y1 = float(input("enter y initial \t"))
for i in range ((n)):
print("_____________________\n")
print("Step %d "% (n-1))
k1 = f.subs([(x,x1),(y,y1)])
k2 = f.subs([(x,(x1+h/2)),(y,(y1+(h/2)*k1))])
k3 = f.subs([(x,(x1+h/2)),(y,(y1+(h/2)*k2))])
k4 = f.subs([(x,(x1+h)),(y,(y1+(k3*h)))])
x2 = x1+h
y2 = y1 + (h/6)*(k1+2*k2+2*k3+k4)
print("K1 = \t ",k1)
print("K2 = \t ",k2)
print("K3 = \t ",k3)
print("K4 = \t ",k4)
print("x2 =\t ",x2)
print("y2 =\t ",y2)
x1 = x2
y1 = y2
n=n+1
def adam():
y = Symbol ('y')
f = eval(input("enter your function here>>\n"))
n = int(input("enter no. of steps>> \t"))
h = float (input("enter the width(h)>> \t"))
x1 = float(input("enter x initial \t"))
y1 = float(input("enter y initial \t"))
k1 = f.subs([(x,x1),(y,y1)])
k2 = f.subs([(x,(x1+h)),(y,(y1+k1*h))])
x2 = x1 +h
y2 = y1 +((h/2)*(k1+k2))
print("Using RK2>>>\n")
print("x1= \t",x1)
print("y1= \t",y1)
print("k1= \t",k1)
print("k2= \t",k2)
print("x2= \t",x2)
print("y2= \t",y2)
for i in range(n):
print("_____________________\n")
print("Step %d "% (n-1))
x3 = x2 +h
y3= y2+h*((3/2)*f.subs([(x,x2),(y,y2)])-(1/2)*f.subs([(x,x1),(y,y1)]))
print("y2 = \t",y3)
x1=x2
y1=y2
x2=x3
y2=y3
print("__Welcome to Numerical Solver__ \n")
i= input("Please Choose The Method You Want >>>\n a)Bisection\n b)Newton Raphson\n c)Secant\n d)False Position\n e)Muller\n f)Lagrange\n g)Newton Divided Diff\n h)Euler\n i)RK4 \n j)Adams-Bashforth \n ")
if (i =='a'):
Bisection()
elif (i == 'b'):
Newton_Raphson()
elif (i == 'c'):
Secant()
elif (i == 'd'):
Flase_position()
elif (i == 'e'):
Muller()
elif (i == 'f'):
Lagrange()
elif (i== 'g'):
newton_int()
elif (i== 'h'):
Euler()
elif (i=='i'):
RK4()
elif (i=='j'):
adam()