抱歉問了一個愚蠢的問題。我只是想用這個,但什麼都不知道。
我的問題是:我想用 python 程式碼繪製圖表。
我透過呼叫以下命令在終端機(我使用的是 mac)中運行它:
pdflatex -shell-escape plot_air_drag.tex
!我找不到檔案“plot_air_drag.tex”。
<*>plot_air_drag.tex
(按 Enter 重試,或按 Control-D 退出)
請輸入另一個輸入檔名:
運行的方法是什麼?
這是我的程式碼:
\documentclass[a4paper]{book}
\usepackage{pgfplots}
\usepackage{python}
\pagestyle{empty}
\begin{document}
\begin{python}
from pylab import *
from scipy.integrate import odeint
from functools import partial
## set initial conditions and parameters
g = 9.81 # acceleration due to gravity
th = 45. # set launch angle
th = th * np.pi/180. # convert launch angle to radians
v0 = 20.0 # set speed
x0=0 # specify initial conditions
y0=0
vx0 = v0*sin(th)
vy0 = v0*cos(th)
## define function to compute f(X,t)
def f_func_k(k,state,time):
f = zeros(4) # create array to hold f vector
f[0] = state[2] # f[0] = x component of velocity
f[1] = state[3] # f[1] = x component of velocity
f[2] = - k*(f[0]**2 + f[1]**2)**(0.5)*f[0] # f[2] = acceleration in x direction
f[3] = -g - k*(f[0]**2 + f[1]**2)**(0.5)*f[1] # f[3] = acceleration in y direction
return f
def gen_plot_data(s,k):
f_func = partial(f_func_k,k)
## set initial state vector and time array
X0 = [ x0, y0, vx0, vy0] # set initial state of the system
t0 = 0.
# tf = input("Enter final time: ")
# tau = input("Enter time step: ")
tf = 3
tau = 0.05
# create time array starting at t0, ending at tf with a spacing tau
t = arange(t0,tf,tau)
## solve ODE using odeint
X = odeint(f_func,X0,t) # returns an 2-dimensional array with the
# first index specifying the time and the
# second index specifying the component of
# the state vector
# putting ':' as an index specifies all of the elements for
# that index so x, y, vx, and vy are arrays at times specified
# in the time array
x = X[:,0]
y = X[:,1]
vx = X[:,2]
vy = X[:,3]
xy = zip(x,y)
np.savetxt(s,xy,fmt='%0.5f')
gen_plot_data("plotdata1.dat",0.04)
gen_plot_data("plotdata2.dat",0)
\end{python}
\begin{tikzpicture}
\begin{axis}[no marks,samples=100,axis lines=center,xlabel=$x$,ylabel=$y$,enlargelimits]
\addplot table {plotdata1.dat};%
\addplot[magenta] table {plotdata2.dat};%
\end{axis}
\end{tikzpicture}
\end{document}
答案1
在 Mac 10.9.2 和 pdfTeX 版本 3.1415926-2.5-1.40.14 (TeX Live 2013) 上運作良好。但:
將您的文件另存為plot_air_drag.tex(錯誤訊息表示您正在使用不同的檔案名稱)
修改你的序言
\documentclass[a4paper]{book}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{python}
啟動終端,
類型 CDSpace
並刪除資料夾與那裡的文件。
按 Enter 鍵。
並使用pdflatex -shell-轉義plot_air_drag.tex