# Homework Help: Python problem: Plotting two functions against each other

1. Mar 2, 2016

### BubblesAreUs

1. The problem statement, all variables and given/known data

Enter a minimum height and velocity into plot function and return a velocity-height plot.

2. Relevant equations

3. The attempt at a solution

Code (Python):
# Find length of general list
n = len(K)

# Build a list for time [0,20] seconds ( Global)
time = n*[0.0]

# Acceleration of gravity
g = -9.80

def height(time, height0):

# Initialise H to be the same list size as time
H = n*[0.0]

for i in range(n):
H[ i ] = 0.5*-9.80*(time[ i ])**2 + height0
return H

def velocity(time,velocity0):
V = n*[0.0]

for j in range(n):
V[j] = -9.80*(time[j]) + velocity0
return V

def plot(velocity0, height0):
# Plot the velocity V versus the height H
#
# Output: none

for k in range(n):
V = velocity(time[k],velocity0)
H = height(time[k],height0)

plot(V,H)

# Set Y-axis range

title('Plot of velocity versus height')
xlabel('velocity ( m/s)')
ylabel('height(m)')
grid('on')
show()
For some reason, I receive the following error:

Traceback (most recent call last):
File "<pyshell#172>", line 1, in <module>
plot(100,400)
File "C:\Users\SILLYHEAD\Dropbox\MATH3511\Lab 1\Exercise7F.py", line 39, in plot
V = velocity(time[k],velocity0)
File "C:\Users\SILLYHEAD\Dropbox\MATH3511\Lab 1\Exercise7F.py", line 27, in velocity
V[j] = -9.80*(time[j]) + velocity0
TypeError: 'float' object has no attribute '__getitem__'

Last edited by a moderator: Mar 2, 2016
2. Mar 2, 2016

### BubblesAreUs

I just noticed that by tinkering around with the function, I was able to get the velocity function to work. Unfortunately, the height function doesn't work and I've tried modifying it once again...

Code (Python):
for p in range(21):
H[p] = (1/2)*g*(p**2)+(velocity0*p)+height0
return H
The output returned is..

[400.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

Seeing that I'm expecting a list of 21 float variables ( aka height), this is obviously not the correct answer. I think the fault lies with the equation that is used to produce the final height given time ( or p), the velocity and initial height.

Last edited by a moderator: Mar 2, 2016
3. Mar 2, 2016

### Staff: Mentor

Couple of things:
1) Please put code tags around your code. This is especially important with Python code, which relies on indentation to show loop bodies, function bodies, etc. I have done this in both of your posts. Here's how to do it

[code=python]
[/code]
2) Part of your code was gobbled up due to the browser interpreting [i] as the start of BBCode to make italics. I fixed that by changing to [ i ]; that is, adding a space before and after i.

When I attempted to run your code, I got a NameError, with K in the first line not defined. In the 2nd post, you said you got things to work. How did your code change from what you posted in the orig. post?

The big problems, I believe, are in how you call your functions.
Code (Python):
for k in range(n):
V = velocity(time[k],velocity0)
H = height(time[k],height0)
The first argument in each function should be a list, not an element of a list. IOW, you should call these functions with time, not time[k].

4. Mar 2, 2016

### Staff: Mentor

One other thing. I used my best guess as to how your code should be formatted. As it is now, your plot function calls the height and velocity functions, but no code calls the plot function. You need some code at the outer level that calls the plot function.

5. Mar 6, 2016

### BubblesAreUs

Alrighty. Thanks for helping me out. I managed to get it running a week ago, but forgot to reply about my status.