-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNum1-9.py
70 lines (59 loc) · 1.94 KB
/
Num1-9.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
# Serie 9
import matplotlib.pyplot as plt
import numpy as np
tol = 2.220446049250313e-10
hmin = 2.220446049250313e-3
class adaptiveSimpson: # basically a readable version of the code from the lecture
def __init__(self, i, f):
self.a, self.b = i
self.delta = [i]
self.f = f
self.integralValue = 0
self.points = []
def simpson(self, interval):
(a,b) = interval
return (b-a)*(self.f(a)+4*self.f((a+b)/2)+self.f(b))/6
def error(self, interval):
a,b = interval
m = (a+b)/2
simp0 = self.simpson(interval)
simp1 = self.simpson((a,m))
simp2 = self.simpson((m,b))
return abs(simp0-simp1-simp2), simp1+simp2
def solve(self):
while len(self.delta) > 0:
deltaOld, self.delta = self.delta, []
for J in deltaOld:
(aJ,bJ) = J
(err, compositeSimpson) = self.error(J)
if err > 10*(bJ-aJ)*tol/(self.b-self.a) and (bJ-aJ) > hmin:
m = (aJ+bJ)/2
J1 = (aJ,m)
J2 = (m,bJ)
self.delta.append(J1)
self.delta.append(J2)
else:
self.integralValue += compositeSimpson
self.points.append(aJ)
self.points.append((aJ+bJ)/2)
self.points.append(bJ)
def plot(self):
print(self.integralValue)
xvalues = np.arange(self.a, self.b, 1e-6)
yvalues = self.f(xvalues)
plt.plot(xvalues, yvalues)
for point in self.points:
plt.axvline(x=point)
plt.show()
def Aufgabe1():
f = lambda x : np.exp(-10*(x-1)**2)
fInterval = (-1,1)
g = lambda x : x**(1/2)
gInterval = (0,1)
# simpf = adaptiveSimpson(fInterval, f)
# simpf.solve()
# simpf.plot()
simpg = adaptiveSimpson(gInterval, g)
simpg.solve()
simpg.plot()
Aufgabe1()