I was wondering how I could fix the error in the following code
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()
The error message 'u' and 'v' must be of shape 'Grid(x,y)' comes up with regard to the strm = ax.streamplot(φ, θ, uφ, uθ, linewidth=2, cmap=plt.cm.autumn) line. How should I fix this?
For reference, I am trying to do a streamplot of $u_{\theta}$ and $u_{\phi}$, where $u_{\theta}=\frac{g}{\omega a}\frac{d}{d\theta}\left(P^n_l\left(cos\theta\right)\right)sin\left(n\phi-\omega t\right)$ and $u_{\phi}=\frac{gn}{\omega a sin\theta}P^n_l\left(cos\theta\right)cos\left(n\phi-\omega t\right)$
EDIT:
This is the current code I have:
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()
The current issue seems to be with the derivative function in Der_Assoc_Legendre, that brings up the error ValueError: math domain error after plotting the first plot and onto the second.