从solve_ivp中提取、打印和打印中间值

2024-05-08 22:10:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我用solve_ivp来解一系列积分微分方程。下面的代码是一个更大代码的简化表示。我希望能够提取并绘制/打印“随着时间的推移”,中间值的演变值,(例如)A_值与t

我曾尝试添加一个计数器,并在每次迭代通过a(t,pools)时将“a_value”的当前值添加到一个列表中,这确实有效-但问题是在这个模型的完整版本中,它变得过大-我遇到了存储空间/大小问题(我想进行200多次中间计算,我的存储阵列大小每个变量超过100万)。我正在尝试找出是否有更有效的方法。我想做一些可以允许最终绘图a_值与t的东西

我也尝试过这个解决方案:Is there a way to store intermediate values when using solve_ivp?,但我确实需要一个方法中的所有中间变量(由于大小),并且我不能完全重新创建它们的绘图结果(有效吗?)

(其他注意事项:我知道“时间”在我的A(t,pools)方法中没有任何作用,我只是重新创建了更大模型的结构,其中t是该方法中的一个必需变量。我确信我也错误地使用了“全局”,但我只是尝试了任何东西……)

任何帮助或指导都将不胜感激!我如何在不使用计数器的情况下保存数据并绘制A_值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()
"""

Tags: andthetolabelstimeplottitlevalue