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

Walter Roberson
Walter Roberson le 9 Mar 2016

1 vote

You do not define NegFunction in what you posted.

5 commentaires

Karan Sandhu
Karan Sandhu le 9 Mar 2016
Modifié(e) : Karan Sandhu le 9 Mar 2016
After running the code again, i get this: "Exiting: Maximum number of function evaluations has been exceeded - increase MaxFunEvals option. Current function value: 0.000000
The minimum value was: z(358139351549666070000000000000000000000000000.000,95234708364011678000000000000000000000000.0"
[xyMinVector,zMin]=fminsearch(NegFunction,[3.5,0], struct('MaxFunEvals', 2000, 'MaxIter', 2000));
John BG
John BG le 9 Mar 2016
Modifié(e) : John BG le 9 Mar 2016
out of bounds: Apply fminsearch within [0 0] centered square side 20.
When the figures show up as large as your results, with so many zeros, either you are encrypting something, lost precision many loops before MATLAB stops, or it's loose way to say Inf.
Walter Roberson
Walter Roberson le 9 Mar 2016
Modifié(e) : Walter Roberson le 9 Mar 2016
fminsearch() is not a global minimizer. It can get stuck in local minima. For this function, there is a minima of value 0 as x(1) approaches infinity and x(2) is something smaller. If you do not happen to enter into the catch-basin of negative values near the solution, then you can end up chasing the 0 out at infinity.
Judging by eye, along y = 0, it appears to me that the watershed for the catch-basin is x = 3.0, so [3.5,0] happens to be at risk of that behaviour. If the starting point had been, for example, [2.5,0] then it would have quickly gone to the minima.
Let this be a cautionary tale about using fminsearch! It is very useful, but it is a local minimizer that cannot always overcome bad starting points.
Ok so I am trying to find the maximum of this function now. Here is my code. I basically negated the original function. The problem is, I keep getting 0.6... as my answer when john pointed out that it should be around 0.3. Any ideas?
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));
[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)

Connectez-vous pour commenter.

Plus de réponses (1)

John BG
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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by