您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在使用scipy.integrate.odeint和python时遇到麻烦

在使用scipy.integrate.odeint和python时遇到麻烦

您的代码有几个问题,如果更仔细地阅读文档字符串odeint,您可能可以解决大多数问题。

为了帮助您入门,以下是使用求解标量微分方程的简单示例odeint。我将尝试使用一个非常简单的方程式,而不是尝试去理解(并可能调试)您的函数。我将用初始条件y(0)= 100求解方程dy / dt = a * y。一旦使本示例生效,就可以进行修改fun解决问题。

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt


def fun(y, t, a):
    """Define the right-hand side of equation dy/dt = a*y""" 
    f = a * y
    return f


# Initial condition
y0 = 100.0

# Times at which the solution is to be computed.
t = np.linspace(0, 1, 51)

# Parameter value to use in `fun`.
a = -2.5

# Solve the equation.
y = odeint(fun, y0, t, args=(a,))

# Plot the solution.  `odeint` is generally used to solve a system
# of equations, so it returns an array with shape (len(t), len(y0)).
# In this case, len(y0) is 1, so y[:,0] gives us the solution.
plt.plot(t, y[:,0])
plt.xlabel('t')
plt.ylabel('y')
plt.show()

这是情节:

该示例生成的图

odeintSciPy Cookbook中可以找到使用的更复杂的示例(向下滚动到标有“常微分方程”的项目符号)。

python 2022/1/1 18:46:09 有318人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