From 0461556e9f4a7d6ed4e7541b91c5ed088e463ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Wed, 28 Nov 2018 17:10:29 +0100 Subject: [PATCH] added 6th week --- ex_21.py | 16 ++++++++++++++++ ex_22.py | 34 ++++++++++++++++++++++++++++++++++ ex_23.py | 23 +++++++++++++++++++++++ ex_24.py | 23 +++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 ex_21.py create mode 100644 ex_22.py create mode 100644 ex_23.py create mode 100644 ex_24.py diff --git a/ex_21.py b/ex_21.py new file mode 100644 index 0000000..578bc55 --- /dev/null +++ b/ex_21.py @@ -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)) + diff --git a/ex_22.py b/ex_22.py new file mode 100644 index 0000000..e93c22a --- /dev/null +++ b/ex_22.py @@ -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() + diff --git a/ex_23.py b/ex_23.py new file mode 100644 index 0000000..f83cf14 --- /dev/null +++ b/ex_23.py @@ -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() + diff --git a/ex_24.py b/ex_24.py new file mode 100644 index 0000000..2d7f4dd --- /dev/null +++ b/ex_24.py @@ -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()