why I got orange warning about contour plot?
1 view (last 30 days)
Show older comments
Patis Thepsorn
on 7 Apr 2023
Commented: Patis Thepsorn
on 8 Apr 2023
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1348584/image.png)
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);
fprintf("The optimal decision is:\nx = %f\ny = %f\nThis solution has value %f\n", x(1), x(2), fval);
figure
hold on
fcont = fcontour(@(x,y) f([x y]), [-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
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);
fprintf("The optimal decision is:\nx = %f\ny = %f\nThis solution has value %f\n", x(1), x(2), fval);
figure
hold on
fcont = fcontour(@(x,y) f([x y]), [-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
0 Comments
Accepted Answer
Cris LaPierre
on 7 Apr 2023
"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);
fprintf("The optimal decision is:\nx = %f\ny = %f\nThis solution has value %f\n", x(1), x(2), fval);
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
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:
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!