2015-12-11 121 views
-1

我想在这个程序中绘制f,但我搞砸了。有人可以看看,并告诉我我在哪里搞乱了。谢谢。在Python 2.7中绘制函数

import math 
#x is the horizontal distance that the ball has traveled 
g=9.81 
v=raw_input('Enter an initial velocity:') 
theta=raw_input('Enter the angle that the object was thrown at:') 
y=raw_input('Enter the initial position of the object on the y-axis:') 
t=(2*v*math.sin(theta))/g 
x=(0.5)*((v*math.sin(theta))+v)*t 
float(v) 
float(theta) 
float(y) 
float(t) 
f=x*math.tan(theta)-(1/(2*(v**2)))*((g(x**2))/(math.cos(theta)**2))+y 
figure(1) 
clf() 

plot(f) 
xlabel('x') 
ylabel('y') 
show() 
+0

对于初学者,您可以使用'raw_input'读取字符串,并尝试将这些字符串用作数字。 – zero323

回答

1

所以首先,我会导入numpy的和matplotlib

import numpy as np 
import matplotlib.pyplot as plt 

然后,你有你的字符串输入转换成浮动,对于您可以使用eval。

initial_velo = eval(raw_input("Whatever you like: ")) 
... 

然后与matplotlib你实际上必须创建值的列表绘制(只是当你收集真实数据,然后将其输入到计算机,然后绘制单一数据点)。对于我喜欢用linspace从numpy的进口:

time_steps = np.linspace(0, t, steps) 
# steps gives the numbers of intervals your time from 0 to t is splitted into 

现在你创建你的函数X和F为t的函数。他们也必须是类型列表。而在最后,你可以画出你通过想要的东西:

plt.figure(1) 
plt.plot(time_steps, f) 
plt.xlabel("x") 
plt.ylabel("y") 
plt.show() 

但是,也许你也应该看如何在matplotlib文档绘制的东西。也numpy有一个伟大的文档。

+0

np.linspace中的步骤(0,t,steps)我只是为步骤指定一个数字?对不起,我是一个总noob。 – Mike

+0

这就是它的工作原理。你也可以创建一个变量“steps = eval(raw_input(”给出想要的步数:“))。也许看看numpy文档。起初很难,但你一定会学到一两个东西。 –