Maxima and Minima using fminsearch()?
Afficher commentaires plus anciens
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)
Réponse acceptée
Plus de réponses (1)
John BG
le 9 Mar 2016
with x step 1 you get
min(min(z))
ans = -0.433333333333333
max(max(z))
ans = 0.309523809523810
zmax_lin_index=find(z==max(max(z)))
=159
zmin_lin_index=find(z==min(min(z)))
=224
[xmax ymax]=ind2sub(size(z),zmax_lin_index)
xmax =
12
ymax =
8
[xmin ymin]=ind2sub(size(z),zmin_lin_index)
xmin =
14
ymin =
11
refining x step down to .01
min(min(z))
=
-0.436048111705817
max(max(z))
=
0.314386299028360
zmax_lin_index=find(z==max(max(z)))
zmax_lin_index =
1393784
zmin_lin_index=find(z==min(min(z)))
zmin_lin_index =
2030325
[xmax ymax]=ind2sub(size(z),zmax_lin_index)
xmax =
1088
ymax =
697
[xmin ymin]=ind2sub(size(z),zmin_lin_index)
xmin =
1311
ymin =
1015
If you find this answer of any help solving this question, please click on the thumbs-up vote link,
thanks in advance
John
Catégories
En savoir plus sur Mathematics 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!