繁体   English   中英

PYTHON,中点方法,类型错误:'float' 对象不能解释为整数

[英]PYTHON, Midpoint Method, TypeError: 'float' object cannot be interpreted as an integer

我正在尝试开发一种用于近似一阶 ODE 的迭代方法。 我一直遇到 float 的错误无法解释为整数我尝试了许多解决方案,例如将 N 更改为 int 之类的

N = 整数(N)

或 h = int((ba)/N)

或者我试过 h = (ba)//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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM