问题
The matrix code obtained as a formula is as follows:
def objective(x):
#= params
x00 = x[0]
y00 = x[1]
u_m = x[2]
a = x[3]
b = x[4]
u_mean_real = x[5]
A = x[6]
B = x[7]
H = x[8]
if 0.9 < y00 <= 1:
alpha = 1.01
elif 0.8 < y00 <= 0.9:
alpha = 1.02
elif 0.7 < y00 <= 0.8:
alpha = 1.03
elif 0.6 < y00 <= 0.7:
alpha = 1.04
elif 0.55 < y00 <= 0.6:
alpha = 1.05
elif y00 <= 0.55:
alpha = 1.1
if len(x) > 9:
alpha = x[9]
x = Symbol('x')
y = Symbol('y')
zeta = Symbol('zeta')
sghi = Symbol('sigh')
zeta0 = Symbol('zeta0')
sghi0 = Symbol('sghi0')
umax = Symbol('umax')
#alpha = Symbol('alpha')
F = Function('F')
u = Function('u')
GF = Function('GF')
G = Symbol('G')
ff = (((G**(alpha/(alpha-1)))*(1-2*alpha+alpha*G))/(1-(1/u_m)*(1-alpha)*(2*alpha-1)*(G-1)*(1-G**(alpha/(alpha-1)))))
gg = (((1/(alpha*(G-1)*(G**(1-alpha))))*((1-alpha)**(alpha/(alpha-1)))*(1-G**(alpha/(alpha-1))))**(1-alpha))
GF = ff - gg
GFF = lambdify(G, GF, modules='numpy')
def F(G):
return GFF(G)
G_val = optimize.fminbound(F,1.0001,2,disp=True)
G_val = float(G_val)
F = ((1-((zeta - zeta0)/(1 -zeta0))**2)**b)*(1 - ((sghi - sghi0)/((A - sghi0)))**2)**a
FF = F.subs([[zeta,x],[sghi, y],[zeta0, x00], [sghi0, y00]])
umax = u_m*u_mean_real
u = (umax/(G-1))*((1-(1-G**(alpha/(alpha-1)))*FF)**((alpha-1)/alpha)-1)
u_sub = u.subs([[G, G_val]])
xx = np.linspace(0,1,69)
yy = np.linspace(0,1,27)
uu = np.empty([len(xx), len(yy)])
u_func = lambdify([x,y], u_sub, "numpy")
for ii in range(len(xx)):
for jj in range(len(yy)):
uu[ii,jj] = u_func(xx[ii], yy[jj])
return uu, xx, yy, FF, u_func
def normalize (value):
#only numpy array
return (value - value.min())/(value.max() - value.min())
}
Where the "uu" is the main matrix ,
And the matrix obtained from Excel is as follows where the main matrix is "df_nump":
{
path = '100802_1.xlsx'
df_main = pd.read_excel(path)
df_summarry = df_main.iloc[1:2, 3:15]
df_summarry.columns = df_main.iloc[0,3:15]
df = df_main.iloc[8:,0:].reset_index()
del df['index']
df.columns = ['x_' + str(x) for x in range(np.size(df,1))]
df_y = normalize(df.iloc[1:,0])
'''x coordinate #experiment data'''
df_y1 = (df.iloc[1:,0])
df_x = normalize(df.iloc[0, 1:])
''' y coordinate #experiment data '''
df_x1 = (df.iloc[0, 1:])
''' z coordinate # experiment data '''
df_nump = df.iloc[1:,1:].fillna(0)
plt.contour(df_x, df_y, df_nump.to_numpy())
plt.colorbar()
DataPointsDomain = [0 ,0.019651515, 0.340909091, 0.386363636, 0.613636364, 0.659090909, 0.980348485, 1]
DataPointsRange = [1 ,0.536288881 ,0.536288881, 0 ,0, 0.536288881, 0.536288881, 1]
x = symbols('x')
y = symbols('y')
A1 = interpolating_spline(1, x, DataPointsDomain, DataPointsRange)
A2 = interpolating_spline(1, y, DataPointsDomain, DataPointsRange)
}
The parameters of the matrix "uu" formula that are known are as follows:
{
x0 = 0.5
y0 = 0.85806221
u_mean =0.957175
c=6.6
d= 0.2797
u_max = 1.287
}
And the parameters that are guessed at and only the limit of parameter are known that ("a","b" are Between two numbers 1 and 5 except for even numbers that also contain numbers 1 and 5 , "alpha" is Between two numbers 1.01 and 1.5, which can include numbers that can be either numbers 1.01 and 1.5
{
a = 1.7
b = 1.7
alpha = 1.03
}
And calculate the "uu" matrix according to the above parameters:
{
u_unique = np.unique(df_main.iloc[9:45, 1:31].fillna(1.3))
uu, xx1, yy1, FF, u_func = objective([x0 , y0, u_max/u_mean, a, b, u_mean, A1, B, H, alpha])
xx2 = xx1*c
yy2 = yy1*d
''' # xx2 => x coordinate # from code
#yy2 => y coordinate #from code
#uu => z coordinate # from code
# 1=<a=<5, 1=<b=<5 with out even number , 1.01=<alpha=<1.5 and must be obtained from optimization between uu & df_nump '''
fig, axs = plt.subplots( figsize=(10, 5))
value = 0.01
levels = u_unique
cset = axs.contour(xx1, yy1, uu.T, levels = levels)
cbar1 = fig.colorbar(cset, ax = axs, shrink=0.9, pad = 0.05)
# In[79]:
x_valocities = np.append(df_x+1, df_x).astype('f')
y_valocities = np.append(df_y, 1-df_y).astype('f')
df_s = df_main.iloc[1:2, 3:15]
}
and code for find closest elements in two matrix ("uu","df_nump") without repetitions [duplicated] and return the indexs of two matrix :
{
list1=np.array(df_y1)
list2=xx2
index_dfy1=[]
index_xx2=[]
for i in range(len(list1)):
if (len(list2)) > 1: #When there are elements in list2
temp_result = abs(list1[i] - list2) #Matrix subtraction
min_val = np.amin(temp_result) #Getting the minimum value to get closest element
min_val_index = np.where(temp_result == min_val) #To find index of minimum value
closest_element = list2[min_val_index] #Actual value of closest element in list2
list2 = list2[list2 != closest_element] #Remove closest element after found
print(i, list1[i], min_val_index[0][0], closest_element[0]) #List1 Index, Element to find, List2 Index, Closest Element
index_dfy1.append(i)
index_xx2.append(min_val_index[0][0])
else: #All elements are already found
print(i, list1[i], 'No further closest unique closest elements found in list2')
idx_dfy1 = index_dfy1
idx_xx2 = index_xx2
print(idx_dfy1)
print(idx_xx2)
###################################################
list1=np.array(df_x1)
list2=yy2
index_dfx1=[]
index_yy2=[]
for i in range(len(list1)):
if (len(list2)) > 1: #When there are elements in list2
temp_result = abs(list1[i] - list2) #Matrix subtraction
min_val = np.amin(temp_result) #Getting the minimum value to get closest element
min_val_index = np.where(temp_result == min_val) #To find index of minimum value
closest_element = list2[min_val_index] #Actual value of closest element in list2
list2 = list2[list2 != closest_element] #Remove closest element after found
print(i, list1[i], min_val_index[0][0], closest_element[0]) #List1 Index, Element to find, List2 Index, Closest Element
index_dfx1.append(i)
index_yy2.append(min_val_index[0][0])
else: #All elements are already found
print(i, list1[i], 'No further closest unique closest elements found in list2')
idx_dfx1 = index_dfx1
idx_yy2 = index_yy2
print(idx_dfx1)
print(idx_yy2)
}
Now I want to optimize between two "uu" , "df_nump" matrix to determine the best value of the "a" , "b" , "alpha" parameters ,And determine the amount of error based on the square root (R-square) method i attach the excel file excel file
来源:https://stackoverflow.com/questions/58682961/how-to-optimize-between-two-matrices-one-of-the-matrices-is-derived-from-excel