MATLAB: Maximum of this function

fminsearch

I want to use fminsearch() to find the max of this function. The first part of this code is the graph I made so I can make an estimate of my x/y values for fminsearch. The second part of my code is how I found my min successfully. The final part of my code is my attempt at finding the max. Although I found the proper x/y values for my max, I just can't get the proper "zmax" value. Here is my code
subplot(2,2,1) % separates figure window into 2 rows and 2 columns for graph
[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)); % minimum
[xyMinVector,zMin]=fminsearch(NegFunction,[2,-1]);
xMin = xyMinVector(1); % value of x when z is a maximum

yMin = xyMinVector(2); % value of y when z is a maximum

fprintf('The minimum value was: z(%6.3f,%6.3f)=%6.3f\n',xMin,yMin,zMin)
%
NegFunction=@(x)-1*(1./((x(1)+3).^2+(x(2)-1).^2+2))+((x(1)-x(2))./((x(1)-1).^2+(x(2)-2).^2+4)); % attempt at max
[xyMaxVector,zMax] = fminsearch(NegFunction,[-0.3,0])
xMax = xyMaxVector(1); % value of x when z is a maximum
yMax = xyMaxVector(2); % value of y when z is a maximum
fprintf('\nThe maximum value was: z(%6.3f,%6.3f)=%6.3f\n',xMax,yMax,-zMax)

Best Answer

You only applied the -1 to the first term. Where you have the ")" just before the "+", that ")" should be at the end of the expression instead.