3

問題を解決するために odeint を使用しようとしていました。私のコードは以下の通りです:

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

eta=1.24e-9/2
def fun(x):
    f=1.05e-8*eta*x**(1.5)*np.exp(13.6/x)
    return (np.sqrt(1.+4*f)-1)/2./f
x=np.arange(0,1,0.001)
y=odeint(fun,x,0)[0]
plt.plot(x,y)
plt.plot(x,x)
plt.show()

2 つの曲線が同じである場合、これは明らかに間違っています。関数をプロットすると、約 0.3 になる前は非常に小さく、指数関数的に 1 になるステップ関数のように見えます。ありがとうございました!

4

1 に答える 1

11

あなたのコードにはいくつかの問題がありますが、ドキュメントストリングをodeintもっと注意深く読めば、そのほとんどは自分で解決できるかもしれません。

始めるために、以下はスカラー微分方程式を で解く簡単な例ですodeint。関数を理解しようとする (場合によってはデバッグしようとする) 代わりに、非常に単純な方程式を使用します。方程式 dy/dt = a * y を初期条件 y(0) = 100 で解きます。この例が機能したら、修正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()

プロットは次のとおりです。

例によって生成されたプロット

の使用のより複雑な例は、 SciPy クックブック(「常微分方程式」というラベルの付いた箇条書きまでスクロールダウン)odeintで見つけることができます。

于 2013-04-15T05:06:29.003 に答える