Python 方程建立中的递归嵌套
我想编写一个“应用程序”,它将绘制链式反应中第n种试剂的浓度的时间函数:a->B->C->D-> 问题是,c_n(t)包含2^n-1个指数函数,它们是基于我发现的模式嵌套的:Python 方程建立中的递归嵌套,python,recursion,nested,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
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]}>
(唯一的区别是我正在尝试构建的新wykres()函数):
如何修复wykres()函数,使其绘制c(n)?我如何构建它以便可以绘制它?我想让Python自动为我想要的任何n构建c_n(t)并绘制它们。问题是:如何修复wykres()函数以绘制c(n)。我想让Python自动为我想要的任意n构建c_n(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)
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