【问题标题】:How can I integrate the Ordinary differential equation using odeint from scipy.integrate?如何使用 scipy.integrate 中的 odeint 积分常微分方程?
【发布时间】:2012-10-26 09:49:54
【问题描述】:

我正在尝试使用 scipy 中的 odeint 在代码中集成定义为 f 的函数。 函数 f 将 theta、t 和 K 作为参数,这些参数在函数 f 下方定义。 y 是结果,我得到了错误。错误的原因是 2 维的 theta。我无法执行整合。有人可以帮我吗?

import numpy as np
import random
from scipy.integrate import odeint

f是要集成的功能

def f(theta, t, K):
    global N   
    tau = 1.5  
    dtheta = np.zeros([T,N], float)  
    for i in range(N):  
        s = 0.  
        for j in range(i+1,N):  
            s = s + np.sin(theta[t-tau,j] - theta[t,i])  
        dtheta[t,i] = K*s  
    return dtheta  

# Number of nodes
N = 10
# Constant
K = 1.0
# Number of time steps 
T = 100
t = np.linspace(0, T, 100, endpoint=False)
theta = np.zeros([T,N], float)

统一生成随机数

for i in range(N):
    theta[0,i] = random.uniform(-180, 180)  

使用来自scipyodeint 集成函数f

y = odeint(f, theta, t, args=(K,))
print y

【问题讨论】:

  • @SilentGhost 在标题中
  • @gokcehan:如果你明白它的意思,你为什么不发表解释,而不是尖刻的评论?
  • @SilentGhost 我并不是想成为一个混蛋,只是我一开始也没有看到这个问题。我不知道这个问题的答案..
  • @gokcehan:但你明白吗?我看到一些使用相关函数的代码。我不知道 integrating using odeint from scipy 是什么意思。
  • @SilentGhost 好吧,我只是在这里推测,但显然scipy.integrate.odeint 是一个集成(数学)函数的函数,他想知道如何在他的函数f 上使用它。希望他能在某个时候加入对话..

标签: python python-2.7 scipy


【解决方案1】:

y = odeint(f, theta.ravel(), t, args=(K,))

def f(theta, t, K):
    global N
    theta = theta.reshape(T, N)
    ...
    return dtheta.ravel()

【讨论】:

  • 坦克很多,伙计。在实施您重塑和展平数组的想法后,不会产生任何错误,但不会产生正确的结果。
  • 它有效,并解决了微分方程 theta' = f(theta)。如果你没有得到你期望的结果,你就犯了一些不同的错误。
猜你喜欢
  • 1970-01-01
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
  • 2016-03-08
  • 1970-01-01
  • 2019-11-16
  • 2018-02-08
相关资源
最近更新 更多