【发布时间】:2019-11-07 14:35:24
【问题描述】:
这是代码:
import numpy as np
def f_func(state,time,cd,mass,rho,A):
"""Calculate the differential of state vector as a function of time
Args:
state (list): the state vector at time t
time (float): the time t
cd (float): the dimensionless drag coefficient
mass (float): mass of the object in kg
rho (float): density of air (kg/m3)
A (float): cross-sectional area of object (kg)
Returns:
(list): the differential of the state vector at time t
"""
# defensive program - check shape of state vector
assert len(state)==2, "Expected length 2 state vector"
vy,y = state
# YOUR CODE HERE
X = np.array([[vy],[y]])
# we know d**2 y / d t**2 = a = -g + 1/(2mass)*(cd*rho*A*vy**2)
d2ydt2 = -g + (1/(2*mass))*(cd*rho*A*vy**2)
a = d2ydt2
# WE KNOW d y / d t = vy
dXdt = np.array([[-g + (1/(2*mass))*(cd*rho*A*(vy)**2)],[vy]])
return dXdt
检查了以下内容:
from nose.tools import assert_equal, assert_almost_equal
a,vy = f_func([0.,78.],0.0,0.5,1,1.2,1)
assert_almost_equal(a, -9.8)
assert_almost_equal(vy, 0.0)
a,vy = f_func([-2.,78.],0.0,0.5,1,1.2,1)
assert_almost_equal(a,-8.6)
assert_almost_equal(vy,-2)
'''
错误信息,我不明白:
type numpy.ndarray doesn't define __round__ method (error from line 6)
【问题讨论】:
-
显示完整的回溯。
-
回溯是否显示
asset_almost_equal调用round有什么不同?如果a是一个数组,那么该差异也将是一个数组。正如其他人指出的那样,数组上的round会引发此错误。那些nose.tools(和unittest)断言是为 Python 标量设计的,而不是 numpy 数组。