两个指数函数中的每一个都由 3 个参数定义,但每个参数只有 2 个点。一种可能性是为这两个函数提供渐近值。我将在这里粘贴我的代码,包括所有公式的推导,因为时间不够——抱歉:
from math import exp, log
from matplotlib import pyplot as plt
X_MIN, X_CTR, X_MAX = 1, 50, 100
Y_MIN, Y_CTR, Y_MAX = 0.29, 2.10, 6.52
c1 = float(input(f"c1 (> {Y_CTR}): "))
c2 = float(input(f"c2 (< {Y_CTR}): "))
plot = input("plot? (y|n): ")[:1] in "yY"
# c1 - a1 * exp(-b1 * X_MIN) == Y_MIN # with a1 > 0, b1 > 0, c1 > Y_CTR
# c1 - a1 * exp(-b1 * X_CTR) == Y_CTR
# c2 + a2 * exp( b2 * X_CTR) == Y_CTR # with a2 > 0, b2 > 0, c2 < Y_CTR
# c2 + a2 * exp( b2 * X_MAX) == Y_MAX
# a1 * exp(-b1 * X_MIN) == c1 - Y_MIN
# a1 * exp(-b1 * X_CTR) == c1 - Y_CTR
# a2 * exp( b2 * X_CTR) == Y_CTR - c2
# a2 * exp( b2 * X_MAX) == Y_MAX - c2
# log(a1) - b1 * X_MIN == log(c1 - Y_MIN)
# log(a1) - b1 * X_CTR == log(c1 - Y_CTR)
# log(a2) + b2 * X_CTR == log(Y_CTR - c2)
# log(a2) + b2 * X_MAX == log(Y_MAX - c2)
# b1 * (X_CTR - X_MIN) == log(c1 - Y_MIN) - log(c1 - Y_CTR)
# b2 * (X_MAX - X_CTR) == log(Y_MAX - c2) - log(Y_CTR - c2)
b1 = (log(c1 - Y_MIN) - log(c1 - Y_CTR)) / (X_CTR - X_MIN)
b2 = (log(Y_MAX - c2) - log(Y_CTR - c2)) / (X_MAX - X_CTR)
# log(a1) == log(c1 - Y_MIN) + b1 * X_MIN
# log(a2) == log(Y_MAX - c2) - b2 * X_MAX
a1 = exp(log(c1 - Y_MIN) + b1 * X_MIN)
a2 = exp(log(Y_MAX - c2) - b2 * X_MAX)
x_lst = list(range(X_MIN, X_MAX+1))
y_lst = [c1 - a1 * exp(-b1 * x) if x < X_CTR else
c2 + a2 * exp( b2 * x) for x in x_lst]
if plot:
plt.plot(x_lst, y_lst)
plt.grid(True)
plt.show()
else:
for x, y in zip(x_lst, y_lst):
print(f"{x},{y:.14}")
例如,使用此输入:
c1 (> 2.1): 2.13
c2 (< 2.1): 2.08
plot? (y|n): y
输出是: