我必须绘制一个两个变量函数来检查模型简并性。我的代码在教程后面看起来像这样:
from numpy import exp,arange
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show
import math
# the function that I'm going to plot
def z_func(x1,x2):
L = exp(-(1-x1)**2 - 100((x2-x1**2)**2))
return L
x1 = arange(-5.0,5.0,0.1)
x2 = arange(-5.0,5.0,0.1)
X1,X2 = meshgrid(x1, x2) # grid of point
Z = z_func(X1, X2) # evaluation of the function on the grid
im = imshow(Z,cmap=cm.RdBu) # drawing the function
# adding the Contour lines with labels
cset = contour(Z,arange(-1,1.5,0.2),linewidths=2,cmap=cm.Set2)
clabel(cset,inline=True,fmt='%1.1f',fontsize=10)
colorbar(im) # adding the colobar on the right
# latex fashion title
title('$z=exp(-(1-x1)^2 - 100(x2-x1^2)^2)$')
show()
我收到以下错误:
File "multi.py", line 14, in <module>
Z = z_func(X1, X2) # evaluation of the function on the grid
File "multi.py", line 8, in z_func
L = exp(-(1-x1)**2 - 100((x2-x1**2)**2))
TypeError: 'int' object is not callable
我认为我如何定义我的功能有问题,如何修复此错误并绘制函数?