added 6th week

This commit is contained in:
Daniel Knüttel 2018-11-28 17:10:29 +01:00
parent d6cca8a716
commit 0461556e9f
4 changed files with 96 additions and 0 deletions

16
ex_21.py Normal file
View File

@ -0,0 +1,16 @@
from scipy.special import zeta
class ZetaApprox(object):
def __init__(self, N):
self._N = N
def __call__(self, s):
result = 1
for n in range(1, self._N + 1):
result += 1 / n**s
return result
if( __name__ == "__main__"):
zeta_approx= ZetaApprox(10000000)
print(zeta_approx(1.15))
print(zeta(1.15))

34
ex_22.py Normal file
View File

@ -0,0 +1,34 @@
import numpy as np
import matplotlib.pyplot as plt
def sin_approx_factory(n):
def factorial(k):
result = 1
for i in range(1, k + 1):
result *= i
return result
def sin(x):
result = 0
for i in range(0, n + 1):
#print("+ (-1)**{} * x**{} / {} ".format(i, 2*i + 1, factorial(2*i + 1)), end = "")
result += (-1)**(i) * x**(2*i + 1) / factorial(2*i + 1)
#print()
return result
return sin
X = np.arange(-4, 4, 0.01)
plots = list()
p0, = plt.plot(X, np.sin(X), label="sin(x)")
plots.append(p0)
for i in range(5):
p, = plt.plot(X, sin_approx_factory(i)(X), label="approx({})".format(i))
plots.append(p)
plt.legend(handles=plots)
plt.show()

23
ex_23.py Normal file
View File

@ -0,0 +1,23 @@
import matplotlib.pyplot as plt
from matplotlib.ticker import FixedLocator, FuncFormatter
import numpy as np
X1 = np.arange(0, 8*np.pi, 0.01)
Y1 = np.cos(X1)
plt.plot(X1, Y1)
X2 = np.array([i*np.pi for i in range(9)])
X3 = np.array([i*np.pi/2 for i in range(1, 16, 2)])
Y2 = np.cos(X2)
Y3 = np.cos(X3)
plt.scatter(X2, Y2, color="green")
plt.scatter(X3, Y3, color="red")
plt.gca().xaxis.set_major_locator(FixedLocator(X2))
plt.gca().xaxis.set_major_formatter(FuncFormatter(lambda x,_: "${}\\pi$".format(x // np.pi)))
plt.show()

23
ex_24.py Normal file
View File

@ -0,0 +1,23 @@
import numpy as np
import matplotlib.pyplot as plt
X = np.arange(0, 10, 0.01)
K = 100
alpha = 0.15
real_Y = K*np.exp(-alpha * X)
def approx_function_generator(delta, alpha, K):
def f(x):
if(x <= 0):
return K
return (1 - delta*alpha)*f(x - delta)
return f
plt.plot(X, real_Y)
plt.plot(X, [approx_function_generator(2, alpha, K)(x) for x in X])
plt.plot(X, [approx_function_generator(1, alpha, K)(x) for x in X])
plt.plot(X, [approx_function_generator(0.1, alpha, K)(x) for x in X])
plt.show()