0

This python program is supposed to simulate an object being thrown off a 50 meter building, with some initial velocity and constant gravitational acceleration. I am using arrays to store the different components, but when it's time to do my computations, my resulting matrix is not turning out like it should. In fact, the resulting matrix is for the most part still empty. What could be causing this problem?

 x = z = vz = vy = ax = ay = time = 0.0
 y = 50 #Initial Height: 50 meters
 vx = 25 #Initial velocity in the x direction: 25 m/s
 az = -9.8 #Constant acceleration in the z direction: -9.8 m/s^2
 deltaTime = .000001

 #Initializes a matrix with 3 columns and 1000 rows for each column: Will hold the corresponding x,y,z coordinate of the particle at time t
 positionMatrix = [[None]*1000 for x in range(3)] 

 posArray = [x, y, z] 
 velArray = [vx, vy, vz] 
 accArray = [ax, ay, az]
 timeArray = [i*deltaTime for i in range(1000)]

 j = 1 #time increment

 for j in range (1,500): #j is the time increment
     for i in range (1,3): #i is each component (x, y, z)

         #x = x + vx*time + .5*ax*(time*time); #y = y + vy*time + .5*ay*(time*time); #z =    z + vz*time + .5*az*(time*time)
         positionMatrix[i][j] = posArray[i] + velArray[i] * timeArray[j] +  1/2*accArray[i] * timeArray[j] * timeArray[j]
         print(positionMatrix)
5
  • @Nix: I fixed that part, but still not getting anything better Commented Jun 20, 2011 at 19:33
  • Personally (and this wouldn't be your issue), I would cache timeArray[j] if using it 3 times. Commented Jun 20, 2011 at 19:34
  • Is this homework? if it is, it should be tagged as such. Commented Jun 20, 2011 at 19:34
  • @cwallenpoole: what do you mean? Are the values in timeArray getting overwritten each time? Commented Jun 20, 2011 at 19:35
  • You're using x in the list, so this change his value. Commented Jun 20, 2011 at 19:37

3 Answers 3

1

Your ranges are wrong - posArray is indexed from 0 to 2 (so posArray[0] = x, posArray[1] = y, posArray[2] = z). Also, you're printing out the matrix every time, so you'll see lots of None there.

You also put 1000 rows in the array, but then only fill 500 of them in.

You should replace the last block of code with:

 for j in range (1000):
     for i in range (3):
         positionMatrix[i][j] = posArray[i] + velArray[i] * timeArray[j] +  1/2*accArray[i] * timeArray[j] * timeArray[j]

 print(positionMatrix)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, now I would like to get an output so that I can plot the data in excel. How could I alter my code to do this
That's a new question, and you should post it separately.
Ok, but even then, by looking at the data, it still doesn't seem accurate. I have a lot of None's and my values are incredibly small. Could it be something with my timeArray?
You've only filled in 500 of your 1000 rows - see edited answer. Even then, with a time delta of 0.000001 and 1000 steps, you're only tracking the object for the first 0.001 second. How far did you think it would fall in that time?
1

I am not sure you have a valid question? How are you judging failure? Is it because you are printing out positionMatrix each time?

It just looks like nothing is there because you are printing out 3k None's each iteration. Change your line of code from:

print(positionMatrix)

to

print(positionMatrix[i][j])

I did a

cnt=0
for j in range (1,500): #j is the time increment
  for i in range (1,3): #i is each component (x, y, z)
    positionMatrix[i][j] = posArray[i] + velArray[i] * timeArray[j] +  1/2*accArray[i] * timeArray[j] * timeArray[j]
    if(positionMatrix[i][j] == None):
        cnt +=1
print 'none count' , cnt

Result was

none count 0

So you can see that each row is getting set to something. At least the ones you are processing, start your range at 0(dont specify 1).

for j in range (500): #j is the time increment
  for i in range (3): #i is each component (x, y, z)

2 Comments

I noticed this too. I was thinking maybe his problem wasn't that the matrix is empty and just that he was getting confused by the reams of output.
Do you mean: print(positionMatrix[i][j])?
0

I don't know if this is the only or even most important issue in your code, but you start your ranges at 1. That means you never loop through the first element of the arrays, index 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.