Maximum of this function?

9 ビュー (過去 30 日間)
Karan Sandhu
Karan Sandhu 2016 年 3 月 9 日
コメント済み: Karan Sandhu 2016 年 3 月 9 日
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.
  1 件のコメント
Karan Sandhu
Karan Sandhu 2016 年 3 月 9 日
Very much appreciated. Thanks.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-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