首页 文章

matplotlib对微分方程的精确解

提问于
浏览
0

我试图用matplotlib在python2.7中绘制微分方程(放射性泄漏模型)的精确解 . 当使用欧拉方法或SciPy绘制图形时,我得到预期结果,但是使用精确解,输出是直线图(应该是对数曲线) .

这是我的代码:

import math
import numpy as np
import matplotlib.pyplot as plt

#define parameters
r = 1
beta = 0.0864
x0 = 0
maxt = 100.0
tstep = 1.0

#Make arrays for time and radioactivity

t = np.zeros(1)

#Implementing model with Exact solution where Xact = Exact Solution
Xact = np.zeros(1)
e = math.exp(-(beta/t))
while (t[-1]<maxt):
    t = np.append(t,t[-1]+tstep)
    Xact = np.append(Xact,Xact[-1] + ((r/beta)+(x0-r/beta)*e))

#plot results
plt.plot(t, Xact,color="green")

我意识到我的问题可能是由于方程不正确,但如果有人能在我的代码中指出错误,我将非常感激 . 干杯 .

1 回答

  • 1

    您可能希望 e 依赖于 t ,如

    def e(t): return np.exp(-t/beta)
    

    然后使用

    Xact.append( (r/beta)+(x0-r/beta)*e(t[-1]) )
    

    但你可以把它缩短为

    t = np.arange(0, maxt+tstep/2, tstep)
    plt.plot(t, (r/beta)+(x0-r/beta)*np.exp(-t/beta), color="green" )
    

相关问题