numpy 如何使用argmin()并从数组中找到最小值

7eumitmz  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(186)

我是新手,所以代码可能不是最好的。我正在尝试寻找最小总成本(TotalC)以及与此最小成本对应的m、k和xm值。我不知道该怎么做。我曾尝试使用min(TotalC),但这在循环内或循环外只返回TotalC的值,而不返回相应的m、k和xm值。任何帮助都将不胜感激。这一部分在代码的末尾,我已经包含了我的全部代码。
我试过用
MinIndex=TotalC.argmin()
但我不确定如何使用它,每次它只返回0。

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)

##################################################### 

    #######Step 4
        def cost (m,k,xM):

            Ck = 900 + 825*((k/1E6)**2) - (1725*(k/1E6))
            Cm = 10*m - 2000
            Cx = 2400*((xM**2)/4)
            TotalC = Ck + Cm + Cx
            minIndex = TotalC.argmin(0)
            print(minIndex)
            return TotalC

        TotalC = cost(m, k, xM)
        minIndex = TotalC.argmin()
        print(minIndex)

        print([xM, m, k, TotalC])
hmtdttj4

hmtdttj41#

argmin()返回最小值的索引。如果您正在寻找最小值本身,请尝试使用.min()。也有可能0是数组中的最低值,因此请记住这一点

相关问题