不确定是否有工具可以为任何函数和系数组合构建波段。您可以通过分析计算出最大值和最小值(如 Mad Physicist 的回答所示),或者如果您不确定(如下所示)计算所有可能的函数输出。
这是一种使用您指定的系数的所有可能组合来确定最小和最大函数值的方法。
import numpy as np
import pandas as pd
# write out the function so that if can take coefficient factors (+/- 1)
def y(x, coef1, coef2, coef3):
return coef1 * x**2 - coef2 * x + coef3
# compute each possible combination of coefficients
coef_signs = [1,-1]
# I use a list comprehension but there are several options
possible_coefs = [(0.06+c1*0.016, 0.65+c2*0.04,1.2+c3*0.001) for c1 in coef_signs for c2 in coef_signs for c3 in coef_signs]
现在我们有了所有可能的函数系数组合的列表。
possible_coefs
[(0.076, 0.6900000000000001, 1.2009999999999998),
(0.076, 0.6900000000000001, 1.199),
(0.076, 0.61, 1.2009999999999998),
(0.076, 0.61, 1.199),
(0.044, 0.6900000000000001, 1.2009999999999998),
(0.044, 0.6900000000000001, 1.199),
(0.044, 0.61, 1.2009999999999998),
(0.044, 0.61, 1.199)]
下面我使用 pandas 是因为我觉得它很舒服
但这不是必需的,您可以使用纯 numpy 数组来构建可能的函数值。
# specify some domain for the function
x = np.linspace(-10,10,100)
# build a Pandas dataframe from the combinations and resulting function values
df = pd.DataFrame(data={f'c0={c[0]};c1={c[1]}; c2={c[2]}':y(x, c[0],c[1],c[2]) for c in possible_coefs})
# calculate the min and max values from all which become the bands
df['min'] = df.min(1)
df['max'] = df.max(1)
# plot
plt.fill_between(x, df['min'], df['max'])