sympy方程的混合解
Mixing solutions of sympy equation
我正在求解具有以下形式的时间相关方程(求 \lambda 的根):
import sympy as sp
import matplotlib.pyplot as plt
t = sp.symbols(r't', real=True, positive=True)
eq = ...
print(repr(eq))
-\lambda**3 - 2*\lambda**2*t + 4*\lambda*t**4 - 8*\lambda*t**3 + 8*\lambda*t**2 - 8*\lambda*t + 4*\lambda + 8*t**3 - 16*t**2 + 8*t
求解方程并将根保存到列表中:
sol = sp.solve(eq)
e_list = [list(sol[i].values())[0] for i in range(len(sol))]
明确显示它们的演变:
x =e_list[0]
lam_x = sp.lambdify(t, x, modules=['numpy'])
x_vals = np.linspace(0.01, .9999, 1000, dtype=complex)
y_vals = np.around(lam_x(x_vals),decimals=5)
plt.plot(np.real(x_vals), np.real(y_vals),label='r1')
x =e_list[1]
lam_x = sp.lambdify(t, x, modules=['numpy'])
x_vals = np.linspace(0.01, .9999, 1000, dtype=complex)
y_vals = np.around(lam_x(x_vals),decimals=5)
plt.plot(np.real(x_vals), np.real(y_vals),label='r2')
x =e_list[2]
lam_x = sp.lambdify(t, x, modules=['numpy'])
x_vals = np.linspace(0.01, .9999, 1000, dtype=complex)
y_vals = np.around(lam_x(x_vals),decimals=5)
plt.plot(np.real(x_vals), np.real(y_vals),label='r3')
plt.legend()
plt.show()
但由于某些原因,两个第一根的解在 ~ .72
之前混合了。我不知道如何纠正这个,然后不要混合
Numpy 会出现这种行为,因为您要进行复杂的分支切割。在这种情况下,您应该使用 Mpmath 作为 lambdify
的评估模块,它以不同方式处理分支切割。例如:
import numpy as np
import matplotlib.pyplot as plt
x =e_list[0]
lam_x = lambdify(t, x, modules=['mpmath'])
x_vals = np.linspace(0.01, .9999, 1000, dtype=complex)
y_vals = []
for _x in x_vals:
y_vals.append(complex(lam_x(_x)))
plt.figure()
plt.plot(np.real(x_vals), np.real(y_vals),label='r1')
x =e_list[1]
lam_x = lambdify(t, x, modules=['mpmath'])
x_vals = np.linspace(0.01, .9999, 1000, dtype=complex)
y_vals = []
for _x in x_vals:
y_vals.append(complex(lam_x(_x)))
plt.plot(np.real(x_vals), np.real(y_vals),label='r2')
x =e_list[2]
lam_x = lambdify(t, x, modules=['numpy'])
x_vals = np.linspace(0.01, .9999, 1000, dtype=complex)
y_vals = np.around(lam_x(x_vals),decimals=5)
plt.plot(np.real(x_vals), np.real(y_vals),label='r3')
plt.legend()
plt.show()
我正在求解具有以下形式的时间相关方程(求 \lambda 的根):
import sympy as sp
import matplotlib.pyplot as plt
t = sp.symbols(r't', real=True, positive=True)
eq = ...
print(repr(eq))
-\lambda**3 - 2*\lambda**2*t + 4*\lambda*t**4 - 8*\lambda*t**3 + 8*\lambda*t**2 - 8*\lambda*t + 4*\lambda + 8*t**3 - 16*t**2 + 8*t
求解方程并将根保存到列表中:
sol = sp.solve(eq)
e_list = [list(sol[i].values())[0] for i in range(len(sol))]
明确显示它们的演变:
x =e_list[0]
lam_x = sp.lambdify(t, x, modules=['numpy'])
x_vals = np.linspace(0.01, .9999, 1000, dtype=complex)
y_vals = np.around(lam_x(x_vals),decimals=5)
plt.plot(np.real(x_vals), np.real(y_vals),label='r1')
x =e_list[1]
lam_x = sp.lambdify(t, x, modules=['numpy'])
x_vals = np.linspace(0.01, .9999, 1000, dtype=complex)
y_vals = np.around(lam_x(x_vals),decimals=5)
plt.plot(np.real(x_vals), np.real(y_vals),label='r2')
x =e_list[2]
lam_x = sp.lambdify(t, x, modules=['numpy'])
x_vals = np.linspace(0.01, .9999, 1000, dtype=complex)
y_vals = np.around(lam_x(x_vals),decimals=5)
plt.plot(np.real(x_vals), np.real(y_vals),label='r3')
plt.legend()
plt.show()
但由于某些原因,两个第一根的解在 ~ .72
之前混合了。我不知道如何纠正这个,然后不要混合
Numpy 会出现这种行为,因为您要进行复杂的分支切割。在这种情况下,您应该使用 Mpmath 作为 lambdify
的评估模块,它以不同方式处理分支切割。例如:
import numpy as np
import matplotlib.pyplot as plt
x =e_list[0]
lam_x = lambdify(t, x, modules=['mpmath'])
x_vals = np.linspace(0.01, .9999, 1000, dtype=complex)
y_vals = []
for _x in x_vals:
y_vals.append(complex(lam_x(_x)))
plt.figure()
plt.plot(np.real(x_vals), np.real(y_vals),label='r1')
x =e_list[1]
lam_x = lambdify(t, x, modules=['mpmath'])
x_vals = np.linspace(0.01, .9999, 1000, dtype=complex)
y_vals = []
for _x in x_vals:
y_vals.append(complex(lam_x(_x)))
plt.plot(np.real(x_vals), np.real(y_vals),label='r2')
x =e_list[2]
lam_x = lambdify(t, x, modules=['numpy'])
x_vals = np.linspace(0.01, .9999, 1000, dtype=complex)
y_vals = np.around(lam_x(x_vals),decimals=5)
plt.plot(np.real(x_vals), np.real(y_vals),label='r3')
plt.legend()
plt.show()