Python problem: Plotting two functions against each other

Click For Summary

Discussion Overview

The discussion revolves around a Python programming problem related to plotting two functions: velocity and height, based on user-defined inputs. Participants are addressing issues in the code implementation, specifically focusing on function definitions, variable handling, and plotting mechanics.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes the goal of the code, which is to create a velocity-height plot based on initial height and velocity inputs.
  • Another participant identifies a TypeError occurring when trying to access elements of a float, suggesting that the function calls are incorrectly passing individual elements instead of lists.
  • A participant mentions that they modified the height function but still received unexpected output, indicating a potential issue with the equation used to calculate height.
  • Multiple participants emphasize the importance of proper code formatting, particularly regarding indentation and the use of code tags for clarity.
  • There is a suggestion that the plot function needs to be explicitly called in the outer code, as it is not currently invoked.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper function calls and code formatting. However, there is no consensus on the specific corrections needed for the height function or the overall implementation, as some issues remain unresolved.

Contextual Notes

Limitations include potential misunderstandings regarding variable scopes, the need for list versus element handling in function arguments, and the correctness of the equations used for height and velocity calculations.

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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K