why I got orange warning about contour plot?

2 views (last 30 days)
I run my code two times but the results are not the same. As you can see in x and y value after running the code. I go the different values but it is the same code. I don't understand what is happening and how should I do with this code to get the correct result. Moreover, when I run in the MATLAB software, the orange warning is coming up like below.
1st trial
% GA without constraints
% Objective function
f = @(x) (x(1).^4) + ((2.*x(1).^2).*x(2)) + x(2).^2 +3;
% LHS of linear inequalities
A = [];
% RHS of linear inequalities
B = [];
% No linear equalities
Aeq = [];
Beq = [];
% Nonlinear constraints
nonlcon = [];
lb = [-Inf, -Inf];
% Alternatively:
% lb = []
ub = [Inf, Inf];
options = optimoptions('ga','ConstraintTolerance',1e-8,'PlotFcn', @gaplotbestf);
% Solve the problem
[x, fval] = ga(f, 2, A, B, Aeq, Beq, lb, ub, nonlcon, options);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
fprintf("The optimal decision is:\nx = %f\ny = %f\nThis solution has value %f\n", x(1), x(2), fval);
The optimal decision is: x = 1.231051 y = -1.511658 This solution has value 3.000015
figure
hold on
fcont = fcontour(@(x,y) f([x y]), [-10 10 -50 50],'LevelStep',200);
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
optpoint = scatter(x(1), x(2), 200, [0 0 0], '*');
legend([optpoint,fcont], {'Solution','Objective'}, 'Location', 'Southwest');
hold off
2nd trial
% GA without constraints
% Objective function
f = @(x) (x(1).^4) + ((2.*x(1).^2).*x(2)) + x(2).^2 +3;
% LHS of linear inequalities
A = [];
% RHS of linear inequalities
B = [];
% No linear equalities
Aeq = [];
Beq = [];
% Nonlinear constraints
nonlcon = [];
lb = [-Inf, -Inf];
% Alternatively:
% lb = []
ub = [Inf, Inf];
options = optimoptions('ga','ConstraintTolerance',1e-8,'PlotFcn', @gaplotbestf);
% Solve the problem
[x, fval] = ga(f, 2, A, B, Aeq, Beq, lb, ub, nonlcon, options);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
fprintf("The optimal decision is:\nx = %f\ny = %f\nThis solution has value %f\n", x(1), x(2), fval);
The optimal decision is: x = 2.313426 y = -5.490086 This solution has value 3.019085
figure
hold on
fcont = fcontour(@(x,y) f([x y]), [-10 10 -50 50],'LevelStep',200);
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
optpoint = scatter(x(1), x(2), 200, [0 0 0], '*');
legend([optpoint,fcont], {'Solution','Objective'}, 'Location', 'Southwest');
hold off

Accepted Answer

Cris LaPierre
Cris LaPierre on 7 Apr 2023
See the description of the function handle input on the fcontour documentation page
"Specify a function of the form z = f(x,y). The function must accept two matrix input arguments and return a matrix output argument of the same size."
The simplest solution is to define 2 anonymous functions; one for ga and one for fcontour.
One other correction. You wrote your anonymous function to only use the first 2 values of the input rather than all values. I have adjusted it to treat the first column as x and the second column as y.
% GA without constraints
% Objective function
f1 = @(x) (x(:,1).^4) + ((2.*x(:,1).^2).*x(:,2)) + x(:,2).^2 +3;
f2 = @(x,y) x.^4 + 2*x.^2.*y + y.^2 +3;
% LHS of linear inequalities
A = [];
% RHS of linear inequalities
B = [];
% No linear equalities
Aeq = [];
Beq = [];
% Nonlinear constraints
nonlcon = [];
lb = [-Inf, -Inf];
% Alternatively:
% lb = []
ub = [Inf, Inf];
options = optimoptions('ga','ConstraintTolerance',1e-8,'PlotFcn', @gaplotbestf);
% Solve the problem
[x, fval] = ga(f1, 2, A, B, Aeq, Beq, lb, ub, nonlcon, options);
Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
fprintf("The optimal decision is:\nx = %f\ny = %f\nThis solution has value %f\n", x(1), x(2), fval);
The optimal decision is: x = 2.205098 y = -4.881599 This solution has value 3.000366
figure
hold on
fcont = fcontour(f2, [-10 10 -50 50],'LevelStep',200);
optpoint = scatter(x(1), x(2), 200, [0 0 0], '*');
legend([optpoint,fcont], {'Solution','Objective'}, 'Location', 'Southwest');
hold off
  3 Comments
Cris LaPierre
Cris LaPierre on 8 Apr 2023
The algorithm uses random sampling to find a minimum. Depending how it samples run to run can lead to different results depending on your function. One solution is to use multiple runs, another is to set the random number generator seed. See here:

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!