Python problem: Plotting two functions against each other

In summary, I was able to get the velocity function to work, but the height function doesn't seem to be working.
  • #1
BubblesAreUs
43
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
  • #2
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:
  • #3
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]
<your code>
[/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.
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
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
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.
 

Related to Python problem: Plotting two functions against each other

1. What is Python?

Python is a high-level, interpreted programming language known for its simple and easy-to-learn syntax. It is widely used for various applications such as web development, data analysis, and scientific computing.

2. How can I plot two functions against each other in Python?

To plot two functions against each other in Python, you can use the Matplotlib library. First, import the necessary modules and define your functions. Then, use the plt.plot() function to plot the two functions on the same graph.

3. What is the benefit of plotting two functions against each other?

Plotting two functions against each other allows you to visualize the relationship between the two functions. This can help in understanding the behavior and patterns of the functions, as well as identifying any intersections or points of interest.

4. Can I customize the plot of two functions in Python?

Yes, you can customize the plot of two functions in Python using various parameters such as color, line style, and axis labels. Matplotlib offers a wide range of customization options to make your plot more visually appealing and informative.

5. Are there any other libraries besides Matplotlib for plotting two functions in Python?

Yes, there are other libraries such as Seaborn, Plotly, and Bokeh that can be used for plotting two functions in Python. Each of these libraries offers unique features and capabilities, so it's worth exploring and choosing the one that best fits your needs.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
908
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
Back
Top