Coding problems

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

    Coding problems

    I got a list of numbers lets say A = [0.456, 0.4344, 1, -1, 4, 5, 6,8]

    how can the function mentioned below be fixed in python using the list of numbers mentioned above and get the six different solutions of 'xf' for lets say the code mentioned below:
    Code:
    c=2
    xfmin = 0.432*c
    xfmax = 0.528*c
    def xf(a, b):                                           
        return a+(b-a)*A
    xfrn= xf(xfmin, xfmax)
    print "xf=", xfrn
    Last edited by bvdet; Apr 28 '10, 01:47 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

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

    Add an argument to the function and iterate on the list in a for loop:
    Code:
    A = [0.456, 0.4344, 1, -1, 4, 5, 6,8]
    
    def xf(a, b, item):
        return a+(b-a)*item
    
    c=2
    xfmin = 0.432*c
    xfmax = 0.528*c
    
    for item in A:
        xfrn= xf(xfmin, xfmax, item)
        print "xf =", xfrn
    BV - Moderator

    Comment

    • ritesh247
      New Member
      • Mar 2010
      • 12

      #3
      thank you very much for the reply,

      since am a beginner in python I also wanted ask you another question:

      let say if you got if you got a sequence of numbers lets say
      Code:
      A = [3.4, 5.6, 7.7, 9.7]
      B = [3.5, 4.7.-3.3, 8.7]
      C = [8.5, 6.7.-8.3, 4.7]
      
      Z = A*B*C
      the value of Z should have four answers


      I tried using for loops, lets say for

      Code:
      A = [3.4, 5.6, 7.7, 9.7]
      B = [3.5, 4.7.-3.3, 8.7]
      C = [8.5, 6.7.-8.3, 4.7]
      for i in A:
      for j in B:
      for k in C:
      Z = i*j*k
      print z # four possible answers

      but it didn't do what I wanted it to do.
      Could you please help me out solve this problem.
      Last edited by ritesh247; Apr 28 '10, 08:56 PM. Reason: forgot to add something

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Since the lists are the same length (4) and you want four answers, one could assume you want to multiply the number at index i of list A by the corresponding index of the other two lists. Iterate on A using built-in function enumerate().
        Code:
        for i, item in enumerate(A):
            print item*B[i]*C[i]

        Comment

        • ritesh247
          New Member
          • Mar 2010
          • 12

          #5
          Is there any other way of doing this other than using enumerate

          lets say if the scenario was such that

          Code:
           A = [3.4, 5.6, 7.7, 9.7]
           B = [3.5, 4.7.-3.3, 8.7]
           C = [8.5, 6.7.-8.3, 4.7]
          
          f=2
          g=4
          
          z=A*B
          y=A*C*f
          u=C*g
          
          ww=z*y*u
          is there any possibility of obtaining four solutions for ww??

          and also lets say if you had to use each one of the column of A B and C at each time, is it possible, cause I have tried different methods but am failing to get what am trying to achieve
          Last edited by ritesh247; Apr 29 '10, 12:27 AM. Reason: forgot to add something

          Comment

          • Glenton
            Recognized Expert Contributor
            • Nov 2008
            • 391

            #6
            Even better is to use numpy arrays. Import numpy, convert a,b,c to numpy arrays, and then z=a*b*c does what you want it to do.

            Comment

            • ritesh247
              New Member
              • Mar 2010
              • 12

              #7
              I tried using numpy arrays but i was getting this error

              Traceback (most recent call last):

              File "C:\Python25\GE M SA.py", line 74, in <module>
              A = matrix([[a11,a12],[a21,a22]])
              File "C:\Python25\Li b\site-packages\numpy\ core\defmatrix. py", line 217, in __new__
              arr = N.array(data, dtype=dtype, copy=copy)
              ValueError: setting an array element with a sequence.

              I would be really thankful if someone can help me out as am struggling to get the end result. I am trying to figure out how I can use each value from the each array to get an output result, in total i will get three different results, is there any method available to solve such a scenario

              Code:
              xfrn=array([0.937143,0.885173,1.0329])
                  
              
              theta_freqrn= array([10.1579,9.99248,10.9348])
              
              kappa_freqrn= array([4.95614, 4, 2])
              
              arn = array([6.32918, 5, 2])
              
              Mthetadotrn= array([-1.15940, 1, 4])
              
              
                  
              s = 7.5
              c = 2.0
              m = 100.0
              
              rho = 1.225
              xcm = 0.5*c
              
              
              e = xfrn/c-0.25    
                      
              
              vel   = zeros((3500,1),float)
              freq1 = zeros((3500,1),float)
              freq2 = zeros((3500,1),float)
              freq3 = zeros((3500,1),float)
              freq4 = zeros((3500,1),float)
              damp1 = zeros((3500,1),float)
              damp2 = zeros((3500,1),float)
              damp3 = zeros((3500,1),float)
              damp4 = zeros((3500,1),float)
              
              vstart = 1.0
              vinc = 0.1
              for icount in range(3500):            
                  V=vstart + icount*vinc
              
                  a11 = (m*s*s*s*c)/3.0
                  a12 = m*s*s*(c*c/2.0-c*xfrn)/2.0
                  a21 = a12
                  a22 = m*s*(c*c*c/3.0-c*c*xfrn+xfrn*xfrn*c)
              
                  A = matrix([[a11,a12],[a21,a22]])
                  Ainv = linalg.inv(A)
                          
                  k1 = ((kappa_freqrn*arn)**2)*a11 
                  k2 = ((theta_freqrn*arn)**2)*a22  
              
                  c11 = (rho*V*c*s*s*s*arn)/6.0
                  c12 = 0
                  c21 = -(rho*V*c*c*s*s*e*arn)/4.0
                  c22 = -(rho*V*c*c*c*s*Mthetadotrn)/8.0
              
                  C = matrix([[c11,c12],[c21,c22]])
              
                  AC = -Ainv*C 
              
                  k11 = k1
                  k12 = (rho*V*V*c*s*s*arn)/4.0
                  k21 = 0
                  k22 = k2 - (rho*V*V*c*c*s*e*arn)/2.0
                  K = matrix([[k11,k12],[k21,k22]])
              
                  AK = -Ainv*K
              
                  matrix_bind_columns = concatenate((AK,AC),axis=1)
                  M = concatenate((matrix([[0.0,0.0,1.0,0.0],[0.0,0.0,0.0,1.0]]),matrix_bind_columns),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]/2*math.pi
                  freq2[icount] = abs(Meig[1])
                  damp2[icount] = -100.0*real(Meig[1])/freq2[icount]
                  freq2[icount] = freq2[icount]/2*math.pi
                  freq3[icount] = abs(Meig[2])
                  damp3[icount] = -100.0*real(Meig[2])/freq3[icount]
                  freq3[icount] = freq3[icount]/2*math.pi
                  freq4[icount] = abs(Meig[3])
                  damp4[icount] = -100.0*real(Meig[3])/freq4[icount]
                  freq4[icount] = freq4[icount]/2*math.pi
              
                  if damp3[icount] < 0:           goes below zero
                      break
              
              ab = (damp3[icount-1])
                      
              
              bc = (damp3[icount])
                      
              
              Vf_min = V-0.1
                      
              
              Vf_max = V
                      
              
              def flutter_speed(ab,bc,Vf_min,Vf_max):                  
                  return (Vf_min+(Vf_max - Vf_min)*(-(ab)/(bc - ab)))
              Vf = flutter_speed(ab,bc,Vf_min,Vf_max)
              print Vf

              Comment

              • ritesh247
                New Member
                • Mar 2010
                • 12

                #8
                I am wondering if anyone can help me fix the array problem out?? If someone can give me feedback on where I am going wrong??

                Comment

                • Glenton
                  Recognized Expert Contributor
                  • Nov 2008
                  • 391

                  #9
                  Do you mean just for the multiplication?

                  Code:
                  In [1]: from numpy import *
                  
                  In [2]: A=array([3.4,5.6,7.7,9.7])
                  
                  In [3]: B=array([3.5,4.7,-3.3,8.7])
                  
                  In [4]: C=array([8.5,6.7,-8.3,4.7])
                  
                  In [5]: f=2
                  
                  In [6]: g=4
                  
                  In [7]: z=A*B
                  
                  In [8]: y=A*C*f
                  
                  In [9]: u=C*g
                  
                  In [10]: ww=z*y*u
                  
                  In [11]: z
                  Out[11]: array([ 11.9 ,  26.32, -25.41,  84.39])
                  
                  In [12]: y
                  Out[12]: array([  57.8 ,   75.04, -127.82,   91.18])
                  
                  In [13]: u
                  Out[13]: array([ 34. ,  26.8, -33.2,  18.8])
                  
                  In [14]: ww
                  Out[14]: array([  23385.88   ,   52931.41504, -107830.48584,  144659.98776])
                  If it's something else, maybe start a new post.

                  Comment

                  Working...