serie 03
This commit is contained in:
BIN
Kuengjoe_S01.zip
BIN
Kuengjoe_S01.zip
Binary file not shown.
BIN
Kuengjoe_S03/Kuengjoe_S03_Aufg1.pdf
Normal file
BIN
Kuengjoe_S03/Kuengjoe_S03_Aufg1.pdf
Normal file
Binary file not shown.
92
Kuengjoe_S03/Kuengjoe_S03_Aufg3.py
Normal file
92
Kuengjoe_S03/Kuengjoe_S03_Aufg3.py
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
def function_values(x1_value, x2_value, x3_value):
|
||||||
|
first_function_value = x1_value + x2_value**2 - x3_value**2 - 13
|
||||||
|
second_function_value = np.log(x2_value**4 / 4) + np.exp(0.5 * x3_value - 1) - 1
|
||||||
|
third_function_value = (x2_value - 3)**2 - x3_value**3 + 7
|
||||||
|
|
||||||
|
return np.array([
|
||||||
|
first_function_value,
|
||||||
|
second_function_value,
|
||||||
|
third_function_value
|
||||||
|
], dtype=float)
|
||||||
|
|
||||||
|
|
||||||
|
def jacobian_matrix(x1_value, x2_value, x3_value):
|
||||||
|
derivative_f1_x1 = 1
|
||||||
|
derivative_f1_x2 = 2 * x2_value
|
||||||
|
derivative_f1_x3 = -2 * x3_value
|
||||||
|
|
||||||
|
derivative_f2_x1 = 0
|
||||||
|
derivative_f2_x2 = 4 / x2_value
|
||||||
|
derivative_f2_x3 = 0.5 * np.exp(0.5 * x3_value - 1)
|
||||||
|
|
||||||
|
derivative_f3_x1 = 0
|
||||||
|
derivative_f3_x2 = 2 * (x2_value - 3)
|
||||||
|
derivative_f3_x3 = -3 * x3_value**2
|
||||||
|
|
||||||
|
return np.array([
|
||||||
|
[derivative_f1_x1, derivative_f1_x2, derivative_f1_x3],
|
||||||
|
[derivative_f2_x1, derivative_f2_x2, derivative_f2_x3],
|
||||||
|
[derivative_f3_x1, derivative_f3_x2, derivative_f3_x3]
|
||||||
|
], dtype=float)
|
||||||
|
|
||||||
|
|
||||||
|
def damped_newton_method(start_vector, tolerance=1e-5, maximum_iterations=100):
|
||||||
|
current_vector = np.array(start_vector, dtype=float)
|
||||||
|
|
||||||
|
for iteration_index in range(maximum_iterations):
|
||||||
|
current_function_values = function_values(
|
||||||
|
current_vector[0],
|
||||||
|
current_vector[1],
|
||||||
|
current_vector[2]
|
||||||
|
)
|
||||||
|
|
||||||
|
current_function_norm = np.linalg.norm(current_function_values, 2)
|
||||||
|
|
||||||
|
if current_function_norm < tolerance:
|
||||||
|
return current_vector, iteration_index, current_function_norm
|
||||||
|
|
||||||
|
current_jacobian_matrix = jacobian_matrix(
|
||||||
|
current_vector[0],
|
||||||
|
current_vector[1],
|
||||||
|
current_vector[2]
|
||||||
|
)
|
||||||
|
|
||||||
|
newton_step = np.linalg.solve(current_jacobian_matrix, -current_function_values)
|
||||||
|
|
||||||
|
damping_factor = 1.0
|
||||||
|
|
||||||
|
while damping_factor > 1e-8:
|
||||||
|
candidate_vector = current_vector + damping_factor * newton_step
|
||||||
|
candidate_function_values = function_values(
|
||||||
|
candidate_vector[0],
|
||||||
|
candidate_vector[1],
|
||||||
|
candidate_vector[2]
|
||||||
|
)
|
||||||
|
|
||||||
|
if np.linalg.norm(candidate_function_values, 2) < current_function_norm:
|
||||||
|
current_vector = candidate_vector
|
||||||
|
break
|
||||||
|
|
||||||
|
damping_factor = damping_factor / 2
|
||||||
|
|
||||||
|
final_function_values = function_values(
|
||||||
|
current_vector[0],
|
||||||
|
current_vector[1],
|
||||||
|
current_vector[2]
|
||||||
|
)
|
||||||
|
final_function_norm = np.linalg.norm(final_function_values, 2)
|
||||||
|
|
||||||
|
return current_vector, maximum_iterations, final_function_norm
|
||||||
|
|
||||||
|
|
||||||
|
start_vector = [1.5, 3.0, 2.5]
|
||||||
|
|
||||||
|
solution_vector, number_of_iterations, final_norm = damped_newton_method(start_vector)
|
||||||
|
|
||||||
|
print("Start vector =", start_vector)
|
||||||
|
print("Approximate solution =", solution_vector)
|
||||||
|
print("Iterations =", number_of_iterations)
|
||||||
|
print("||f(x^(k))||_2 =", final_norm)
|
||||||
93
Kuengjoe_S03/Kuengjoe_S03_aufg2.py
Normal file
93
Kuengjoe_S03/Kuengjoe_S03_aufg2.py
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
import numpy as np
|
||||||
|
import sympy as sp
|
||||||
|
|
||||||
|
|
||||||
|
def function_vector(x_value, y_value):
|
||||||
|
first_function_value = (x_value ** 2) / (186 ** 2) - (y_value ** 2) / (300 ** 2 - 186 ** 2) - 1
|
||||||
|
second_function_value = ((y_value - 500) ** 2) / (279 ** 2) - ((x_value - 300) ** 2) / (500 ** 2 - 279 ** 2) - 1
|
||||||
|
return np.array([first_function_value, second_function_value], dtype=float)
|
||||||
|
|
||||||
|
|
||||||
|
def jacobian_matrix(x_value, y_value):
|
||||||
|
jacobian_11 = (2 * x_value) / (186 ** 2)
|
||||||
|
jacobian_12 = (-2 * y_value) / (300 ** 2 - 186 ** 2)
|
||||||
|
|
||||||
|
jacobian_21 = (-2 * (x_value - 300)) / (500 ** 2 - 279 ** 2)
|
||||||
|
jacobian_22 = (2 * (y_value - 500)) / (279 ** 2)
|
||||||
|
|
||||||
|
return np.array([
|
||||||
|
[jacobian_11, jacobian_12],
|
||||||
|
[jacobian_21, jacobian_22]
|
||||||
|
], dtype=float)
|
||||||
|
|
||||||
|
|
||||||
|
def newton_method_for_system(start_vector, tolerance=1e-5, maximum_number_of_iterations=100):
|
||||||
|
current_vector = np.array(start_vector, dtype=float)
|
||||||
|
|
||||||
|
for iteration_index in range(maximum_number_of_iterations):
|
||||||
|
current_function_value = function_vector(current_vector[0], current_vector[1])
|
||||||
|
current_function_norm = np.linalg.norm(current_function_value, 2)
|
||||||
|
|
||||||
|
if current_function_norm < tolerance:
|
||||||
|
return current_vector, iteration_index, current_function_norm
|
||||||
|
|
||||||
|
current_jacobian_matrix = jacobian_matrix(current_vector[0], current_vector[1])
|
||||||
|
newton_step = np.linalg.solve(current_jacobian_matrix, -current_function_value)
|
||||||
|
current_vector = current_vector + newton_step
|
||||||
|
|
||||||
|
final_function_value = function_vector(current_vector[0], current_vector[1])
|
||||||
|
final_function_norm = np.linalg.norm(final_function_value, 2)
|
||||||
|
return current_vector, maximum_number_of_iterations, final_function_norm
|
||||||
|
|
||||||
|
|
||||||
|
def plot_implicit_functions():
|
||||||
|
x_symbol, y_symbol = sp.symbols('x y')
|
||||||
|
|
||||||
|
first_function_expression = x_symbol ** 2 / 186 ** 2 - y_symbol ** 2 / (300 ** 2 - 186 ** 2) - 1
|
||||||
|
second_function_expression = (y_symbol - 500) ** 2 / 279 ** 2 - (x_symbol - 300) ** 2 / (500 ** 2 - 279 ** 2) - 1
|
||||||
|
|
||||||
|
first_plot = sp.plot_implicit(
|
||||||
|
sp.Eq(first_function_expression, 0),
|
||||||
|
(x_symbol, -2000, 2000),
|
||||||
|
(y_symbol, -2000, 2000),
|
||||||
|
show=False
|
||||||
|
)
|
||||||
|
|
||||||
|
second_plot = sp.plot_implicit(
|
||||||
|
sp.Eq(second_function_expression, 0),
|
||||||
|
(x_symbol, -2000, 2000),
|
||||||
|
(y_symbol, -2000, 2000),
|
||||||
|
show=False
|
||||||
|
)
|
||||||
|
|
||||||
|
first_plot.append(second_plot[0])
|
||||||
|
first_plot.show()
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print("Plot of the two hyperbolas is opened...")
|
||||||
|
plot_implicit_functions()
|
||||||
|
|
||||||
|
# Hier die 4 Startvektoren aus dem Plot eintragen
|
||||||
|
start_vectors = [
|
||||||
|
np.array([-1300, 1600]),
|
||||||
|
np.array([750, 900]),
|
||||||
|
np.array([-200, 70]),
|
||||||
|
np.array([250, 220])
|
||||||
|
]
|
||||||
|
|
||||||
|
print("\nNewton-Verfahren für die 4 Startvektoren:\n")
|
||||||
|
|
||||||
|
for solution_index, current_start_vector in enumerate(start_vectors, start=1):
|
||||||
|
solution_vector, number_of_iterations, final_norm = newton_method_for_system(current_start_vector)
|
||||||
|
|
||||||
|
print(f"Lösung {solution_index}:")
|
||||||
|
print(f"startvektor = {current_start_vector}")
|
||||||
|
print(f" Näaherungslösung = ({solution_vector[0]:.10f}, {solution_vector[1]:.10f})")
|
||||||
|
print(f" Iterationen = {number_of_iterations}")
|
||||||
|
print(f" ||f(x^(k))||_2 = {final_norm:.10e}")
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user