Maximum of this function?

9 次查看(过去 30 天)
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)
  1 个评论
Jos (10584)
Jos (10584) 2016-3-9
What is the error you get? Is the value different from what you expected?

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2016-3-9
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by