PYTHON,Midpoint方法,TypeError:'float'对象不能解释为整数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PYTHON,Midpoint方法,TypeError:'float'对象不能解释为整数相关的知识,希望对你有一定的参考价值。

我正在尝试开发一种近似一阶ODE的迭代方法。我继续遇到浮动的错误不能解释为一个整数我已经尝试了很多解决方案,如将N更改为int类似

N = int(N)

或h = int((b-a)/ N)

或者我试过h =(b-a)// N.

但即使我尝试其他解决方案,我最终得到浮点错误划分:除以零。我知道代码并不那么激烈,但是我很难尝试将输入更改为接受N,如整数而不是浮点数,以便可以正确迭代数组。

这是我的代码:

TypeError: 'float' object cannot be interpreted as an integer

在:

File "/Users/luciusanderson/Documents/Numerical Analysis 
II/ODE_Approx_methods.py", line 61, in Midpoint_meth
y = np.zeros((N+1,))      #array to hold Midpoint Method approximated y 
values




def Midpoint_meth(def_fn, a, b, N, ya):

"""
    Test the Midpoint Method to solve
    initial value problem y'=f(t,y) with t in [a,b] and y(a) = ya.
    Step size h is computed according to input number of mesh points N
"""

f = def_fn #input definining function


h = (b-a)/N # developing step size h, from input values divided by N




t = np.arange(a, b+h, h) #array intialized to hold mesh points t
y = np.zeros((N+1,))      #array to hold Midpoint Method approximated y values

y[0] = ya   #intial condition 

#iterative method

for  i in range(0, N):

    tau = t[i]      #current mesh point t 
    w = y[i]        #current value y(t)


    # next iteration using midpoint method 
    y[i + 1] = w + h*f(tau + h/2.0, w + h*f(tau, w /2.0))


return (t, y)

############ Example #1 Given Points 

N_mdeul1 = 20.0  # number of mesh points
a_mdeul1 = 0.0 # left end point of interval [a,b]
b_mdeul1 = 2.0 # right end point of interval [a,b]
ya_mdeul1 = 0.5 # initial value y(a)

# defining function and true solution of function #1
def_fn_mdeul1 = exmp_fn.exmp1_def_fn
sol_mdeul1 = exmp_fn.exmp1_sol


# run Euler's method from ODE_Approx_methods for example #1

(t_mdeul1,w_mdeul1) = ODE_Approx_methods.Midpoint_meth(def_fn_mdeul1, 
答案

我破解了我认为的相关部分。

def Midpoint_meth(def_fn, a, b, N, ya):

    """
        Test the Midpoint Method to solve
        initial value problem y'=f(t,y) with t in [a,b] and y(a) = ya.
        Step size h is computed according to input number of mesh points N
    """
    print(type(N))
    f = def_fn #input definining function


    h = (b-a)/N # developing step size h, from input values divided by N


    t = np.arange(a, b+h, h) #array intialized to hold mesh points t
    y = np.zeros((N+1,))      #array to hold Midpoint Method approximated y values

    y[0] = ya   #intial condition 

    #iterative method

    for  i in range(0, N):

        tau = t[i]      #current mesh point t 
        w = y[i]        #current value y(t)


        # next iteration using midpoint method 
        y[i + 1] = w + h*f(tau + h/2.0, w + h*f(tau, w /2.0))


    return (t, y)

############ Example #1 Given Points 

N_mdeul1 = 20.0  # number of mesh points
a_mdeul1 = 0.0 # left end point of interval [a,b]
b_mdeul1 = 2.0 # right end point of interval [a,b]
ya_mdeul1 = 0.5 # initial value y(a)



# run Euler's method from ODE_Approx_methods for example #1

(t_mdeul1,w_mdeul1) = Midpoint_meth(1, a_mdeul1, b_mdeul1, ya_mdeul1, N_mdeul1)

我收到错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-31-51b398a6cbbd> in <module>()
     43 # run Euler's method from ODE_Approx_methods for example #1
     44 
---> 45 (t_mdeul1,w_mdeul1) = Midpoint_meth(1, a_mdeul1, b_mdeul1, ya_mdeul1, N_mdeul1)
     46 

<ipython-input-31-51b398a6cbbd> in Midpoint_meth(def_fn, a, b, N, ya)
     20     #iterative method
     21 
---> 22     for  i in range(0, N):
     23 
     24         tau = t[i]      #current mesh point t

TypeError: 'float' object cannot be interpreted as an integer

这让我在你的电话中看到了:

(t_mdeul1,w_mdeul1) = Midpoint_meth(1, a_mdeul1, b_mdeul1, ya_mdeul1, N_mdeul1)

第四个参数(函数调用中的N),即变量

ya_mdeul1

ya_mdeul1 = 0.5 

绝对不是一个int

以上是关于PYTHON,Midpoint方法,TypeError:'float'对象不能解释为整数的主要内容,如果未能解决你的问题,请参考以下文章

Binary Search using Python

[数学]midpoint 法

python数据结构与算法第十四天二分查找

Python - TypeError:'int'对象不可迭代

这个答案的python 3版本是啥?

Python Dataframe Groupby 均值和标准差