You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python自定义ODE求解类solve方法返回None,如何正确获取结果列表

ODE求解器返回None问题修复方案

核心问题

  • solve成员方法仅执行了对应求解逻辑,没有显式返回求解结果,Python函数无显式return时默认返回None
  • 原代码存在缩进错误,Heun、RungeKutta方法未完成实现也未设置返回逻辑
  • 原Euler方法内调用的fnc是笔误,和入参名fcn不一致会触发命名错误

修复后代码

class ODEsolver():
    def __init__(self):
        self.init=self
    def Euler(self, fcn, x0, y0, xend, h):
        estimate=[y0]
        z=y0
        b=0
        t=x0   
        while(t<xend):
            slope1=fcn(bisection(t,301,1200,10**-4))
            y=z+slope1*h/8
            t+=h
            z=y
            estimate.append(y)
        return estimate

    def Heun(self, fcn, x0, y0, xend, h):
        # 此处补充Heun方法实现逻辑,最后返回结果列表
        estimate = []
        # 实现逻辑省略
        return estimate

    def RungeKutta(self,fcn,x0,y0,xend,h):
        # 此处补充RungeKutta方法实现逻辑,最后返回结果列表
        estimate = []
        # 实现逻辑省略
        return estimate

    def solve(self,which, fcn, x0, y0, xend, h):
        if which==0:
            return self.Euler(fcn,x0,y0,xend,h)
        elif which==1:
            return self.Heun(fcn,x0,y0,xend,h)
        else:
            return self.RungeKutta(fcn,x0,y0,xend,h)

修改完成后执行e=r.solve(0,diff,0,1200,6000,30)即可正常获取到求解结果列表。

内容的提问来源于stack exchange,提问作者Davis Son

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 00:54:01