【问题标题】:matplotlib exact solution to a differential equationmatplotlib 微分方程的精确解
【发布时间】:2018-10-21 01:05:01
【问题描述】:

我正在尝试使用 matplotlib 在 python2.7 中绘制微分方程(放射性泄漏模型)的精确解。当使用 Euler 方法或 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")

我意识到我的问题可能是由于公式不正确,但如果有人能指出我的代码中的错误,我将不胜感激。干杯。

【问题讨论】:

    标签: python python-2.7 numpy matplotlib differential-equations


    【解决方案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" ) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多