Example: plot a function given on the command line

  • 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) with correct Python syntax can be turned to running code on the fly

    previousnexttable of contents