简体   繁体   English

如何从数组中找到最小值

[英]How to find minimum value from an array

I'm new to python so the code may not be the best.我是 python 的新手,所以代码可能不是最好的。 I'm trying to find the minimum Total Cost (TotalC) and the corresponding m,k and xM values that go with this minimum cost.我试图用这个最低成本找到 go 的最低总成本 (TotalC) 和相应的 m、k 和 xM 值。 I'm not sure how to do this.我不知道该怎么做。 I have tried using min(TotalC) however this gives an error within the loop or outside the loop only returns the value of TotalC and not the corresponding m, k, and xM values.我尝试使用 min(TotalC) 但这会在循环内或循环外产生错误,仅返回 TotalC 的值,而不是相应的 m、k 和 xM 值。 Any help would be appreciated.任何帮助,将不胜感激。 This section is at the end of the code, I have included my entire code.本节在代码的末尾,我已经包含了我的整个代码。

    import numpy as np
    import matplotlib.pyplot as plt
    
    
    def Load(x):
        Fpeak = (1000 + (9*(x**2) - (183*x))) *1000     #Fpeak in N
        td = (20 - ((0.12)*(x**2)) + (4.2*(x))) / 1000  #td in s
        
        return Fpeak, td
    
    #####################################################################################################
    ####################### Part 2 ########################
    
    def displacement(m,k,x,dt):           #Displacement function
    
        Fpeak, td = Load(x)               #Load Function from step 1
        
        w = np.sqrt(k/m)                  # Natural circular frequency
        T = 2 * np.pi /w                  #Natural period of blast (s)
        time = np.arange(0,2*T,0.001)     #Time array with range (0 - 2*T) with steps of 2*T/100
        
        
        zt = []                           #Create a lsit to store displacement values
        for t in time:
                if (t <= td):
                    zt.append((Fpeak/k) * (1 - np.cos(w*t)) + (Fpeak/(k*td)) * ((np.sin(w*t)/w) - t))
                else:
                    zt.append((Fpeak/(k*w*td)) * (np.sin(w*t) - np.sin(w*(t-td))) - ((Fpeak/k) * np.cos(w*t))) 
                    
        
        zmax=max(zt)            #Find the max displacement from the list of zt values
        return zmax             #Return max displacement
    
    k = 1E6
    m = 200
    dt = 0.0001
    x = 0
    
    
    z = displacement(m,k,x,dt)
    
    ###################################################################################
    ############### Part 3 #######################
    # k = 1E6 , m = 200kg , Deflection = 0.1m
    
    
    k_values = np.arange(1E6, 7E6, ((7E6-1E6)/10))   #List of k values between min and max (1E6 and 7E6).
    m_values = np.arange(200,1200,((1200-200)/10))   #List of m values between min and max 200kg and 1200kg
    
    xM = []
    
    for k in k_values: # values of k
        for m in m_values: # values of m within k for loop
        
    
            def bisector(m,k,dpoint,dt):  #dpoint = decimal point accuracy
                 xL = 0
                 xR = 10
                 xM = (xL + xR)/2
                 zmax = 99
    
                 while round(zmax, dpoint) !=0.1:
                     zmax = displacement(m,k,xM,dt)
                     if zmax > 0.1:
                         xL = xM
                         xM = (xL + xR)/2
                     else:
                         xR = xM
                         xM = (xL + xR)/2
                 return xM
    
            xM = bisector(m, k, 4, 0.001)
            print('xM value =',xM)
        
####################################################################################        
            def cost (m,k,xM):
            
                Ck = np.array(900 + 825*((k/1E6)**2) - (1725*(k/1E6)))
                Cm = np.array(10*m - 2000)
                Cx = np.array(2400*((xM**2)/4))
                TotalC = Ck + Cm + Cx
                print(TotalC)
                print(min(TotalC))
                return TotalC
        
            TotalC = cost(m, k, xM)
        
            print([xM, m, k, TotalC])

You want this:你要这个:

minIndex = TotalC.argmin()

Now you use that numeric index into all your arrays TotalC , Ck , Cm and Cx .现在,您在所有 arrays TotalCCkCmCx中使用该数字索引。

find the index of the min value in total and the index should be same for corresponding arrays.求最小值的总索引,对应的 arrays 的索引应该相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM