3

我想编写一个“应用程序”,它将绘制第 n 个试剂的浓度随时间的连锁反应函数:A->B->C->D->...

问题是,c_n(t) 包含 2^n - 1 个指数函数 - 根据我发现的模式嵌套:

c_1(t) = c_0_1 * exp(-k_1 * t)

c_2(t) = c_0_2 * exp(-k_2 * t) + c_0_1 * k_1 * {[exp(-k_1 * t) - exp(-k_2 * t)]/[k_2 - k_1]}

c_3(t) = c_0_3 * exp(-k_3 * t) + c_0_2 * k_2 * {[exp(-k_2 * t) - exp(-k_3 * t)]/[k_3 - k_2]} + c_0_1 * k_1 * k_2 * [1/(k_2-k_1)] * <{[exp(-k_1 * t) - exp(-k_3 * t)]/[k_3 - k_1]} - {[exp(-k_2 * t) - exp(-k_3 * t)]/[k_3 - k_2]}>

如您所见,每个方程都是重复出现的元素的总和。嵌套的数量取决于关系的程度:0 次(A 到 A)- 简单指数函数,1 次(A 到 B,B 到 C 等)- 1 次嵌套,2 次(A 到 C) , B 到 D 等) - 2 个嵌套等。

每个方程可以分为重复出现的部分:

  • “独立”单位:c_0_n * exp(-k_n * t),

  • 基本单位:f(a,b) = (exp(- k_n[b - 1] * t) - exp(- k_n[a - 1] * t)) / (k_n[a - 1] - k_n[b - 1]),

  • 基于基本单元的嵌套单元,

  • 每个嵌套单元之前常数(参数)的乘积。

第 n 个方程的每个嵌套单元都源自第 (n-1) 个方程的嵌套单元。方程本身可以通过迭代积分得到。第 n 个试剂的可能方程的数量(基于独立动力学常数 k 的数量)由贝尔数 B(n) 给出。

每个这样的方程都可以从具有第 n 个试剂的 n 个独立动力学常数的方程中获得(都相互独立)。人们只需要找到这样的方程的石灰。例如,如果 k_3 = k_4 和 k_7 = k_2,那么我们正在寻找 lim k_4->k_3 [lim k_7->k_2 (f(t))]。

工作代码:

print
print ("Commands: komendy() - list of commands, test() - sets initial parameters, zakres() - asks for the number of reagents, tabela() - displays the table, stez() - asks for the initial concentrations, kin() - asks for the kinetic constants.")
print

n = 0

import matplotlib.pyplot as plt

import numpy as np

def komendy(): # displays the list of commands
    print
    print ("Commands: komendy() - list of commands, test() - sets initial parameters, zakres() - asks for the number of reagents, tabela() - displays the table, stez() - asks for the initial concentrations, kin() - asks for the kinetic constants.")
    print
    return

def zakres(): # number of reagents query
    global n, zakres_n, c_0_n, k_n
    n = int(raw_input("Define the number of n reagents: "))
    zakres_n = range(1, n + 1)
    c_0_n = [int(0)] * n
    k_n = [int(0)] * n
    return

def stez(): # initial concentrations query
    while True:
        y = int(raw_input("Define the value of c_0_n for n equal to (press 0 to break): "))
        if y == 0:
            break
        x = raw_input("Define the value of c_0_" + str(y) + ": ")
        if "." in x:
            c_0_n[y - 1] = float(x)
        else:
            c_0_n[y - 1] = int(x)
    return

def kin(): # kinetic constants query
    while True:
        q = int(raw_input("Define the value of k_n for n equal to (press 0 to break): "))
        if q == 0:
            break
        p = raw_input("Define the value of k_" + str(q) + ": ")
        if "." in p:
            k_n[q - 1] = float(p)
        else:
            k_n[q - 1] = int(p)
    return

def tabela(): # displays the table with the initial data
    if n == 0:
        zakres()
        print
        print "n:     ", zakres_n
        print "c_0_n: ", c_0_n
        print "k_n:   ", k_n
        print
    else:
        print
        print "n:     ", zakres_n
        print "c_0_n: ", c_0_n
        print "k_n:   ", k_n
        print
    return

