Matlab optimization store residuals in variable

I am using optimoptions function and I want to keep residual result after each iteration of the variable. Now if I want to display the iterations on the first line of the following code, I could see them in the console, but still save them as a vector variable? I tried to get residual output on the second line, but that only gives me the result of the last iteration. Thank.

options = optimoptions(@lsqnonlin,'Algorithm','Levenberg-Marquardt','Display','iter','StepTolerance',1e-4);
[params,resnorm,residual_opt,exitflag,output,lambda,jacobian] = lsqnonlin(@minDistance,params0,[],[],options);
angles = params(1:3);
R = euler2mat(angles);
T = params(4:6);
end

      

the iteration is displayed here. I just want to use the third colomn.

                                        First-Order                    Norm of 
 Iteration  Func-count    Residual       optimality      Lambda           step
     0           7      5.4943e+09        3.84e+10         0.01
     1          14     7.39183e+08        8.74e+09        0.001        6.32624
     2          21     4.56928e+07         1.1e+09       0.0001        2.59042
     3          28     2.41748e+06        1.21e+08        1e-05        2.61414
     4          35          135873        1.39e+07        1e-06        1.45824
     5          42         8031.22        1.65e+06        1e-07       0.743095
     6          49         487.971           2e+05        1e-08       0.372708
     7          56         30.0687        2.47e+04        1e-09       0.186396
     8          63         1.86599        3.07e+03        1e-10      0.0931815
     9          70        0.116209             382        1e-11      0.0465832
    10          77      0.00724993            47.7        1e-12      0.0232892
    11          84     0.000452687            5.96        1e-13       0.011644
    12          91     2.82764e-05           0.744        1e-14     0.00582186

      

+3


source to share


2 answers


I don't have an optimization toolkit available to test this, but you should be able to get the residuals at each iteration by defining an inference function called each iteration.

In your optimizations, you add your function descriptor as 'OutputFctn', @myOutputFunction

. In a function, you can access the residual value (among other things) and either plot the values โ€‹โ€‹or save them to a file.



function stop = myOutputFunction(x,optimValues,state)

% Do not use the user defined function to determine when to stop
stop = false;

% The following should be the value you are looking for in the 
% current iteration.
currentResidual = optimValues.residual;

end

      

+1


source


You cannot get all the leftovers directly without changing the function code.

However, if you want to plot the residuals for all iterations, you can do so by adding the "PlotFcn" parameter with the value "@optimplotfval". See documentation, optimization options for details . You can even make your own build function based on an existing one.

- Hint -



To create a custom timeline, start with one of these, for example optimplotfval

. You can paste the code into a new file and rename the function. Remove the unused command, for example change the title. Extract from the varargin

additional parameter that is the handle of the axes you want to use and use it in all construction functions.

To use this custom chart, do a close on your regular chart:

'PlotFcn', @(x,optimValues,state)mycustomplot(x,optimValues,state, hMyAxis)

      

+1


source







All Articles