feat: aufgabe 4 abgeschlossen
This commit is contained in:
parent
fe8866f1df
commit
b0f13399bb
@ -4,32 +4,46 @@ import numpy as np
|
|||||||
# %%
|
# %%
|
||||||
print("Aufgabe 4")
|
print("Aufgabe 4")
|
||||||
|
|
||||||
def fixpunkt_iteration(f, x, tol=1e-7):
|
def fixpunkt_iteration(f, x=1.0, tol=1e-7, N=1000):
|
||||||
x_new = f(x)
|
x_new = f(x)
|
||||||
i = 0
|
i = 0
|
||||||
|
|
||||||
while(np.abs(x_new-x) > tol):
|
while(np.abs(x_new-x) > tol):
|
||||||
|
if i > N:
|
||||||
|
raise RuntimeError("Keine Konvergenz erreicht")
|
||||||
|
|
||||||
x = x_new
|
x = x_new
|
||||||
x_new = f(x)
|
x_new = f(x)
|
||||||
i += 1
|
i += 1
|
||||||
return [x_new, i]
|
return [x_new, i]
|
||||||
|
|
||||||
x0 = 1.0
|
|
||||||
|
|
||||||
f = lambda x: 1-(1/5)*x
|
|
||||||
fixpunkt_a, iterationen_a = fixpunkt_iteration(f, x0)
|
|
||||||
|
|
||||||
f = np.cos
|
|
||||||
fixpunkt_b, iterationen_b = fixpunkt_iteration(f, x0)
|
|
||||||
|
|
||||||
f = lambda x: np.e**-x
|
|
||||||
fixpunkt_c, iterationen_c = fixpunkt_iteration(f, x0)
|
|
||||||
|
|
||||||
# Ausgabe
|
# Ausgabe
|
||||||
print("----------------------------------------------------------------------")
|
print("----------------------------------------------------------------------")
|
||||||
print(__file__)
|
print(__file__)
|
||||||
print("----------------------------------------------------------------------")
|
print("----------------------------------------------------------------------")
|
||||||
print(f"a) f(x) = 1-(1/5)*x\nFixpunkt: {fixpunkt_a}\nIterationen: {iterationen_a}\n")
|
|
||||||
print(f"b) f(x) = cos(x)\nFixpunkt: {fixpunkt_b}\nIterationen: {iterationen_b}\n")
|
f = lambda x: 1-(1/5)*x
|
||||||
print(f"c) f(x) = e⁻x\nFixpunkt: {fixpunkt_c}\nIterationen: {iterationen_c}\n")
|
try:
|
||||||
|
fixpunkt_a, iterationen_a = fixpunkt_iteration(f)
|
||||||
|
except RuntimeError as error:
|
||||||
|
print(f"a) f(x) = 1-(1/5)*x\n{error}")
|
||||||
|
else:
|
||||||
|
print(f"a) f(x) = 1-(1/5)*x\nFixpunkt: {fixpunkt_a}\nIterationen: {iterationen_a}\n")
|
||||||
|
|
||||||
|
f = np.cos
|
||||||
|
try:
|
||||||
|
fixpunkt_b, iterationen_b = fixpunkt_iteration(f)
|
||||||
|
except RuntimeError as error:
|
||||||
|
print(f"b) f(x) = cos(x)\n{error}")
|
||||||
|
else:
|
||||||
|
print(f"b) f(x) = cos(x)\nFixpunkt: {fixpunkt_b}\nIterationen: {iterationen_b}\n")
|
||||||
|
|
||||||
|
f = lambda x: np.e**-x
|
||||||
|
try:
|
||||||
|
fixpunkt_c, iterationen_c = fixpunkt_iteration(f)
|
||||||
|
except RuntimeError as error:
|
||||||
|
print(f"c) f(x) = e⁻x\n{error}")
|
||||||
|
else:
|
||||||
|
print(f"c) f(x) = e⁻x\nFixpunkt: {fixpunkt_c}\nIterationen: {iterationen_c}\n")
|
||||||
|
|
||||||
print("----------------------------------------------------------------------")
|
print("----------------------------------------------------------------------")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user