方程构建中的递归嵌套

时间:2012-10-22 20:49:28

标签: python recursion nested

我想编写一个“应用程序”,它将绘制第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)并绘制所有这些。

0 个答案:

没有答案