Home > OS >  How to find minimum value from an array
How to find minimum value from an array

Time:12-24

I'm new to python so the code may not be the best. I'm trying to find the minimum Total Cost (TotalC) and the corresponding m,k and xM values that go with this minimum cost. 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. 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])

CodePudding user response:

You want this:

minIndex = TotalC.argmin()

Now you use that numeric index into all your arrays TotalC, Ck, Cm and Cx.

CodePudding user response:

find the index of the min value in total and the index should be same for corresponding arrays.

  • Related