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]
# 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'])
对于给定的
x
,预期值将介于及
另一种看待它的方式是,因为
σ
总是非负的,所以ax2 + bx + c
的最大绝对偏移量总是不管
a
、b
和c
的符号是什么这是一个快速而肮脏的解决方案,但在视觉上可能与实际结果无法区分
不确定是否有工具可以为任何函数和系数组合构建频带。你可以通过分析计算出最大值和最小值(如Mad物理学家的答案所示),或者如果你不确定,可以计算出所有可能的函数输出(如下所示)
下面是一种使用指定系数的所有可能组合来确定最小和最大函数值的方法
现在我们有了所有可能的函数系数组合的列表
下面我用熊猫,因为我对它很舒服 但这并不是必须的,您可以使用纯numpy数组轻松地构建可能的函数值
我会沿着这条路线做一些事情(不确定我是否错过了任何标志或SMT)
编辑:添加通用多项式
a0 + a1 * x + a2 * x ^ 2 + ...
形式的系数calc_lower和calc_upper可以通过一些编码和;计算具有奇数幂的术语,以确定在每种情况下使用哪一个(我想我太懒了,无法通过编程计算)
相关问题 更多 >
编程相关推荐