35 lines
887 B
Python
35 lines
887 B
Python
from numpy import *
|
|
from matplotlib.pyplot import *
|
|
|
|
|
|
# functions are defined via def (don't forget the :)
|
|
# return is defined via the return keyword
|
|
def myfunc(x):
|
|
tmp = 2*x**3. + 5
|
|
return tmp
|
|
|
|
# when using more parameters one can give them default values
|
|
def myfunc2(x, a=1., b=5.):
|
|
return (x - a)**2. + b
|
|
|
|
|
|
############# main program below (old stuff) ##############
|
|
|
|
x = linspace(-2., 2., 100)
|
|
|
|
fig = figure()
|
|
ax = fig.add_subplot(1,1,1)
|
|
|
|
ax.plot(x, myfunc(x), color='r', linewidth=2, label='myfunc')
|
|
ax.plot(x, myfunc2(x, 2., 4.), color='b', linewidth=2, label=r'myfunc2 a=2, b=4')
|
|
ax.plot(x, myfunc2(x, 2.), color='g', linewidth=2, label=r'myfunc2 a=2, b=default')
|
|
ax.plot(x, myfunc2(x, b=2.), color='y', linewidth=2, label=r'myfunc2 a=default, b=2')
|
|
|
|
ax.set_xlabel('x values')
|
|
ax.set_ylabel(r'$f(x)$')
|
|
ax.set_title('functions')
|
|
leg = ax.legend()
|
|
|
|
show() # show plot
|
|
|