【问题标题】:Its showing error 'float' object cannot be interpreted as an integer其显示错误“浮动”对象不能解释为整数
【发布时间】:2021-12-26 07:21:34
【问题描述】:

我正在尝试绘制曲线。

代码:


    f5=[]
    r4=[]
    for Re in range(2300,1e6,1e5):
    def f_transition_turbulent(Re,ftol=0.001, MaxIter=1000):
        error=10
        Iter_num=0
        f0=0.3164/(Re)**0.25
        while error>ftol and Iter_num<MaxIter:
            f4=1/(2*np.log(Re*np.sqrt(f0))-0.8)**2
            error=abs(f4-f0)
            Iter_num=Iter_num+1
            f0=f4
        return f4
        f5.append(f4)
        r4.append(Re)
    plt.loglog(r4,f5,color='b',lw=2)

运行后我得到的错误是

范围=范围(2300,1e6,1e5)

TypeError: 'float' 对象不能被解释为整数

【问题讨论】:

标签: python


【解决方案1】:

range() 不适用于科学计数法。

import numpy as np # add this for numpy
import matplotlib.pyplot as plt # add this por pyplot

f5 = []
r4 = []

for Re in range(2300, 1000000, 100000):
    def f_transition_turbulent(Re, ftol=0.001, MaxIter=1000):
        f4 = [] # need initialize f4
        error = 10
        Iter_num = 0
        f0 = 0.3164 / (Re) ** 0.25
        while error > ftol and Iter_num < MaxIter:
            f4 = 1 / (2 * np.log(Re * np.sqrt(f0)) - 0.8) ** 2
            error = abs(f4 - f0)
            Iter_num = Iter_num + 1
            f0 = f4
        return f4
        f5.append(f4)
        r4.append(Re)

    f5.append(f_transition_turbulent(Re))
    r4.append(Re)



plt.plot(r4, f5)
plt.xlabel('Re')
plt.ylabel('f')
plt.title('Transition turbulent')
plt.show()

此图表将保留

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-01
    • 2021-12-05
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多