微分方程的matplotlib精确解
matplotlib exact solution to a differential equation
我正在尝试使用 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")
我意识到我的问题可能是由于方程式不正确造成的,但如果有人能指出我的代码中的错误,我将不胜感激。干杯。
您可能希望 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" )
我正在尝试使用 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")
我意识到我的问题可能是由于方程式不正确造成的,但如果有人能指出我的代码中的错误,我将不胜感激。干杯。
您可能希望 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" )