c
! www.simula.no/˜hpl
Example: plot a function given on the command line
Ta s k : p l o t ( e. g . ) f(x)=e
−0.2x
sin(2πx) for x ∈ [0, 4π]
Specify f(x) and x interval as text on the command line:
Unix/DOS> python plotf.py "exp(-0.2
*
x)
*
sin(2
*
pi
*
x)" 0 4
*
pi
Program:
from scitools.all import
*
formula = sys.argv[1]
xmin = eval(sys.argv[2])
xmax = eval(sys.argv[3])
x=linspace(xmin,xmax,101)
y=eval(formula)
plot(x, y, title=formula)
Thanks to eval,input(text)withcorrectPythonsyntaxcanbe
turned to running code on the fly
Numerical Python – p. 265/728
Kommentare zu diesen Handbüchern