MATLAB: Maxima and Minima using fminsearch()

fminsearch

I am trying to find the minima and maxima of this function. The first section of my code is the graph of a surface I had to create using this function. The second section of my code is my (failed) attempt at using fminsearch() to find the minimum of the function (though I also have to find the max as well). I require some assistance on how this fminsearch() function is formatted, and how it can be used to find the max too. The picture I linked is what my answers are supposed to look like. Here is my code
x=[-10:1:10];
y=x;
subplot(2,2,1) % separates figure window into 2 rows and 2 columns
[xGrid yGrid]=meshgrid(x,y);
z=(1./((xGrid+3).^2+(yGrid-1).^2+2))+((xGrid-yGrid)./((xGrid-1).^2+(yGrid-2).^2+4)); % function of x/y
surf(x,y,z) % standard projection of surface is isometric
title('Isometric View') % graph title
xlabel('x'),ylabel('y'),zlabel('z') % graph labels
NegFunction=@(x)(1./((x(1)+3).^2+(x(2)-1).^2+2))+((x(1)-x(2))./((x(1)-1).^2+(x(2)-2).^2+4));
[xyMinVector,zMin]=fminsearch(NegFunction,[3.5,0]);
xMin = xyMinVector(1); % value of x when z is a minimum
yMin = xyMinVector(2); % value of y when z is a minimum
fprintf('The minimum value was: z(%0.3f,%0.3f)=%2.0f\n',xMin,yMin,zMin)

Best Answer

You do not define NegFunction in what you posted.