ts= numpy.linspace(0.,300.,1001) o.integrate(ts,MWPotential2014) o.plot3d(alpha=0.4) plt.xlim(-100.,100.) plt.ylim(-100.,100) plt.gca().set_zlim3d(-100.,100) plt.show()我怎么减少刻度数
时间: 2024-03-05 21:52:37 浏览: 111
你可以在调用 `numpy.linspace` 函数时,将第三个参数(即刻度数)设置为较小的值,例如:
```python
ts = numpy.linspace(0., 300., 501) # 将刻度数减少至 501
```
这样可以将刻度数减少一半,从而减少绘图时的计算量。如果这样还不够,你可以进一步将刻度数减小,但这可能会导致图像的精度降低。
另外,如果你想绘制更平滑的曲线,可以尝试增加 `numpy.linspace` 函数的第三个参数,从而增加刻度数。但是,增加刻度数可能会导致程序的运行速度变慢。
相关问题
import numpy as np import math from scipy import integrate def f(x): return math.sin(x)*math.sin(x) #复化梯形法 def func(a,b,n,f): x = np.linspace(a,b,n+1) sum1 = 0 h =(b-a)/n for i in range(补充代码1): 补充代码2 return sum1 #复化辛普森法 def func1(a,b,n,f): x = np.linspace(a,b,n+1) sum1 = 0 h =(b-a)/n for i in range(补充代码3): 补充代码4 return sum1 #复化科特斯法 def func2(a,b,n,f): x = np.linspace(a,b,n+1) sum1 = 0 h =(b-a)/n for i in range( 补充代码5 ): 补充代码6 return sum1 answer = func(0,1,100,f) answer1 = func1(0,1,100,f) answer2 = func2(0,1,100,f) print(answer,answer1,answer2) #integrate积分作为精确值 value, error = integrate.quad(f,0, 1) print(value,error) print("error: ", abs(answer-value), abs(answer1-value), abs(answer2-value))import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文 标签 plt.rcParams['axes.unicode_minus']=False #用来正常显示负号 f=lambda x:np.sin(x)*np.sin(x) #向前差商 def fo_df(x,h): plt.plot([x-h,x],[f(x-h),f(x)],'r--',label='前') result= 补充代码7 return result #向后差商 def back_df(x,h): plt.plot([x,x+h],[f(x),f(x+h)],'k--',label='后') result= 补充代码8 return result#中心差商 def cen_df(x,h): a=(f(x-h)+f(x+h))/2 plt.plot([x-h,x+h],[f(x-h)+f(x)-a,f(x+h)+f(x)-a],'g--',label='中') result= 补充代码9 return result xx=np.linspace(-0.5,1.5,20) yy=f(xx) plt.plot(xx,yy) print('前',fo_df(0.5,0.5)) print('后',back_df(0.5,0.5)) print('中',cen_df(0.5,0.5)) plt.legend(loc='best') plt.show() xx=np.linspace(-1,1,20) yy=f(xx) plt.plot(xx,yy) print('前',fo_df(0.5,0.25)) print('后',back_df(0.5,0.25)) print('中',cen_df(0.5,0.25)) plt.legend(loc='best') plt.show()
代码中有一些没有补充的部分,我来帮你补充一下。
复化梯形法:
```python
def func(a,b,n,f):
x = np.linspace(a,b,n+1)
sum1 = 0
h =(b-a)/n
for i in range(n):
sum1 += (f(x[i])+f(x[i+1]))*h/2
return sum1
```
复化辛普森法:
```python
def func1(a,b,n,f):
x = np.linspace(a,b,n+1)
sum1 = 0
h =(b-a)/n
for i in range(n):
sum1 += h/6*(f(x[i])+4*f((x[i]+x[i+1])/2)+f(x[i+1]))
return sum1
```
复化科特斯法:
```python
def func2(a,b,n,f):
x = np.linspace(a,b,n+1)
sum1 = 0
h =(b-a)/n
for i in range(n//2):
sum1 += h/3*(f(x[2*i])+4*f((x[2*i]+x[2*i+1])/2)+f(x[2*i+1]))
return sum1
```
向前差商:
```python
def fo_df(x,h):
plt.plot([x-h,x],[f(x-h),f(x)],'r--',label='前')
result= (f(x)-f(x-h))/h
return result
```
向后差商:
```python
def back_df(x,h):
plt.plot([x,x+h],[f(x),f(x+h)],'k--',label='后')
result= (f(x+h)-f(x))/h
return result
```
中心差商:
```python
def cen_df(x,h):
a=(f(x-h)+f(x+h))/2
plt.plot([x-h,x+h],[f(x-h)+f(x)-a,f(x+h)+f(x)-a],'g--',label='中')
result= (f(x+h)-f(x-h))/(2*h)
return result
```
以下代码哪里出错了:import numpy as np from scipy.integrate import odeint from matplotlib import animation import matplotlib.pyplot as plt def f(y,t,G,M): return np.array([y[1], y[0]*y[3]**2-G*M/y[0]**2, y[3], -2*y[1]*y[3]/y[0]]) #初始条件 v0=0 #解微分方程 G,M=6.67e-11,2e30 theta0=0 t= np.linspace(0,20,200) r0=1.5e11 y0=[r0,0,theta0,np.sqrt(2*G*M/r0**3)] res=odeint(f,y0,t,args=(G,M)) x=res[:,0]*np.sin(res[:,2]) y=res[:,0]*np.cos(res[:,2]) #画图 fig=plt.figure() plt.axis([-5*r0,5*r0,-5*r0,5*r0]) myline,=plt.plot([],[],'b',lw=2) mypoint,=plt.plot([],[],'ro',ms=20) def animate(i): mypoint.set_data([x[i],y[i]]) myline.set_data(x[:i],y[:i]) return mypoint,myline anim=animation.FuncAnimation(fig,animate,frames=len(t),interval=100) plt.show()
代码中缺少空格,应该在第一行的 import numpy as np 和 from scipy.integrate import odeint 之间加一个空格。正确代码如下:
```
import numpy as np
from scipy.integrate import odeint
from matplotlib import animation
import matplotlib.pyplot as plt
def f(y,t,G,M):
return np.array([y[1],
y[0]*y[3]**2-G*M/y[0]**2,
y[3],
-2*y[1]*y[3]/y[0]])
#初始条件 v0=0
#解微分方程
G,M=6.67e-11,2e30
theta0=0
t= np.linspace(0,20,200)
r0=1.5e11
y0=[r0,0,theta0,np.sqrt(2*G*M/r0**3)]
res=odeint(f,y0,t,args=(G,M))
x=res[:,0]*np.sin(res[:,2])
y=res[:,0]*np.cos(res[:,2])
#画图
fig=plt.figure()
plt.axis([-5*r0,5*r0,-5*r0,5*r0])
myline,=plt.plot([],[],'b',lw=2)
mypoint,=plt.plot([],[],'ro',ms=20)
def animate(i):
mypoint.set_data([x[i],y[i]])
myline.set_data(x[:i],y[:i])
return mypoint,myline
anim=animation.FuncAnimation(fig,animate,frames=len(t),interval=100)
plt.show()
```
阅读全文