Hi,

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.

## Best Answer