【发布时间】:2022-10-18 19:50:31
【问题描述】:
具有如下两个耦合方程的系统:
two_exponential = lambda x, kernel, c: np.array([x[0] - np.exp(kernel[0] * x[0] + kernel[2] * x[1] + c), x[1] - np.exp(kernel[1] * x[1] + kernel[3] * x[0] + c)])
我想找到两条线与scipy.fsolve 的交点。
我这样做的方法是为b11,b22, b12, b21 的不同配置找到这个系统的根源。
b = np.array([b11, b22, b12, b21])
x_min_plot = -10
x_max_plot = 35
x_1 = np.linspace(x_min_plot, x_max_plot, 100)
x_2 = np.linspace(x_min_plot, x_max_plot, 100)
x_1, x_2 = np.meshgrid(x_1, x_2)
z_1 = -x_1 + np.exp(b[0] * x_1 + b[2] * x_2 + c)
z_2 = -x_2 + np.exp(b[1] * x_2 + b[3] * x_1 + c)
x_sols = []
x_min = 0
x_max = 35
for x in np.arange(x_min, x_max, 5):
for y in np.arange(x_min, x_max, 5):
initial = np.array([x, y])
x_sol = fsolve(two_exponential, initial, args=(b, c), full_output=1)
if x_sol[2] == 1: # if the solution converged
x_sols.append(np.round(x_sol[0], 2))
# [x for i, x in enumerate(x_sols) if not np.isclose(x, x_sols[i-1], atol = 1e-1).all()]
x_sols = np.unique(x_sols, axis=0)
print(f'z*: {np.round(x_sols, 2)}')
if x_sol[2] != 1:
print('no solution')
我还将解决方案四舍五入以忽略重复的根,因为我只想找到唯一的根。 该代码在某些情况下似乎可以正常工作:
但不适用于其他一些条件:
你知道这样的问题会从哪里出现吗?
【问题讨论】:
-
降低误差容限。当近似值足够好时,该方法将停止,如果图形在感兴趣区域中较浅,则可能会出错。
-
可以消除未知数 X1 或 X2 之一以获得单变量方程。
-
请注意,在您的两个示例中,解决方案接近渐近线。所以它们的交点可以做出很好的初始近似。
标签: python scipy numerical-methods