how to print fval and optimised values at each iteration?

Hi I have a very simple task: printing fval and x values of each iteration. I already printed x values of each iteration, as mentioned in Outputfunction. However I have no luck printing fval of each iteration.

2 Comments

You defined the function, and each iteration provides the x values... why not just call the function with those values?
How should I call and save it in every iterative? Because I can't call it on myoutput, can't I? Sorry I'm very new to Matlab.

Sign in to comment.

 Accepted Answer

Use the optimvalues structure to get fval, as described here

4 Comments

sadly replacing optimvalues with fval doesn't work, it only produce history of x values. Or I'm doing it wrong?
I can't tell what you're doing. The link I gave you explains that the iterative solver will pass 3 variables to your Output Function
stop = outfun(x, optimValues, state)
optimValues is a structure that reports fval as one of its fields. So you can get fval from there and display it from your output Function just as you are doing for x.
okay, I can call optimValues alone. But why I can't call both x values and optimValues by rewriting like this
history = [history;x;optimValues];
and what should I write instead
There are different things that you could do. My preference would be to make "history" a structure array
optimValues.x=x;
history = [history; optimValues];

Sign in to comment.

More Answers (0)

Asked:

on 14 Apr 2016

Commented:

on 14 Apr 2016

Community Treasure Hunt

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

Start Hunting!