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

问题描述 投票:-1回答:1

我正在尝试开发一种近似一阶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, 
python arrays floating-point ode
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.