how to capture variables and and store them in an array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ritesh247
    New Member
    • Mar 2010
    • 12

    #1

    how to capture variables and and store them in an array?

    Lets say for this function:
    x=5
    z=10
    for i in range (200):
    V= i*z*x

    how would i be able to capture all the values of V in the range 200 (make an array, as i have tried making it but i have gotten no where) and calculate its mean??

    2) If I am supposed to create a probability density function..how can I create one using python??
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Question 1
    The easiest way is to generate a list with a list comprehension.
    Code:
    >>> [i*5*10 for i in range(10)]
    [0, 50, 100, 150, 200, 250, 300, 350, 400, 450]
    >>>
    Question 2
    Your question is vague. A function can be created for just about any task. Anyway, I don't know the mathematics. Ask me about trigonometry or algebra though.

    An example of a function that calculates an average of a list of numbers:
    Code:
    >>> def avg(seq):
    ... 	return sum(seq)/float(len(seq))
    ... 
    >>> avg([i*5*10 for i in range(10)])
    225.0
    >>>

    Comment

    • ritesh247
      New Member
      • Mar 2010
      • 12

      #3
      I am trying all those steps but am still not getting the required results, I am attaching my script code. Could you please have a look at it and tell me why am not being able to store the variables in a list. Since I want to calculate the mean of Vf. When i try getting the mean for Vf i gt the same value as Vf and thts not the correct answer:

      Code:
      for carlo in range (20): # Initially carrying out in the range from 0 to 20
          s = 7.5
          c = 2.0
          m = 100.0
          theta_freq = 10
          a = 2*math.pi
          rho = 1.225
          Mthetadot = -1.2
          xcm = 0.5*c
          xf = 0.48*c
          e = xf/c-0.25
      
          def kappa_freq(a, b):                                            # function for generating random variable
              return a+(b-a)*np.random.uniform()
          kappa_freqrn= kappa_freq(4.5, 5.5)
          #print "kappa_freqrn=", kappa_freqrn
      
          vel   = zeros((2501,1),float)
          freq1 = zeros((2501,1),float)
          freq2 = zeros((2501,1),float)
          freq3 = zeros((2501,1),float)
          freq4 = zeros((2501,1),float)
          damp1 = zeros((2501,1),float)
          damp2 = zeros((2501,1),float)
          damp3 = zeros((2501,1),float)
          damp4 = zeros((2501,1),float)
      
          vstart = 1.0
          vinc = 0.1
          for icount in range(1751):            # velocity loop
          
              V=vstart + icount*vinc
              a11 = (m*s*s*s*c)/3.0
              a22 = m*s*(c*c*c/3.0-c*c*xf+xf*xf*c)
              a12 = m*s*s*(c*c/2.0-c*xf)/2.0
              a21 = a12
              A = matrix([[a11,a21],[a12,a22]])
              Ainv = linalg.inv(A)
      
      
      
              k1 = ((kappa_freqrn*a)**2)*a11
              k2 = ((theta_freq*a)**2)*a22
      
              rv = rho*V
              c11 = rv*c*s*s*s*a/6.0
              c12 = 0.0
              c21 = -rv*c*c*s*s*e*a/4.0
              c22 = -rv*c*c*c*s*Mthetadot/8.0
              C = matrix([[c11,c12],[c21,c22]])
              AC = -Ainv*C
      
              rv2 = rho*V*V
              k11 = k1
              k12 = rv2*c*s*s*a/4.0
              k21 = 0.0
              k22 = k2 - rv2*c*c*s*e*a/2.0
              K = matrix([[k11,k12],[k21,k22]])
              AK = -Ainv*K
      
              temp = concatenate((AK,AC),axis=1)
              M = concatenate((matrix([[0.0,0.0,1.0,0.0],[0.0,0.0,0.0,1.0]]),temp),axis=0)
      
              Meig = linalg.eig(M)[0]
      
      
              vel[icount] = V
              freq1[icount] = abs(Meig[0])
              damp1[icount] = -100.0*real(Meig[0])/freq1[icount]
              freq1[icount] = freq1[icount]/a
              freq2[icount] = abs(Meig[1])
              damp2[icount] = -100.0*real(Meig[1])/freq2[icount]
              freq2[icount] = freq2[icount]/a
              freq3[icount] = abs(Meig[2])
              damp3[icount] = -100.0*real(Meig[2])/freq3[icount]
              freq3[icount] = freq3[icount]/a    
              freq4[icount] = abs(Meig[3])
              damp4[icount] = -100.0*real(Meig[3])/freq4[icount]
              freq4[icount] = freq4[icount]/a
      
              if damp3[icount] < 0:          
                  break
      
      
      
          ab = (damp3[icount-1])
          #print ab 
      
          bc = (damp3[icount])
          #print bc
      
          Vf_min = V-0.1
          #print 'Vf_min=', Vf_min
      
          Vf_max = V
          #print 'Vf_max=', Vf_max
      
      
      
          def flutter_speed(ab,bc,Vf_min,Vf_max):                 # flutter_speed function
              return (Vf_min+(Vf_max - Vf_min)*(-(ab)/(bc - ab)))
          Vf = flutter_speed(ab,bc,Vf_min,Vf_max)
          print Vf
          Vf1 = [Vf_max for i in range(carlo)]
          print Vf1
      I want to calculate the mean of Vf1...hw can i do tht?? I would appreciate all your help
      Last edited by bvdet; Apr 12 '10, 01:05 AM. Reason: Add code tags

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Please use code tags when posting code. See posting guidelines here.

        Before the for loop, initialize an empty list.
        Code:
        outputList = []
        At the end of your code:
        Code:
            Vf = flutter_speed(ab,bc,Vf_min,Vf_max)
            outputList.append(Vf[0])
            
        print sum(outputList)/len(outputList)

        Comment

        • ritesh247
          New Member
          • Mar 2010
          • 12

          #5
          I will make sure I will use the code tags when posting code.
          I tried implementing the method you wrote down but it gives me the same results as before. I am trying to print out the mean velocity all its giving me is the velocity which is the last Vf it prints out. I am wondering if this problem can be solved. Really sorry for asking so many questions.

          Comment

          • ritesh247
            New Member
            • Mar 2010
            • 12

            #6
            Got it working thanks :)

            but now when i try making a list for Vf_min anf Vf_max i get this error:
            Type Error: 'float' object is unsubscriptable

            why do i get such an error??

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              In the code I commented on, Vf was a single element array. You must be attempting the same thing on a float, which gives you the error.

              Code:
              >>> Vf
              array([ 157.88112949])
              >>> Vf[0]
              157.88112949000001
              >>>

              Comment

              Working...