如何解决不打印出 z 和 theta 的值
我正在使用四阶 Runge-Kutta 方法求解两个耦合常微分方程。由于应用此方法,我无法打印 z 的值。源代码如下供参考。请帮助我通过打印 z 和 theta 的更新值来修复此代码。谢谢。
#Import neeeded modules
import numpy as np
import matplotlib.pyplot as plt
#Input parameters
k = 5 #longitudinal torsional constant
delta = 10**-3 #longitudinal torsional constant
I = 10**-4 #Rotational Inertia
eps = 10**-2 #coupling constant
m = 0.5
#Time Step
#Setting time array for graph visualization
dt = 0.002 #Time Step
tStop = 0.30 #Maximum time for graph visualization derived from Kinematics
t = np.arange(0.,tStop+dt,dt) #Array of time
z = np.zeros(len(t))
dz = np.zeros(len(t))
theta = np.zeros(len(t))
dtheta = np.zeros(len(t))
#Functions that include the equations of motion
def dYdt(t,u):
z,dz,theta,dtheta = u
ddz = -(k*z+0.5*eps*theta)/m
ddtheta = -(delta*theta+0.5*eps*z)/I
return np.array([dz,ddz,dtheta,ddtheta])
def rk4(t,u,dt):
for i in range(len(t)-1):
# runge_kutta
k1 = dYdt(t[i],u[i])
k2 = dYdt(t[i] + dt/2,u[i] + dt/2 * k1)
k3 = dYdt(t[i] + dt/2,u[i] + dt/2 * k2)
k4 = dYdt(t[i] + dt,u[i] + dt * k3)
u.append(u[i] + (k1 + 2*k2 + 2*k3 + k4) * dt / 6)
#Unpacking
z,dtheta = np.asarray(u).T
print(z)
这是我用来想出源代码的运动方程。 Coupled ODEs
解决方法
这就是我认为您想要的,但我不知道要传递给 u
的参数是什么。另外,z,dz,theta,dtheta = np.asarray(u).T
是一维向量,所以我不知道您期望 import numpy as np
import matplotlib.pyplot as plt
#Input parameters
k = 5 #longitudinal torsional constant
delta = 10**-3 #longitudinal torsional constant
I = 10**-4 #Rotational Inertia
eps = 10**-2 #Coupling constant
m = 0.5
#Time Step
#Setting time array for graph visualization
dt = 0.002 #Time Step
tStop = 0.30 #Maximum time for graph visualization derived from Kinematics
t = np.arange(0.,tStop+dt,dt) #Array of time
#Functions that include the equations of motion
def dYdt(t,u):
z,dtheta = u
ddz = -(k*z+0.5*eps*theta)/m
ddtheta = -(delta*theta+0.5*eps*z)/I
return np.array([dz,ddz,dtheta,ddtheta])
def rk4(t,u,dt):
for i in range(len(t)-1):
# runge_kutta
k1 = dYdt(t[i],u[i])
k2 = dYdt(t[i] + dt/2,u[i] + dt/2 * k1)
k3 = dYdt(t[i] + dt/2,u[i] + dt/2 * k2)
k4 = dYdt(t[i] + dt,u[i] + dt * k3)
u.append(u[i] + (k1 + 2*k2 + 2*k3 + k4) * dt / 6)
#Unpacking
return np.asarray(u).T
z,dtheta = rk4(t,dt)
print(z)
做什么。因此,此代码不会运行,但会向您展示一个潜在的设计。
# Dropping the duplicates in order to get the unique dealer list
dealer_df = df_s[["DealerID","LAT","LNG"]].drop_duplicates()
dealer_df = dealer_df.set_index("DealerID")
from sklearn.neighbors import KNeighborsClassifier
# Instantiating with n_neighbors as 1 and weights as "distance"
knn = KNeighborsClassifier(n_neighbors=1,weights="distance",n_jobs=-1)
knn.fit(dealer_df.values,dealer_df.index)
df_c["Nearest Dealer"] = knn.predict(df_c[["LAT","LNG"]].values)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。