Initial
This commit is contained in:
22
uebung_01/exercise_1.py
Normal file
22
uebung_01/exercise_1.py
Normal file
@ -0,0 +1,22 @@
|
||||
import numpy as np
|
||||
|
||||
for n in [3,5,10,100,1000]:
|
||||
A = np.zeros((n,n))
|
||||
x = np.zeros((n,1))
|
||||
y_c = np.zeros((n,1))
|
||||
|
||||
for i in range(0,n):
|
||||
x[i] = i+1
|
||||
y_c[i] = (i+1) * n;
|
||||
|
||||
for j in range(0,n):
|
||||
A[i,j] =(i+1) / (j+1)
|
||||
|
||||
y = A@x
|
||||
|
||||
if n == 3 or n == 5:
|
||||
print(x)
|
||||
print(A)
|
||||
print(y)
|
||||
|
||||
print("Fuer n =",n,":",np.linalg.norm(y - y_c))
|
24
uebung_01/exercise_2a.py
Normal file
24
uebung_01/exercise_2a.py
Normal file
@ -0,0 +1,24 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# x = np.linspace(0, 2 * np.pi, 100)
|
||||
x_1 = np.arange(0, 2 * np.pi, 1)
|
||||
x_0_5 = np.arange(0, 2 * np.pi, 0.5)
|
||||
x_0_1 = np.arange(0, 2 * np.pi, 0.1)
|
||||
|
||||
y_s_1 = np.sin(x_1)
|
||||
y_s_0_5 = np.sin(x_0_5)
|
||||
y_s_0_1 = np.sin(x_0_1)
|
||||
y_c = np.cos(x_1)
|
||||
|
||||
plt.plot(x_1,y_s_1,"purple", label="sin(x) with stepsize 1")
|
||||
plt.plot(x_0_5,y_s_0_5,"pink", label="sin(x) with stepsize 0.5")
|
||||
plt.plot(x_0_1,y_s_0_1,"red", label="sin(x) with stepsize 0.1")
|
||||
plt.plot(x_1,y_c,"green", label="cos(x)")
|
||||
|
||||
plt.title("A2.a) Sine and Cosine")
|
||||
plt.xlabel("x")
|
||||
plt.ylabel("y")
|
||||
plt.legend()
|
||||
|
||||
plt.show()
|
22
uebung_01/exercise_2b.py
Normal file
22
uebung_01/exercise_2b.py
Normal file
@ -0,0 +1,22 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
plt.rcParams['text.usetex'] = True
|
||||
|
||||
# x = np.linspace(0, 2 * np.pi, 100)
|
||||
x = np.arange(-2, 2, 0.1)
|
||||
|
||||
f = np.exp(-x**2)
|
||||
g = np.sin(x**2)
|
||||
h = np.sin(1 / (x**3 + 9))
|
||||
|
||||
plt.plot(x, f, "purple", label=r"$f(x) = \exp(-x^2)$")
|
||||
plt.plot(x, g, "red", label=r"$g(x) = \sin(x^2)$")
|
||||
plt.plot(x, h, "green", label=r"$h(x) = \frac{1}{x^3 + 9}$")
|
||||
|
||||
plt.title("A2.b)")
|
||||
plt.xlabel("x")
|
||||
plt.ylabel("y")
|
||||
plt.legend()
|
||||
|
||||
plt.show()
|
68
uebung_01/exercise_3.py
Normal file
68
uebung_01/exercise_3.py
Normal file
@ -0,0 +1,68 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def runge(x):
|
||||
return 1 / (1 + 25 * x**2)
|
||||
|
||||
def divided_differences(x, y):
|
||||
n = len(y)
|
||||
a = np.zeros((n,n))
|
||||
|
||||
a[:,0] = y
|
||||
|
||||
for i in range(1,n):
|
||||
for j in range(1,i+1):
|
||||
a[i,j] = (a[i,j-1] - a[i-1,j-1]) / (x[i] - x[i-j]) # (links daneben - links darüber) / (x[zeile] - x[zeile - spalte])
|
||||
|
||||
return a
|
||||
|
||||
def newton_interpolation(a, data, x):
|
||||
n = len(a)
|
||||
|
||||
p = np.zeros(len(x))
|
||||
|
||||
for i in range(1,n+1):
|
||||
p = a[n-i] + (x - data[n-i]) * p # Horner Schema; '-' and '*' are overloaded for numpy arrays: so at least one argument 'data' or 'x' has to be numpy array
|
||||
|
||||
return p
|
||||
|
||||
####################################################################################################################################################################
|
||||
n = 12
|
||||
|
||||
x = np.linspace(-1, 1, 200)
|
||||
x_e = np.linspace(-1, 1, n) # equidistant grid points
|
||||
|
||||
# Chebyshev grid points
|
||||
x_c = np.zeros(n)
|
||||
for i in range(0,n):
|
||||
x_c[i] = np.cos((2 * i + 1) * np.pi / (2 * n))
|
||||
|
||||
f = runge(x)
|
||||
y_e = runge(x_e) # values for grid points for interpolation with equidistant grid points
|
||||
y_c = runge(x_c) # values for grid points for interpolation with Chebyshev grid points
|
||||
|
||||
# Interpolation with equidistant grid points and evaluation of interpolated values at x
|
||||
a_e = np.diag(divided_differences(x_e, y_e))
|
||||
p_e = newton_interpolation(a_e, x_e, x)
|
||||
|
||||
# Interpolation with Chebyshev grid points and evaluation of interpolated values at x
|
||||
a_c = np.diag(divided_differences(x_c, y_c))
|
||||
p_c = newton_interpolation(a_c, x_c, x)
|
||||
|
||||
|
||||
# Plotting of Runge and the two interpolated polynomials
|
||||
plt.rcParams['text.usetex'] = True
|
||||
|
||||
plt.plot(x, f, label=r"$f(x) = \frac{1}{1 + 25 x^2}$")
|
||||
plt.plot(x, p_e, label=r"$p_n(x)$ equidistant")
|
||||
plt.plot(x, p_c, label=r"$p_n(x)$ Chebyshev")
|
||||
plt.plot([], [], ' ', label="$n = {}$".format(n))
|
||||
|
||||
plt.title("A3)")
|
||||
plt.xlabel("x")
|
||||
plt.ylabel("y")
|
||||
plt.legend()
|
||||
|
||||
plt.show()
|
||||
|
||||
# braendel@math.tu-freiberg.de
|
Reference in New Issue
Block a user