从solve_ivp

时间:2020-10-27 22:20:30

标签: python differential-equations

我正在使用solve_ivp求解一系列积分微分方程。以下代码是更大代码的简化表示。我希望能够提取和打印/打印“随时间推移”的中间值的不断变化的值,例如A_value vs t。

我尝试添加一个计数器,并通过A(t,pools)每次迭代将'A_value'的当前值添加到列表中,这确实可行-但问题出在该模型的完整版本中,变得过大-我遇到了存储空间/大小问题(我要遵循200多个中间计算,每个变量我的存储阵列大小超过一百万)。我试图找出是否有一种方法可以更有效地做到这一点。我想要一些允许最终绘图A_value vs t的东西。

我也尝试了以下解决方案:Is there a way to store intermediate values when using solve_ivp?,但是我真的需要一种方法(由于尺寸大小)内的所有中间变量,而且我不能完全重新创建它们的绘图结果(行得通吗?)。

(其他说明:我知道'time'在我的A(t,pools)方法中没有任何作用,我只是在重新创建更大模型的结构,其中t是该方法中的必需变量。我确定我也错误地使用了'global',但只是尝试了任何事情...)

任何帮助或指导将不胜感激!如何在不使用计数器的情况下保存数据并绘制A_value vs t。我仍然是python“新手”,并尝试边做边学。预先感谢

“”“

import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
tspan = (0, 10)     #set the start and stop time
pools0 = [1, 2]         #set the initial values

def A(t, pools):

    global A_value
    global B_value
    global C_value
    global D_value
    global time

    time = t
    if (time ==0):
        cows = 1.0

    C1, C2 = pools      #the pools get saved in here

    A_value = 2*C1 + 3*C2 - C1**2
    B_value = 1*C1 + 4*C2
    C_value = 1*C1 + 3*C2 - C2**2
    D_value = 2*C1 + 5*C2

    print(time, A_value, B_value, C_value, D_value)     # essentially, how this prints, I want to be able to save/store this data

    return [time, A_value, B_value, C_value, D_value]

time, A_value, B_value, C_value, D_value = A(t, pools)

def model_integ(t, pools):

    global A_value
    global B_value
    global C_value
    global D_value
    global time

    time, A_value, B_value, C_value, D_value = A(t, pools)

    dC1 = A_value + B_value
    dC2 = C_value + D_value

    return [dC1, dC2]       # return the differential

pools = solve_ivp(model_integ, tspan, pools0)

#print(A(pools.y[0], pools.y[1]) )

C1, C2 = pools.y
t = pools.t

print(t, C1, C2)      # this works fine, pulling the t, C1, C2 values from model_integ() for printing
print(time, A_value, B_value, C_value, D_value)     # this only prints out their FINAL values


# C1 pool vs t, storage variables
#---------------------------------------------------------------
plt.scatter(t,C1)
plt.xlabel("t, d")      # adding labels and title on the plot
plt.ylabel("C1 pools")    # adding labels and title on the plot
#plt.ylim(0.0,2500)
plt.xlim(0,10)
plt.title("C1/C2 change with time")   # adding labels and title on the plot
plt.tight_layout()      # this takes care of labels overflowing the plot. It's good to use it all the time.
plot_file = "cute_little_plot.png"  # output file to save plot
plt.savefig(plot_file)  # output file to save plot
plt.show()
plt.close()

# A_value vs t, storage variables
#---------------------------------------------------------------
plt.scatter(t,A_value)
plt.xlabel("t, d")      # adding labels and title on the plot
plt.ylabel("A_value")    # adding labels and title on the plot
#plt.ylim(0.0,2500)
plt.xlim(0,10)
plt.title("A_value change with time")   # adding labels and title on the plot
plt.tight_layout()      # this takes care of labels overflowing the plot. It's good to use it all the time.
plot_file = "Avalue_plot.png"  # output file to save plot
plt.savefig(plot_file)  # output file to save plot
plt.show()
plt.close()
"""

0 个答案:

没有答案