2015-12-11 90 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文檔。起初很難,但你一定會學到一兩個東西。 –