Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to optimize between two matrices, one of the matrices is derived from Excel data
#1

#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())
}
#[color=#E74C3C][b]
#Where the "uu" is the main matrix ,

#And the matrix obtained from Excel is as follows where the main matrix is "df_nump":[/b][/color]

{ 
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)
}

#[b][color=#E74C3C]The parameters of the matrix "uu" formula that are known are as follows:[/color][/b]

{
        x0 = 0.5  
        y0 = 0.85806221   
        u_mean =0.957175
        c=6.6
        d= 0.2797
        u_max = 1.287 

}

#[color=#E74C3C][b]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[/b]
[/color]
{
a = 1.7

b = 1.7 

alpha = 1.03
}

#[color=#E74C3C][color=#E74C3C][b]And calculate the "uu" matrix according to the above parameters:[/b][/color][/color]

{




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] 
}

#[b][color=#E74C3C]and code for find closest elements in two matrix ("uu","df_nump") without repetitions [duplicated] and return the indexs of two matrix :[/color][/b]

{
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

#https://drive.google.com/file/d/1tRpKh37MNe3KWsSmFKreipB5Ymv-#V1QY/view?usp=sharing
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Need Help With Filtering Data For Excel Files Using Pandas eddywinch82 9 556 Aug-06-2019, 03:44 PM
Last Post: eddywinch82
  Aligning excel data gat 1 275 Jun-17-2019, 07:05 PM
Last Post: michalmonday
  Copy raw data in excel to another new excel file keerthiprashanth 5 731 Oct-20-2018, 10:13 AM
Last Post: volcano63
  Solve a system of non-linear equations in Python (scipy.optimize.fsolve) drudox 7 9,933 Aug-18-2018, 02:27 AM
Last Post: scidam
  Pandas dataframe: sum of exponentially weighted correlation matrices per row vvvcvvcv 1 857 May-29-2018, 01:09 AM
Last Post: scidam
  minimize with scipy.optimize tobenmoben 0 811 Feb-17-2018, 01:47 PM
Last Post: tobenmoben
  A matrix of matrices MaximeDt 18 2,586 Nov-26-2017, 01:23 PM
Last Post: heiner55
  I'm working onn below code to extract data from excel using python kiran 1 1,135 Oct-24-2017, 01:42 PM
Last Post: kiran
  Write data into existing Excel (xlsx) file with multiple sheets BNB 1 10,164 Jun-01-2017, 04:22 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)