2.13

import numpy as np
from scipy.optimize import least_squares

def f(x):
return (np.abs(x + 1) - np.abs(x - 1)) / 2 + np.sin(x)

def g(x):
return (np.abs(x + 3) - np.abs(x - 3)) / 2 + np.cos(x)

def equations(variables):
x1, x2, y1, y2 = variables[:4]
eq1 = 2 * x1 - (3 * f(y1) + 4 * g(y2) - 1)
eq2 = 3 * x2 - (2 * f(y1) + 6 * g(y2) - 2)
eq3 = y1 - (f(x1) + 3 * g(x2) - 3)
eq4 = 5 * y2 - (4 * f(x1) + 6 * g(x2) - 1)
eq5 = x1 + y1 - (f(y2) + g(x2) - 2)
eq6 = x2 - 3 * y2 - (2 * f(x1) - 10 * g(y1) - 5)

return [eq1, eq2, eq3, eq4, eq5, eq6]

initial_guess = [0, 0, 0, 0]

result = least_squares(equations, initial_guess)
numeric_solution = result.x
print("最小二乘解:", numeric_solution)

3022

posted @ 2024-10-21 21:16  Tsuki*  阅读(4)  评论(0编辑  收藏  举报