473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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??
Apr 11 '10 #1

✓ answered by bvdet

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

Before the for loop, initialize an empty list.
Expand|Select|Wrap|Line Numbers
  1. outputList = []
At the end of your code:
Expand|Select|Wrap|Line Numbers
  1.     Vf = flutter_speed(ab,bc,Vf_min,Vf_max)
  2.     outputList.append(Vf[0])
  3.  
  4. print sum(outputList)/len(outputList)

6 2211
bvdet
2,851 Expert Mod 2GB
Question 1
The easiest way is to generate a list with a list comprehension.
Expand|Select|Wrap|Line Numbers
  1. >>> [i*5*10 for i in range(10)]
  2. [0, 50, 100, 150, 200, 250, 300, 350, 400, 450]
  3. >>> 
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:
Expand|Select|Wrap|Line Numbers
  1. >>> def avg(seq):
  2. ...     return sum(seq)/float(len(seq))
  3. ... 
  4. >>> avg([i*5*10 for i in range(10)])
  5. 225.0
  6. >>> 
Apr 11 '10 #2
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:

Expand|Select|Wrap|Line Numbers
  1. for carlo in range (20): # Initially carrying out in the range from 0 to 20
  2.     s = 7.5
  3.     c = 2.0
  4.     m = 100.0
  5.     theta_freq = 10
  6.     a = 2*math.pi
  7.     rho = 1.225
  8.     Mthetadot = -1.2
  9.     xcm = 0.5*c
  10.     xf = 0.48*c
  11.     e = xf/c-0.25
  12.  
  13.     def kappa_freq(a, b):                                            # function for generating random variable
  14.         return a+(b-a)*np.random.uniform()
  15.     kappa_freqrn= kappa_freq(4.5, 5.5)
  16.     #print "kappa_freqrn=", kappa_freqrn
  17.  
  18.     vel   = zeros((2501,1),float)
  19.     freq1 = zeros((2501,1),float)
  20.     freq2 = zeros((2501,1),float)
  21.     freq3 = zeros((2501,1),float)
  22.     freq4 = zeros((2501,1),float)
  23.     damp1 = zeros((2501,1),float)
  24.     damp2 = zeros((2501,1),float)
  25.     damp3 = zeros((2501,1),float)
  26.     damp4 = zeros((2501,1),float)
  27.  
  28.     vstart = 1.0
  29.     vinc = 0.1
  30.     for icount in range(1751):            # velocity loop
  31.  
  32.         V=vstart + icount*vinc
  33.         a11 = (m*s*s*s*c)/3.0
  34.         a22 = m*s*(c*c*c/3.0-c*c*xf+xf*xf*c)
  35.         a12 = m*s*s*(c*c/2.0-c*xf)/2.0
  36.         a21 = a12
  37.         A = matrix([[a11,a21],[a12,a22]])
  38.         Ainv = linalg.inv(A)
  39.  
  40.  
  41.  
  42.         k1 = ((kappa_freqrn*a)**2)*a11
  43.         k2 = ((theta_freq*a)**2)*a22
  44.  
  45.         rv = rho*V
  46.         c11 = rv*c*s*s*s*a/6.0
  47.         c12 = 0.0
  48.         c21 = -rv*c*c*s*s*e*a/4.0
  49.         c22 = -rv*c*c*c*s*Mthetadot/8.0
  50.         C = matrix([[c11,c12],[c21,c22]])
  51.         AC = -Ainv*C
  52.  
  53.         rv2 = rho*V*V
  54.         k11 = k1
  55.         k12 = rv2*c*s*s*a/4.0
  56.         k21 = 0.0
  57.         k22 = k2 - rv2*c*c*s*e*a/2.0
  58.         K = matrix([[k11,k12],[k21,k22]])
  59.         AK = -Ainv*K
  60.  
  61.         temp = concatenate((AK,AC),axis=1)
  62.         M = concatenate((matrix([[0.0,0.0,1.0,0.0],[0.0,0.0,0.0,1.0]]),temp),axis=0)
  63.  
  64.         Meig = linalg.eig(M)[0]
  65.  
  66.  
  67.         vel[icount] = V
  68.         freq1[icount] = abs(Meig[0])
  69.         damp1[icount] = -100.0*real(Meig[0])/freq1[icount]
  70.         freq1[icount] = freq1[icount]/a
  71.         freq2[icount] = abs(Meig[1])
  72.         damp2[icount] = -100.0*real(Meig[1])/freq2[icount]
  73.         freq2[icount] = freq2[icount]/a
  74.         freq3[icount] = abs(Meig[2])
  75.         damp3[icount] = -100.0*real(Meig[2])/freq3[icount]
  76.         freq3[icount] = freq3[icount]/a    
  77.         freq4[icount] = abs(Meig[3])
  78.         damp4[icount] = -100.0*real(Meig[3])/freq4[icount]
  79.         freq4[icount] = freq4[icount]/a
  80.  
  81.         if damp3[icount] < 0:          
  82.             break
  83.  
  84.  
  85.  
  86.     ab = (damp3[icount-1])
  87.     #print ab 
  88.  
  89.     bc = (damp3[icount])
  90.     #print bc
  91.  
  92.     Vf_min = V-0.1
  93.     #print 'Vf_min=', Vf_min
  94.  
  95.     Vf_max = V
  96.     #print 'Vf_max=', Vf_max
  97.  
  98.  
  99.  
  100.     def flutter_speed(ab,bc,Vf_min,Vf_max):                 # flutter_speed function
  101.         return (Vf_min+(Vf_max - Vf_min)*(-(ab)/(bc - ab)))
  102.     Vf = flutter_speed(ab,bc,Vf_min,Vf_max)
  103.     print Vf
  104.     Vf1 = [Vf_max for i in range(carlo)]
  105.     print Vf1
