好的,我知道这个问题被问了很多,但通常回答都是特殊的案例解决方法。我想你可以说我试图理解为什么除了如何让它发挥作用外,它还会起作用。
# ---- Find Spring Rate ---- #
import math
import numpy as np
from pylab import plot, show, title, xlabel, ylabel, axis
import matplotlib as mpl
from matplotlib import pyplot as plt
'''
F = Load
D = mean dia of Coil
d = wire dia
Na = # of active coils
G = Shear Modulus
'''
F = 30 #lbf Assumed load
D = 0.423 #in Measured
d = 0.055 #in Measured
#N_a = 2.75
G = 11.85*10**(6) #psi # pg 526 Table 10-5 - Machine Theory Book
E = 29.0*10**(6) #psi # pg 526 Table 10-5 - Machine Theory Book
X1 = []
Y1 = []
for N_a in range(1,6,0.025):
y = (8*F*D**(3)*N_a)/(d**(4)*G)
k = (d**(4)*G)/(8*D**(3)*N_a)
K=(F//y)
# print("N_a =", N_a)
# print("y=", y)
# print("k=", k)
# print("K=", K)
# print('\n')
X1.append(N_a)
Y1.append(k)
print("X1=", X1)
print("Y1=", Y1)
当我运行此代码时,我得到以下
Spring_Rate_Mk1.py", line 25, in <module>
for N_a in range(1,6,0.025):
TypeError: 'float' object cannot be interpreted as an integer
我尝试过的事情:
X1 = [1.0,1.025,1.0499999999999998,1.0749999999999997,1.0999999999999996,1.1249999999999996,1.1499999999999995,1.1749999999999994,1.1999999999999993,1.2249999999999992,...等]
将方程式分解为部分
A = (d**(4)*G)
B = (8*D**(3)*N_a)
k = A/B
答案 0 :(得分:1)
np.arange
有效,你只是看到浮动的表示。
并且range
不适用于浮点步骤,但在您的情况下,自开始以来很容易修复&amp;终点实际上是整数。
如果没有arange
,您可以使用整数范围并除以1/0.025
为40
之后的值,只需将您的边界乘以40
并稍后应用除法(使用40.0
只是在python 2)的情况下
for N_a in range(40,240):
N_a /= 40.0
print(N_a)
打印:
1.0
1.025
1.05
1.075
1.1
1.125
1.15
1.175
1.2
1.225
1.25
1.275
1.3
1.325
1.35
依此类推....除以10的倍数和2的幂使得结果没有表示错误。