我不知道我怎麼會在下面的代碼Streamplot在Python錯誤
import numpy as np
import matplotlib.pyplot as plt
from sympy.functions.special.polynomials import assoc_legendre
from scipy.misc import factorial, derivative
import sympy as sym
def main():
t = 36000
a=637000000
H=200
g=9.81
x = sym.symbols('x')
for l in range(1, 6):
ω=np.sqrt(g*H*l*(l+1))/a
for n in range(l+1):
nθ, nφ = 128, 256
θ, φ = np.linspace(0, np.pi, nθ), np.linspace(0, 2*np.pi, nφ)
legfun_sym = sym.functions.special.polynomials.assoc_legendre(l, n, x)
legfun_num = sym.lambdify(x,legfun_sym)
X, Y = np.meshgrid(θ, φ)
uθ = (g/(a*ω))*Der_Assoc_Legendre(legfun_num, l, n, X)*np.sin(n*Y-ω*t)
uφ = (g/(a*ω*np.sin(X)))*Assoc_Legendre(l, n, X)*np.cos(n*Y-ω*t)
#speed = np.sqrt(uθ**2 + uφ**2)
fig0, ax = plt.subplots()
strm = ax.streamplot(φ, θ, uφ, uθ, linewidth=2, cmap=plt.cm.autumn)
fig0.colorbar(strm.lines)
plt.show()
def Assoc_Legendre(m, n, X):
L=[]
for i in X:
k=[]
for j in i:
k.append(assoc_legendre(m, n, np.cos(j)))
L.append(k)
return np.array(L)
def Der_Assoc_Legendre(legfun_num, m, n, X):
L=[]
for i in X:
k=[]
for j in i:
k.append(derivative(legfun_num, j, dx=1e-7))
L.append(k)
return np.array(L)
if __name__=='__main__':
main()
錯誤消息'u' and 'v' must be of shape 'Grid(x,y)'
又發表了關於strm = ax.streamplot(φ, θ, uφ, uθ, linewidth=2, cmap=plt.cm.autumn)
線修復錯誤。我應該如何解決這個問題?
作爲參考,我試圖做一個$ u _ {\ theta} $和$ u _ {\ phi} $的流圖,其中$ u _ {\ theta} = \ frac {g} {\ omega a} \ frac {d} {d \ theta} \ left(P^n_l \ left(cos \ theta \ right)\ right)sin \ left(n \ phi \ωmega\ right)$ and $ u _ {\ phi} = \壓裂{GN} {\歐米加罪過\ THETA} p^N_L \左(COS \ THETA \右)COS \左(N \ PHI-\歐米加噸\右)$
編輯:
這是我現有的代碼:
import numpy as np
import matplotlib.pyplot as plt
from sympy.functions.special.polynomials import assoc_legendre
from scipy.misc import factorial, derivative
import sympy as sym
def main():
t = 36000
a=637000000
H=200
g=9.81
x = sym.symbols('x')
X, Y = np.mgrid[0.01:np.pi-0.01:100j,0:2*np.pi:100j]
for l in range(1, 6):
ω=np.sqrt(g*H*l*(l+1))/a
for n in range(l+1):
#nθ, nφ = 128, 256
#θ, φ = np.linspace(0.001, np.pi-0.001, nθ), np.linspace(0, 2*np.pi, nφ)
legfun_sym = sym.functions.special.polynomials.assoc_legendre(l, n, x)
legfun_num = sym.lambdify(x, legfun_sym)
uθ = (g/(a*ω*np.sin(X)))*Der_Assoc_Legendre(legfun_num, l, n, X)*np.sin(n*Y-ω*t)
uφ = (g/(a*ω))*Assoc_Legendre(l, n, X)*np.cos(n*Y-ω*t)
#speed = np.sqrt(uθ**2 + uφ**2)
fig0, ax = plt.subplots()
strm = ax.streamplot(Y, X, uθ,uφ, linewidth=0.5, cmap=plt.cm.autumn)
#fig0.colorbar(strm.lines)
plt.show()
print("next")
def Assoc_Legendre(m, n, X):
L=[]
for i in X:
k=[]
for j in i:
k.append(assoc_legendre(m, n, np.cos(j)))
L.append(k)
return np.float64(np.array(L))
def Der_Assoc_Legendre(legfun_num, m, n, X):
L=[]
for i in X:
k=[]
for j in i:
k.append(derivative(legfun_num, j, dx=0.001))
L.append(k)
return np.float64(np.array(L))
if __name__=='__main__':
main()
當前問題似乎與中的衍生函數,在繪製第一個繪圖並繪製到第二個繪圖後出現錯誤ValueError: math domain error
。
儘管'np.sin(X)'在0和$ \ pi $處的劃分不是很好,但在我看來'dtype'與函數'Assoc_Legendre'和'Der_Assoc_Legendre'有差別。我不太清楚如何修復這個 – Jelmes
@Jelmes你是對的,我不知何故錯過了只有有問題的人才用sympy來計算。因此改爲'append(float(assoc_legendre(...)))',但是你仍然會有無窮無盡(我們將會看到'streamplot'如何處理這些)。 –
PS謝謝;) –