I want to calculate the mean of Vf1...hw can i do tht?? I would appreciate all your help
Apr 12 '10 #3
bvdet
2,851 Expert Mod 2GB
Please use code tags when posting code. See posting guidelines here.

Before the for loop, initialize an empty list.
Expand|Select|Wrap|Line Numbers
  1. outputList = []
At the end of your code:
Expand|Select|Wrap|Line Numbers
  1.     Vf = flutter_speed(ab,bc,Vf_min,Vf_max)
  2.     outputList.append(Vf[0])
  3.  
  4. print sum(outputList)/len(outputList)
Apr 12 '10 #4
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.
Apr 12 '10 #5
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??
Apr 12 '10 #6
bvdet
2,851 Expert Mod 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. >>> Vf
  2. array([ 157.88112949])
  3. >>> Vf[0]
  4. 157.88112949000001
  5. >>> 
Apr 12 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: aa | last post by:
I am migrating to PHP from ASP where there are the Application Scope variables which are accessible from any page on a website and which are used, in particular, for hit counters. Is there a similar...
1
by: jty202 | last post by:
1. Can I have an array of objects as sessions variable in other words sessions array-of-objects? If so how? 2. Besides Sessions variable what else can I use to store large amount of data...
2
by: guttyguppy | last post by:
This code: <? //PHP SCRIPT: getimages.php Header("content-type: application/x-javascript"); //This function gets the file names of all images in the current directory //and ouputs them as a...
2
by: Michaelk | last post by:
Can somebody tell me how many Session variables would be considered exessive, and when they start really affect the server speed. For example on 20-30 asp pages I need to use about 200-300 session...
3
by: sunbeam | last post by:
Short Description of the Project: we developed a e-learning system for our students. each student has a unique username/password to view the modules he/she should view and nothing more. since we...
1
by: Dhuwad | last post by:
Dear All, I wanted to track the user behaviour on my site and wants to store it. To meet this objective, I am planning to capture the mouse pointer (x,y) and store it in the dianamic array and...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
1
by: untitled | last post by:
i wrote an application in c++ that capture a motion from a bmp sequence and outputs a 3DS max secript file contains the motions keys. i have two points here need to be fixed: 1- it is an offline...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
4
by: mbatestblrock | last post by:
I hope this makes some sense. My ultimate goal here is to execute a block of code if the mouse has not moved in a minute or so within the broswer. The machine I am running this on is for internal...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.