PYTHON, Midpoint Method, TypeError: 'float' object cannot be interpreted as an integer
PYTHON, Midpoint Method, TypeError: 'float' object cannot be interpreted as an integer
我正在尝试开发一种用于逼近一阶 ODE 的迭代方法。我将 运行 保留为 float cannot be interpreted as an integer 的错误我已经尝试了很多解决方案,例如将 N 更改为
之类的 int
N = 整数(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
绝对不是整数
我正在尝试开发一种用于逼近一阶 ODE 的迭代方法。我将 运行 保留为 float cannot be interpreted as an integer 的错误我已经尝试了很多解决方案,例如将 N 更改为
之类的 intN = 整数(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
绝对不是整数