Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab optimoptions save residual in variable

I'm using the optimoptions function and I want to save the residual after each iteration in a variable. Now if I choose to display the iterations in the first line of the following code, I could see them in console, but is there anyway to save them as a vector variable? I have tried to get the residual output in the second line but it gives me only the result of the last iteration. Thank you.

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

here is the display of iterations. 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
like image 350
jiayi Avatar asked Mar 05 '26 02:03

jiayi


1 Answers

I do not have the optimization toolbox available to test this but you should be able to get the residuals in each iteration by defining an output function that is called each iteration.

In your optimization options you add your function handle as 'OutputFctn', @myOutputFunction. In the function you can access the residual value (among other things) and either plot the values or save them to 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
like image 154
user1884905 Avatar answered Mar 07 '26 07:03

user1884905