v-Ptグラフをプロットしたいがx and y must have same first dimension, but have shapes (10000,) and (1,)のエラーが出る
python
1from scipy.special import kv 2import matplotlib.pyplot as plt 3from scipy.integrate import quad 4import numpy as np 5import math 6from math import gamma 7from sympy import * 8import os 9 10xs=np.logspace(-4, 2, 10000) 11ys=np.logspace(0, 5, 10000) 12vs=np.logspace(0,5,10000) 13 14m=9.10938356*1e-31 15c=2.99792458*1e+8 16q=1.6021766208*1e-19 17pa=np.pi/2#ピッチ角# 18sin_pa=math.sin(pa) 19B1=100*1e-6*1e-4 20a = gamma(1/3) 21def v_c(y): 22 return 3*y**2*q*B1*sin_pa/(4*np.pi*m*c) 23 24f = lambda z: kv(5/3,z) 25F = [quad(f,x,np.inf)[0]*x for x in xs] 26G = [(4*np.pi/np.sqrt(3)/a)*(x/2)**(1/3) for x in xs] 27H = [((np.pi/2)**(1/2))*(x**(1/2))*(np.exp(-x))for x in xs] 28 29def A(x,y,F,G,H): 30 if x <= 5.0*1e-3: 31 return G(x,y) 32 elif 5.0*1e-3 < x < 30: 33 return F(x,y) 34 elif 30 <= x: 35 return H(x,y) 36 37vec_A = np.vectorize(A) 38def L(x,y): 39 return vec_A(xs,ys,F,G,H) 40 41def P(v,y): 42 x=v/v_c 43 return (math.sqrt(3)*q**3*B1*sin_pa/(m*c**2))*L(v/v_c) 44 45gmin=1 46gmax=10**5 47N=10 48p=-2 49 50def R(v,y): 51 return N*y**-p*P 52 53def Pt(v): 54 return [quad(lambda y:R, gmin, gmax)[0] for y in ys] 55 56 57fig = plt.figure() 58ax = fig.add_subplot(1,1,1) 59ax.grid 60ax.set_yscale('log') 61ax.set_xscale('log') 62ax.plot(vs,Pt) 63 64plt.show()
エラー
python
1ValueError Traceback (most recent call last) 2<ipython-input-5-33ed3b99f09b> in <module> 3 60 ax.set_yscale('log') 4 61 ax.set_xscale('log') 5---> 62 ax.plot(vs,Pt) 6 63 7 64 plt.show() 8 9C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs) 10 1663 """ 11 1664 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D._alias_map) 12-> 1665 lines = [*self._get_lines(*args, data=data, **kwargs)] 13 1666 for line in lines: 14 1667 self.add_line(line) 15 16C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in __call__(self, *args, **kwargs) 17 223 this += args[0], 18 224 args = args[1:] 19--> 225 yield from self._plot_args(this, kwargs) 20 226 21 227 def get_next_color(self): 22 23C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs) 24 389 x, y = index_of(tup[-1]) 25 390 26--> 391 x, y = self._xy_from_xy(x, y) 27 392 28 393 if self.command == 'plot': 29 30C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _xy_from_xy(self, x, y) 31 268 if x.shape[0] != y.shape[0]: 32 269 raise ValueError("x and y must have same first dimension, but " 33--> 270 "have shapes {} and {}".format(x.shape, y.shape)) 34 271 if x.ndim > 2 or y.ndim > 2: 35 272 raise ValueError("x and y can be no greater than 2-D, but have " 36 37ValueError: x and y must have same first dimension, but have shapes (10000,) and (1,)
書き換えてみたけど別のエラーが出る。
python
1from scipy.special import kv 2import matplotlib.pyplot as plt 3from scipy.integrate import quad 4import numpy as np 5import math 6from math import gamma 7from sympy import * 8import os 9 10xs=np.logspace(-4, 2, 10000) 11 12 13m=9.10938356*1e-31 14c=2.99792458*1e+8 15q=1.6021766208*1e-19 16pa=np.pi/2 17sin_pa=math.sin(pa) 18B1=100*1e-6*1e-4 19a = gamma(1/3) 20 21f = lambda z: kv(5/3,z) 22F = [quad(f,x,np.inf)[0]*x for x in xs] 23G = [(4*np.pi/np.sqrt(3)/a)*(x/2)**(1/3) for x in xs] 24H = [((np.pi/2)**(1/2))*(x**(1/2))*(np.exp(-x))for x in xs] 25 26def A(x,F,G,H): 27 if x <= 5.0*1e-3: 28 return G(x,y) 29 elif 5.0*1e-3 < x < 30: 30 return F(x,y) 31 elif 30 <= x: 32 return H(x,y) 33 34vec_A = np.vectorize(A) 35def L(x): 36 return vec_A(xs,F,G,H) 37 38def P(v,y): 39 return (math.sqrt(3)*q**3*B1*sin_pa/(m*c**2))*L(v/v_c) 40 41gmin=1 42gmax=10**5 43N=10/(10**5-1) 44p=-2 45 46 47def s(x): 48 b=L*x**(-1/2) 49 return b 50 51def x1(v): 52 return 4*np.pi*m*c*v/(3*q*B1*sin_pa*gmin**2) 53def x2(v): 54 return 4*np.pi*m*c*v/(3*q*B1*sin_pa*gmax**2) 55 56def b(v): 57 b1=-1/math.sqrt(v) 58 b2=(math.sqrt(3)*q**3*B1*sin_pa/(2*m*c**2)) 59 b3=math.sqrt(3*q*B1*sin_pa/(4*np.pi*m*c)) 60 return b1*b2*b3*N 61 62def Pt(v): 63 a=[quad(s,x1(v),x2(v))[0] for x in xs] 64 return b*a[0] 65 66v=np.logspace(-4,2,10000) 67 68plt.plot(v,Pt(v)) 69plt.show() 70
エラー
python
1ValueError Traceback (most recent call last) 2<ipython-input-14-4d47f52aab3a> in <module> 3 66 v=np.logspace(-4,2,10000) 4 67 5---> 68 plt.plot(v,Pt(v)) 6 69 plt.show() 7 70 8 9<ipython-input-14-4d47f52aab3a> in Pt(v) 10 61 11 62 def Pt(v): 12---> 63 a=[quad(s,x1(v),x2(v))[0] for x in xs] 13 64 return b*a[0] 14 65 15 16<ipython-input-14-4d47f52aab3a> in <listcomp>(.0) 17 61 18 62 def Pt(v): 19---> 63 a=[quad(s,x1(v),x2(v))[0] for x in xs] 20 64 return b*a[0] 21 65 22 23C:\ProgramData\Anaconda3\lib\site-packages\scipy\integrate\quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst) 24 336 25 337 # check the limits of integration: \int_a^b, expect a < b 26--> 338 flip, a, b = b < a, min(a, b), max(a, b) 27 339 28 340 if weight is None: 29 30ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。