简体   繁体   English

方法 Euler TypeError: cannot unpack non-iterable NoneType object

[英]Method Euler TypeError: cannot unpack non-iterable NoneType object

Trying method Euler and Runge-kutta, but receive error:尝试方法 Euler 和 Runge-kutta,但收到错误:

line 59, in <module>
    xloc , yloc, h , er = rkf45step(xrk[-1],yrk[-1],h,f)
TypeError: cannot unpack non-iterable NoneType object"

Why I am getting this error?为什么我收到此错误? Can you help me, please.你能帮我吗。 I tried a larger timeout, but can't solve this problem.我尝试了更大的超时,但无法解决此问题。

import numpy as np
import matplotlib.pyplot as plt#
beta = -10.
y0 = 1.
h = 0.02
xL = 0.
xR = 1.
N = int((xR-xL)/h)
h = (xR-xL)/float(N)
x = np.linspace(xL,xR,N)
y = np.zeros(N)
xexact = np.linspace(xL,xR,max(1000.,100*N))
def f(x,y):
    return beta * y
def exact(x):
    return y0*np.exp(beta*x)
def eulerIncrementFunction(x,yn,h,ode):
    return ode(x,yn)
def rkf45step(x,yn,h,ode):
    hmin = 1e-5
    hmax = 5e-1
    emin = 1e-7
    nMax = 100
    emax = 1e-5
    if x+h > xR:
        h = xR-x
        update = 0
        for i in range(nMax):
            k1 = ode(x,yn)
            k2 = ode(x+h/4.,yn+h/4.*k1)
            k3 = ode(x+3./8.*h,yn+3./32.*h*k1-9./32.*h*k2)
            k4 = ode(x+12./13.*h,yn+1932./2197.*h*k1-7200./2197.*h*k2+7296./2197.*h*k3)
            k5 = ode(x+h,yn+439./216.*h*k1-8.*h*k2+3680./513.*h*k3-845./4104.*h*k4)
            k6 = ode(x+h/2.,yn-8./27.*h*k1+2.*h*k2-3544./2565.*h*k3+1859./4140.*h*k4-11./40.*h*k5)
            y4 = yn + h * (25./216*k1 + 1408./2565.*k3+2197./4104.*k4-1./5.*k5) 
            y5 = yn + h * (16./135.*k1 + 6656./12825.*k3 + 28561./56430.*k4 - 9./50.*k5 +2./55.*k6)
            er = np.abs(y5-y4)
            if er < emin:
                h = min(2.*h,hmax)
                if x+h > xR:
                    h = xR-x
                    break
                elif er > emax:
                        h = max(h/2.,hmin)
                else:
                    break
            if i==nMax-1:
                print ("max number of iterations reached, check parameters")
            return x+h, y5, h , er
y[0] = y0
for i in range(N-1):
    y[i+1] = y[i] + h * eulerIncrementFunction(x[i],y[i],h,f)
nMax = 1000
xrk = np.zeros(1)
yrk = y0*np.ones(1)
hrk = np.zeros(1)
h = 0.5
for i in range(nMax):
    xloc , yloc, h , er = rkf45step(xrk[-1],yrk[-1],h,f)
    xrk = np.append(xrk,xloc)
    yrk = np.append(yrk,yloc)
    if i==0:
        hrk[i] = h
    else:
        hrk = np.append(hrk,h)
    if xrk[-1]==xR:
        break
plt.subplot(211)
plt.plot(xexact,exact(xexact))
plt.plot(x,y)
plt.plot(xrk,yrk, markersize=7,markeredgewidth=1)
plt.legend()

On this line:在这条线上:

xloc , yloc, h , er = rkf45step(xrk[-1],yrk[-1],h,f)

xrk[-1] will be 0, and h is 0.5. xrk[-1]将为 0, h为 0.5。

Now in your function, rkf45step , you have this check:现在在你的 function, rkf45step中,你有这个检查:

if x+h > xR:

x+h evaluates to 0.5, and xR is 1, so this condition evaluates to False . x+h的计算结果为 0.5, xR为 1,因此该条件的计算结果为False Therefore, none of the code inside this condition gets executed, including your one and only return statement.因此,此条件中的任何代码都不会被执行,包括您的唯一一个return语句。 In the absence of an executed return, a function will return None by default.在没有执行返回的情况下,默认情况下 function 将返回None Therefore, you are attempting to unpack a single None into 4 variables ( xloc, yloc, h, er ).因此,您尝试将单个None解压缩为 4 个变量( xloc, yloc, h, er )。 This is not possible, and hence the exception that you are seeing.这是不可能的,因此您看到的是例外。

The solution is to fix either the logic in your rkf45step function to always return 4 values.解决方案是修复rkf45step function 中的逻辑以始终返回 4 个值。 Or fix the call to the function to check for None first:或者修复对 function 的调用以先检查None

values = rkf45step(xrk[-1],yrk[-1],h,f)
if values is None:
    # take some corrective action here knowing that values is None
else:
    xloc , yloc, h , er = values

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

相关问题 Numpy 抛出 TypeError: cannot unpack non-iterable NoneType object - Numpy throwing TypeError: cannot unpack non-iterable NoneType object TypeError:无法解压不可迭代的 NoneType 对象 - TypeError: cannot unpack non-iterable NoneType object TypeError:无法解压不可迭代的 NoneType 对象错误 - TypeError: cannot unpack non-iterable NoneType object Error PyAutoGui TypeError:无法解压不可迭代的NoneType object - PyAutoGui TypeError: cannot unpack non-iterable NoneType object -- TypeError: cannot unpack non-iterable NoneType object - -- TypeError: cannot unpack non-iterable NoneType object Pyautogui Image 工作一段时间后不可迭代,TypeError: cannot unpack non-iterable NoneType object - Pyautogui Image non-iterable after working for a while, TypeError: cannot unpack non-iterable NoneType object 类型错误:无法解压不可迭代的 object - TypeError: cannot unpack non-iterable object TypeError: cannot unpack non-iterable NoneType object: 找不到我的数据变成“无类型”的位置 - TypeError: cannot unpack non-iterable NoneType object: Cannot find where my data turned into “None Type” 如何修复 TypeError:无法解压不可迭代的 NoneType 对象 - How can I fix TypeError: cannot unpack non-iterable NoneType object Python - TypeError:尽管检查了 None,但无法解压不可迭代的 NoneType 对象 - Python - TypeError: cannot unpack non-iterable NoneType object despite checks for None
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM