Maximum of this function?
Afficher commentaires plus anciens
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 commentaire
Jos (10584)
le 9 Mar 2016
What is the error you get? Is the value different from what you expected?
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur 2-D and 3-D Plots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!