由于三个变量对计算机,没有压力,用穷举
def get_error(a1,a2,a3):
return (1+a1)*(1+a3)/(2+a2+a3)-0.5
max_value=-1
min_value=1
index_max=[1,1,1]
index_min=[1,1,1]
for i in range(-20,21):
for j in range(-50,51):
for k in range(-50,51):
a1=i/1000
a2=j/1000
a3=k/1000
error=get_error(a1,a2,a3)
if(error>max_value):
max_value=error
index_max[0]=a1
index_max[1]=a2
index_max[2]=a3
if(error<min_value):
min_value=error
index_min[0]=a1
index_min[1]=a2
index_min[2]=a3
print('max',index_max,max_value/0.5,min_value/0.5)
print('min',index_min,max_value/0.5,min_value/0.5)
输出结果:
max [0.02, -0.05, 0.05] 0.07100000000000017 -0.06899999999999995
min [-0.02, 0.05, -0.05] 0.07100000000000017 -0.06899999999999995
|