Python Python problem: Plotting two functions against each other

AI Thread Summary
The discussion revolves around a Python code implementation for plotting a velocity-height graph based on initial height and velocity inputs. The user encounters a TypeError when trying to access elements of a float object, indicating issues with how functions are called. The height and velocity functions are designed to compute values based on time, but the user mistakenly passes single time values instead of the entire list. Additionally, there are problems with the height function returning an incorrect list of heights, likely due to errors in the calculation formula. Suggestions include ensuring proper function calls with lists, correcting code formatting for Python's indentation requirements, and adding a call to the plot function at the outer level to execute the plotting process. The user reports having resolved some issues but seeks further clarification on remaining problems.
BubblesAreUs
Messages
43
Reaction score
1

Homework Statement



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

Homework Equations

The Attempt at a Solution



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 Vdef 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:
Technology news on Phys.org
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...

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:
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

Python:
<your code>
2) Part of your code was gobbled up due to the browser interpreting 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.
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].
 
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.
 
Mark44 said:
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.

Alrighty. Thanks for helping me out. I managed to get it running a week ago, but forgot to reply about my status.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
2
Views
2K
Replies
1
Views
4K
Replies
8
Views
1K
Replies
11
Views
3K
Replies
17
Views
3K
Replies
1
Views
2K
Back
Top