From d39e3fcff0773e4335c7c8aee7a418584784d61d Mon Sep 17 00:00:00 2001 From: zimmersandro Date: Thu, 2 Apr 2026 17:41:56 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20Skript=20mit=20Newton=20Verfahren,=20ei?= =?UTF-8?q?nfach=20und=20modifiziert.=20build:=20sympy=20zu=20requirements?= =?UTF-8?q?=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 3 +- src/optimierung/newton_verfahren.py | 102 ++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/optimierung/newton_verfahren.py diff --git a/requirements.txt b/requirements.txt index 4c5bca9..5011da1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ matplotlib == 3.10.8 numpy == 2.4.3 black == 26.3.1 -ipykernel == 7.2.0 \ No newline at end of file +ipykernel == 7.2.0 +sympy == 1.14.0 \ No newline at end of file diff --git a/src/optimierung/newton_verfahren.py b/src/optimierung/newton_verfahren.py new file mode 100644 index 0000000..c34ef13 --- /dev/null +++ b/src/optimierung/newton_verfahren.py @@ -0,0 +1,102 @@ +#%% +import numpy as np +import sympy as sp +import matplotlib.pyplot as plt + +#%% +# "Einfacher" Newton Verfahren +# Parameter +N = 200 +x = sp.Symbol('x') + +# Funktion +f_sym = sp.sin(3*x)+0.02*x**2 + +# Ableitungen +f = sp.lambdify(x, f_sym, "numpy") +f_prime = sp.lambdify(x, sp.diff(f_sym, x), "numpy") +f_second = sp.lambdify(x, sp.diff(f_sym, x, 2), "numpy") + +# Daten +x_data = np.linspace(0,10,N) +f_data = f(x_data) + +# Plot +plt.figure(1) +plt.plot(x_data, f_data) +plt.xlabel("x") +plt.ylabel("y") +plt.grid("on") +plt.axis("image") + +# Newton Verfahren +# Startwert +startwerte = [6.6, 3.4, 8.38] + +# Iterationsformel +x_n = lambda x: x - f_prime(x)/f_second(x) + +# Iteration +for x_0 in startwerte: + n = 0 + x_i = x_0 + f_x_prime = f_prime(x_0) + print(f"n: {n}\nf(x): {x_0}\nf'(x): {f_x_prime}\n") + + limit = 4 + + while np.abs(f_x_prime) > 1e-16 and limit > 0: + n += 1 + x_i = x_n(x_i) + f_x_prime = f_prime(x_i) + print(f"n: {n}\nf(x): {x_i}\nf'(x): {f_x_prime}\n") + limit -= 1 + +#%% +# "Modifizierter" Newton Verfahren +# Parameter +N = 200 +x = sp.Symbol('x') + +# Funktion +f_sym = sp.sin(3*x)+0.02*x**2 + +# Ableitungen +f = sp.lambdify(x, f_sym, "numpy") +f_prime = sp.lambdify(x, sp.diff(f_sym, x), "numpy") +f_second = sp.lambdify(x, sp.diff(f_sym, x, 2), "numpy") + +# Daten +x_data = np.linspace(0,10,N) +f_data = f(x_data) + +# Plot +plt.figure(1) +plt.plot(x_data, f_data) +plt.xlabel("x") +plt.ylabel("y") +plt.grid("on") +plt.axis("image") + +# Newton Verfahren +# Startwert +startwerte = [6.6, 3.4, 8.38] + +# Iterationsformel +x_n = lambda x: x - f_prime(x)/np.abs(f_second(x)) + +# Iteration +for x_0 in startwerte: + n = 0 + x_i = x_0 + f_x_prime = f_prime(x_0) + print(f"n: {n}\nf(x): {x_0}\nf'(x): {f_x_prime}\n") + + limit = 4 + + while np.abs(f_x_prime) > 1e-16 and limit > 0: + n += 1 + x_i = x_n(x_i) + f_x_prime = f_prime(x_i) + print(f"n: {n}\nf(x): {x_i}\nf'(x): {f_x_prime}\n") + limit -= 1 \ No newline at end of file