diff --git a/introduction/serie_2.py b/introduction/serie_2.py index c86393f..cfaf9c8 100644 --- a/introduction/serie_2.py +++ b/introduction/serie_2.py @@ -1,14 +1,15 @@ -#%% +# %% import numpy as np # %% print("Aufgabe 4") + def fixpunkt_iteration(f, x=1.0, tol=1e-7, N=1000): x_new = f(x) i = 0 - while(np.abs(x_new-x) > tol): + while np.abs(x_new - x) > tol: if i > N: raise RuntimeError("Keine Konvergenz erreicht") @@ -17,18 +18,21 @@ def fixpunkt_iteration(f, x=1.0, tol=1e-7, N=1000): i += 1 return [x_new, i] + # Ausgabe print("----------------------------------------------------------------------") print(__file__) print("----------------------------------------------------------------------") -f = lambda x: 1-(1/5)*x +f = lambda x: 1 - (1 / 5) * x 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") + print( + f"a) f(x) = 1-(1/5)*x\nFixpunkt: {fixpunkt_a}\nIterationen: {iterationen_a}\n" + ) f = np.cos try: @@ -47,3 +51,38 @@ else: print(f"c) f(x) = e⁻x\nFixpunkt: {fixpunkt_c}\nIterationen: {iterationen_c}\n") print("----------------------------------------------------------------------") + +##% +# Parameter +q = 2.0 +a_0 = 1.0 +pr = 16 + + +# Funktionen +def f(x): + y = 0.5 * (x + q / x) + return y + + +# Iteration +w = 0.0 +a = a_0 +k = 0 + +print("----------------------------------------------------------------------") +print(__file__) +print("----------------------------------------------------------------------") +print("Iteration :") +print(f"a_{k} = {a:#.16g}") + +while a != w: + w = a + k = k + 1 + a = f(a) + print(f"a_{k} = {a:#.16g}") + +# Ausgabe : +print("----------------------------------------------------------------------") +print(f"Wurzel: sqrt({q:#.{pr}g}) = {a:#.{pr}g}") +print("----------------------------------------------------------------------")