How to obtain a contour plot from given function?

function cost = obj_function(x)
cost =(x(1)).^2+(x(2)-3).^2;
end
%% the lines after comments are new files but in the same folder(the below is the constraints)
function [c, ceq] = nonlcon(x)
ceq = (x(1).^2)+(3*x(2))-3;
c = x(1).^2.*4 + x(2).^2.*9 - 36;
%%below is the rest of it
LB = [];
UB = [];
X0 = [-1, 1000000];
A = [];
B = [];
Aeq = [];
Beq = [];
[x, fval] = fmincon(@obj_function, X0, A, B, Aeq, Beq, UB, LB, @nonlcon)
My entire function is given above. It maybe confusing to understand but how can i implement my function into contour function?

 Respuesta aceptada

Mario Malic
Mario Malic el 2 de Nov. de 2020
Editada: Mario Malic el 2 de Nov. de 2020
You need to obtain all x values and their corresponding objective function values. Reference to the answer that the idea is taken from.
function cost = obj_function(x)
peristent X_Vals F_Vals
if ~nargin
cost.X_Vals = X_Vals;
cost.F_Vals = F_Vals;
return
end
% Initialising values
X_Vals(1,:) = [0 0];
F_Vals(1,1) = 0;
cost =(x(1)).^2+(x(2)-3).^2;
X_Vals(end+1,:) = x;
F_Vals(end+1,1) = cost;
end
After optimisation is finished call objective function without arguments, it will return a structure containing the needed values.
cost = obj_function();
You'll get only points with the values, I am not sure if that is enough for the contour plot, if not, then check out scatteredInterpolant function.
Also, make sure you delete first row from both variables, since those are just values to initialise the variable. There's also another approach to run your optimisation nested, that way you can have variables in your workspace when optimisation is finished. You can find an example of it if you search the documentation or this website.

4 comentarios

Utku Senel
Utku Senel el 3 de Nov. de 2020
Editada: Utku Senel el 3 de Nov. de 2020
Thanks for your answer, but where should I call my function? When I call it right after the last "end" it won't see it. I tried to call it in a seperate script but this time it says "not enough input arguments". Apart from that, when I run the first part of your code I get this which I don't understand.
Replace your cost function with my code, add this after you call fmincon
Values = obj_function();
Utku Senel
Utku Senel el 3 de Nov. de 2020
It worked, thank you for your answer. I will look up the link you added for the contour.
Mario Malic
Mario Malic el 3 de Nov. de 2020
Great! You're welcome, here's an answer that can get you started with this task - https://www.mathworks.com/matlabcentral/answers/633869-surface-plot-using-fitrpg#answer_532484
P.S. Don't forget to remove the first row in X_Vals and F_Vals.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Function Creation en Centro de ayuda y File Exchange.

Preguntada:

el 2 de Nov. de 2020

Comentada:

el 3 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by