Python matplotlib的相位图结果不准确
我正试图绘制以下代码中sh2函数中定义的方程的相位potrait。我知道预期阶段图应为[[预期阶段图][1][1][1]: 但这是我的结果:[[result][1]: 我正在使用integrate.odeint。有人能建议我在下面的罐子里可以改变什么,或者是否最好使用另一种算法,使我的结果更接近预期 请查找我的代码:Python matplotlib的相位图结果不准确,python,matplotlib,scipy,data-science,ode,Python,Matplotlib,Scipy,Data Science,Ode,我正试图绘制以下代码中sh2函数中定义的方程的相位potrait。我知道预期阶段图应为[[预期阶段图][1][1][1]: 但这是我的结果:[[result][1]: 我正在使用integrate.odeint。有人能建议我在下面的罐子里可以改变什么,或者是否最好使用另一种算法,使我的结果更接近预期 请查找我的代码: import matplotlib.pyplot as plt import numpy as np from numpy import sin import scipy.inte
import matplotlib.pyplot as plt
import numpy as np
from numpy import sin
import scipy.integrate as integrate
from math import *
g = 9.81
l = 1.6
l_big = 2.0
l_small = 1.6
m = 0.01
alpha = l_big-l_small
k = 10*(10**40)
def sh2(r1,t):
theta1,omega1 = r1
sh2_theta1 = omega1
sh2_omega1 = -g*(l + ((1/2)*alpha*(1-np.tanh(theta1*omega1*k))))*sin(theta1)
return np.array([sh2_theta1, sh2_omega1],float)
init_state = np.radians([30.0,0])
dt = 1/10.0
time = np.arange(0,10.0,dt)
timexo = np.arange(0,10.0,dt)
state2 = integrate.odeint(sh2,init_state,time)
print(len(state2),len(timexo))
state2_plot = np.transpose(state2[0:2500])
plt.plot(timexo[0:2500],state2_plot[1], '--m', label = r'$\theta = \frac{\pi}{6}$')
plt.xlabel('Time t (s) ')
plt.ylabel('Angular Velocity' ' ' r'$\dot{\theta}$')
plt.show()
#code for phase plot
# initial values
x_0 = 0.0 # intial angular position
v_0 = 1.0 # initial angular momentum
t_0 = 0 # initial time
# initial y-vector from initial position and momentum
y0 = np.array([x_0,v_0])
# max value of time and points in time to integrate to
t_max = 10
N_spacing_in_t = 10000
# create vector of time points you want to evaluate
t = np.linspace(t_0,t_max,N_spacing_in_t)
# create vector of positions for those times
y_result = integrate.odeint(sh2, init_state, t)
# get angle and angular momentum
angle = y_result[:,0]
angular_velocity = y_result[:,1]
# plot result
fig = plt.figure()
plt.plot(angle, angular_velocity,'--k',lw=1)
plt.xlabel('Angle' ' ' r'$\theta$')
plt.ylabel(r'Angular Velocity' r' $\dot{\theta}$')
plt.gcf().savefig('pumping.png',dpi=300)
plt.show()
感谢您的参观时间您可能需要替换
(1/2)
使用0.5
,否则脚本将无法再现python 2.7下的绘图。此外,我将进一步梳理sh2
中的数学,并为您正在建模的系统提供一些背景。目前,还不完全清楚代码或代码实现的数学是否存在问题(可能是后者)总而言之,这是一个问得很好的问题。如果你画出导数,你会得到一个像第一个一样的图。但是,对于给定的参数,你会到达摆的振荡变成旋转的区域,因此外部区域不是很圆。通常检查你的摆方程它是ddotphi=-g/l*sin(phi)
,长度是除数,这很有意义,因为g
是一个加速度,ddotphi
具有单位1/次^2。您可能需要替换(1/2)
使用0.5
,否则脚本将无法再现python 2.7下的绘图。此外,我将进一步梳理sh2
中的数学,并为您正在建模的系统提供一些背景。目前,还不完全清楚代码或代码实现的数学是否存在问题(可能是后者)总而言之,这是一个问得很好的问题。如果你画出导数,你会得到一个像第一个一样的图。但是,对于给定的参数,你会到达摆的振荡变成旋转的区域,因此外部区域不是很圆。通常检查你的摆方程它是ddotphi=-g/l*sin(phi)
,长度以除数表示,因为g
是加速度,ddotphi
具有单位1/次^2。