1

friends, I'm new to python and trying to plot three curves with each other, but I have some problems which I can't figure out:

I try to solve trajectory of a ball with different vectors:

1- different x values 2- different velocity values 3- different angle values

     import numpy as np
import math as m
import matplotlib.pylab as pl


def ball(x, theta, v0, y0):
    v0= v0/3.6
    g = 9.81
    theta = m.radians(theta)
    return x * m.tan(theta) + y0\
              - 1./(2.0 * v0 ** 2.0 ) * g * x**2.0 / (m.cos(theta)**2)

x = np.linspace(0, 10, 100)

#part1
theta = 60
y0 = 1.0    
v0= 15.0
y = ball(x, theta, v0, y0)
pl.plot(x, y)


#part2
theta = 60
y0 = 1.0
for v0 in range(10.0, 60.0, 10.0):
  y2 = ball(x, theta, v0, y0)
  pl.plot(x, y)

#part3
y0 = 1.0
for theta in range(0.0, 112.5, 22.5):
   y3 = ball(x, theta, v0, y0)
   pl.plot(x, y)

pl.plot(x, y, "r*")
pl.plot(x, y2, "bo")
pl.plot(x, y3, "y^")
pl.xlabel("X")
pl.ylabel("Y")
pl.legend(["x,y","x,y","x,y"])
pl.show()

please help me what is going on ??

2
  • 1
    So what's unclear about the error message? You are passing in floating point numbers into the range() function. Pass in integers instead. Commented Jan 20, 2017 at 13:55
  • my problem was that i should use floating, now i know how i can use it, thank you Commented Jan 20, 2017 at 14:35

3 Answers 3

2

The range function does not accept floating point numbers as arguments, so you must use integers. What you can do instead, then, is to just increase each part of the range function by a factor of 10, and then decrease it by a factor of 10 within the loop. For example:

for i in range(int(0.0*10), int(112.5*10), int(22.5*10)):
    realI = i/10.0
    #now realI is the float you had wanted originally. 
Sign up to request clarification or add additional context in comments.

1 Comment

No problem! Though you should mark it as the answer then so that future people can find it!
0

Range(start value, end value, step)

You can't use floating type numbers, use integers.

Comments

0

From Python 2.7 Documentation on range():

This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns a list of plain integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not be zero (or else ValueError is raised).

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.