1
votes

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

3
So what's unclear about the error message? You are passing in floating point numbers into the range() function. Pass in integers instead. - Martijn Pieters
my problem was that i should use floating, now i know how i can use it, thank you - Hitch

3 Answers

2
votes

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. 
0
votes

Range(start value, end value, step)

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

0
votes

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).