在解决Python中的物理问题时遇到问题。 这是一个ODE系统。 ODE System 在某些时间点,必须注入一些剂量以提高值A.
我必须用四阶龙格库塔法解决这个问题。 我以为我得到了它,但每次都会出现计算错误。
RuntimeWarning: overflow encountered
经过短暂的计算时间后,数值会上升到无穷大...... 另一个问题是,在第3个循环中,Tr得到一个物理上不可能的负值。
这是我的代码:
a = 10.
v = 8.
x = 0.8
p = 1.02
u1 = 0.2
u2 = 0.1
ur = 0.25
c = 0.0001
N_0 = a
T1_0 = 0.001
T2_0 = 0.0001
Tr_0 = 0.001
A_0 = 0.
t = 0.
h = 1.
while (t <= int(np.amax(time))):
for i in range(len(time)):
if t == time[i]:
A_0 = A_0 + dosis[i]
print('INITAL VALUE: ',' t =', t,' A =', A_0,' N =', N_0,' T1 =', T1_0,' T2 =', T2_0,' Tr =', Tr_0)
a1 = -(A_0) * (T1_0 + T2_0 + Tr_0)
b1 = -(N_0) + a - (N_0 * A_0 * ((T1_0 / (1. + u2 * T2_0)) + c))-(p * N_0 * A_0 * (T2_0 + c)) - (x * N_0 * A_0 *(Tr_0 + c))
c1 = -(T1_0) + ((v * N_0 * A_0) / (1. + ur * Tr_0)) * ((T1_0 / (1. + u2 * T2_0)) + c)
d1 = -(T2_0) + (p * ((v * N_0 * A_0) / (1. + ur * Tr_0)) * ((T2_0 + c) / (1. + u1 * (T1_0 / (1. + u2 * T2_0)))))
e1 = -(Tr_0) + (x * v * N_0 * A_0 * (Tr_0 + c))
A_1 = A_0 + (h / 2.) * a1
N_1 = N_0 + (h / 2.) * b1
T1_1 = T1_0 + (h / 2.) * c1
T2_1 = T2_0 + (h / 2.) * d1
Tr_1 = Tr_0 + (h / 2.) * e1
a2 = -(A_1) * (T1_1 + T2_1 + Tr_1)
b2 = -(N_1) + a - (N_1 * A_1 * ((T1_1 / (1. + u2 * T2_1)) + c))-(p * N_1 * A_1 * (T2_1 + c)) - (x * N_1 * A_1 *(Tr_1 + c))
c2 = -(T1_1) + ((v * N_1 * A_1) / (1. + ur * Tr_1)) * ((T1_1 / (1. + u2 * T2_1)) + c)
d2 = -(T2_1) + (p * ((v * N_1 * A_1) / (1. + ur * Tr_1)) * ((T2_1 + c) / (1. + u1 * (T1_1 / (1. + u2 * T2_1)))))
e2 = -(Tr_1) + (x * v * N_1 * A_1 * (Tr_1 + c))
A_2 = A_0 + (h / 2.) * a2
N_2 = N_0 + (h / 2.) * b2
T1_2 = T1_0 + (h / 2.) * c2
T2_2 = T2_0 + (h / 2.) * d2
Tr_2 = Tr_0 + (h / 2.) * e2
a3 = -(A_2) * (T1_2 + T2_2 + Tr_2)
b3 = -(N_2) + a - (N_2 * A_2 * ((T1_2 / (1. + u2 * T2_2)) + c))-(p * N_2 * A_2 * (T2_2 + c)) - (x * N_2 * A_2 *(Tr_2 + c))
c3 = -(T1_2) + ((v * N_2 * A_2) / (1. + ur * Tr_2)) * ((T1_2 / (1. + u2 * T2_2)) + c)
d3 = -(T2_2) + (p * ((v * N_2 * A_2) / (1. + ur * Tr_2)) * ((T2_2 + c) / (1. + u1 * (T1_2 / (1. + u2 * T2_2)))))
e3 = -(Tr_2) + (x * v * N_2 * A_2 * (Tr_2 + c))
A_3 = A_0 + h * a3
N_3 = N_0 + h * b3
T1_3 = T1_0 + h * c3
T2_3 = T2_0 + h * d3
Tr_3 = Tr_0 + h * e3
a4 = -(A_3) * (T1_3 + T2_3 + Tr_3)
b4 = -(N_3) + a - (N_3 * A_3 * ((T1_3 / (1. + u2 * T2_3)) + c))-(p * N_3 * A_3 * (T2_3 + c)) - (x * N_3 * A_3 *(Tr_3 + c))
c4 = -(T1_3) + ((v * N_3 * A_3) / (1. + ur * Tr_3)) * ((T1_3 / (1. + u2 * T2_3)) + c)
d4 = -(T2_3) + (p * ((v * N_3 * A_3) / (1. + ur * Tr_3)) * ((T2_3 + c) / (1. + u1 * (T1_3 / (1. + u2 * T2_3)))))
e4 = -(Tr_3) + (x * v * N_3 * A_3 * (Tr_3 + c))
t = t + h
A_0 = A_0 + (h / 6.) * (a1 + (2. * a2) + (2. * a3) + a4)
N_0 = N_0 + (h / 6.) * (b1 + (2. * b2) + (2. * b3) + b4)
T1_0 = T1_0 + (h / 6.) * (c1 + (2. * c2) + (2. * c3) + c4)
T2_0 = T2_0 + (h / 6.) * (d1 + (2. * d2) + (2. * d3) + d4)
Tr_0 = Tr_0 + (h / 6.) * (e1 + (2. * e2) + (2. * e3) + e4)
这是当前的输出:
INITAL VALUE: t = 0.0 A = 0.0 N = 10.0 T1 = 0.001 T2 = 0.0001 Tr = 0.001
INITAL VALUE: t = 1.0 A = 0.1 N = 10.0 T1 = 0.0003750000000000001 T2 = 3.7500000000000003e-05 Tr = 0.0003750000000000001
INITAL VALUE: t = 2.0 A = 0.2975779058326524 N = 9.9799116753568 T1 = 0.09021366809218588 T2 = 0.029730294675718548 Tr = 0.04000932816912045
INITAL VALUE: t = 3.0 A = 0.20021726558830752 N = 9.428758582263455 T1 = 4.6119100465532386 T2 = 3.8633993244370335 Tr = -7.920033484370073
INITAL VALUE: t = 4.0 A = 40682939.2164535 N = 737615666179.137 T1 = -32.736202523709856 T2 = 265493.88168389443 Tr = -5887693774039.941
INITAL VALUE: t = 5.0 A = inf N = nan T1 = -inf T2 = inf Tr = -inf
INITAL VALUE: t = 6.0 A = nan N = nan T1 = nan T2 = nan Tr = nan
INITAL VALUE: t = 7.0 A = nan N = nan T1 = nan T2 = nan Tr = nan
INITAL VALUE: t = 8.0 A = nan N = nan T1 = nan T2 = nan Tr = nan
INITAL VALUE: t = 9.0 A = nan N = nan T1 = nan T2 = nan Tr = nan
INITAL VALUE: t = 10.0 A = nan N = nan T1 = nan T2 = nan Tr = nan
我希望有人知道什么可以帮助我 谢谢你的时间!!!
答案 0 :(得分:0)
我终于可以解决问题了。 但我必须将步长调整到h = 0.001,这样才不会出错。 这需要将程序计算为3小时......
我可以使用while循环使其更快一点吗?