def wykres(): # plots the basic unit
    global f_t, t_k, t, t_d
    a = int(raw_input("a = "))
    b = int(raw_input("b = "))
    reag = map(int, raw_input("Provide the reagents to plot (separate with spacebar): ").split(" "))
    t_k = float(raw_input("Define time range from 0 to: "))
    t_d = float(raw_input("Set the precision of the time axis: "))
    t = np.arange(0,t_k,t_d)
    p = []
    def f_t(t):
        return (np.exp(- k_n[b - 1] * t) - np.exp(- k_n[a - 1] * t)) / (k_n[a - 1] - k_n[b - 1])
    f_t = f_t(t)
    for i in reag:
        p += plt.plot(t,i*f_t)

并且 [还] 不起作用的代码(唯一的区别是我正在尝试构建的新 wykres() 函数):

print
print ("Commands: komendy() - list of commands, test() - sets initial parameters, zakres() - asks for the number of reagents, tabela() - displays the table, stez() - asks for the initial concentrations, kin() - asks for the kinetic constants.")
print

n = 0

import matplotlib.pyplot as plt

import numpy as np

def komendy(): # displays the list of commands
    print
    print ("Commands: komendy() - list of commands, test() - sets initial parameters, zakres() - asks for the number of reagents, tabela() - displays the table, stez() - asks for the initial concentrations, kin() - asks for the kinetic constants.")
    print
    return

def zakres(): # number of reagents query
    global n, zakres_n, c_0_n, k_n
    n = int(raw_input("Define the number of n reagents: "))
    zakres_n = range(1, n + 1)
    c_0_n = [int(0)] * n
    k_n = [int(0)] * n
    return

def stez(): # initial concentrations query
    while True:
        y = int(raw_input("Define the value of c_0_n for n equal to (press 0 to break): "))
        if y == 0:
            break
        x = raw_input("Define the value of c_0_" + str(y) + ": ")
        if "." in x:
            c_0_n[y - 1] = float(x)
        else:
            c_0_n[y - 1] = int(x)
    return

def kin(): # kinetic constants query
    while True:
        q = int(raw_input("Define the value of k_n for n equal to (press 0 to break): "))
        if q == 0:
            break
        p = raw_input("Define the value of k_" + str(q) + ": ")
        if "." in p:
            k_n[q - 1] = float(p)
        else:
            k_n[q - 1] = int(p)
    return

def tabela(): # displays the table with the initial data
    if n == 0:
        zakres()
        print
        print "n:     ", zakres_n
        print "c_0_n: ", c_0_n
        print "k_n:   ", k_n
        print
    else:
        print
        print "n:     ", zakres_n
        print "c_0_n: ", c_0_n
        print "k_n:   ", k_n
        print
    return

def wykres(): # plots the requested functions
    global t_k, t, t_d, f, constans
    reag = map(int, raw_input("Provide the reagents to plot (separate with spacebar): ").split(" "))
    t_k = float(raw_input("Define the time range from 0 to: "))
    t_d = float(raw_input("Define the precision of the time axis: "))
    t = np.arange(0,t_k,t_d)
    p = []

    def f(a,b): # basic unit
        return  (np.exp(- k_n[b - 1] * t) - np.exp(- k_n[a - 1] * t)) / (k_n[a - 1] - k_n[b - 1])

    def const(l,r): # products appearing before the nested parts
        const = 1
        constans = 1
        for h in range(l,r):
            const = const * k_n[h]
        constans = c_0_n[l] * const
        return

    def czlonF(g): # nested part
        czlonF = 0
        for u in range(g):
            czlonF = czlonF + npoch(f(a,b),g)

        if g == 1:
            czlonF(g) = 0
        return

    def npoch(f(a,b),n):
        f = f(a,b)
        for x in range(b+1, n+1):
            f = npoch(f(a,b),x)
        return

    def c(j): # final result, concentration in time function
        return

    def czlon0(m): # 'independent' part
        return (c_0_n[m - 1] * np.exp(- k_n[m - 1] * t))

    for i in reag: # the actual plot command
        p += plt.plot(t,c(i))
    plt.show()
    return

def test():
    global n, zakres_n, k_n, c_0_n
    n = 5
    zakres_n = range(1, n + 1)
    k_n = [1,2,3,4,5]
    c_0_n = [2,3,4,5,6]
    return
    plt.show()
    return

def test():
    global n, zakres_n, k_n, c_0_n
    n = 5
    zakres_n = range(1, n + 1)
    k_n = [1,2,3,4,5]
    c_0_n = [2,3,4,5,6]
    return

如何修复 wykres() 函数以绘制 c(n)?我如何构建它以便可以绘制它?我希望 Python 自动为我想要的任何 n 构建 c_n(t) 并绘制所有这些。

4

0 回答 0