MATLAB: Unable to plot 3-d surface with fmincon

plotplot 3-dplot surface

i want to plot 3-D surface ,basically i have two variables (intialguess value) plot with my objective function(fitnessfun).which show something local minima in the plot .Can you give me code suggestion.and i easily plot objective function with iteration . i want with my variables
xo=[0.001,0.005]; % assumptions
lb=[0.0001,0.003]; %lower bound
ub=[0.01,0.08]; %upper bound
nonlincon = @(x)constraint_new(x); % calling constraint function
Fitnessfun =@(x)weight_testvariable(x); %calling objective function
options = optimoptions(@fmincon,'Display','iter-detailed','Algorithm','sqp','MaxIterations',1500)
options = optimset('Display','iter','TolFun',1e-6)
options = optimset('PlotFcns',@optimplotfval);

Best Answer

In my understanding you are trying to visualize the two variables and objective function value with respect to the iterations. The values of variable that are taken in course of optimizing the objective function can be monitored using optimValues parameter.
For plotting the variables with iteration and fval:
  • You can write a Custom plot function using this link. The reference file in the link can show the syntax in which you can get the X(1), X(2), fval and iteration number.
  • This information you can append in three variables and plot fval as color intensity using surf.
  • Try to append the values in current iteration in an array so that you may plot current and previous values also to the surface plot.
  • For every PlotFcns option the .m file is there in MATLAB and you can take help of how to access data in between of iterations.
For Plotting the data in 2D plots:
  • Other way is to plot the 2D curve that contains variables value on one subplot and fval on other subplot with respect to the iterations. This can be done as follow:
options = optimset('PlotFcns',{@optimplotx,@optimplotfval});
If you want to return the outputs so that it may get used later :
  • Return the values of variables and fval that are taken between iterations by writing a custom OutputFcn for which syntax is mentioned here.