How store evolution of x over the iterations of lsqnonlin?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I'm solving a nonlinear system of equations with lsqnonlin and want to save the evolution of values of X.
The ouput of the system is defined by a function file 'error.m', which takes X with size 4x1 and returns a vector of residuals with size 8x1.
I run a script with the structure below that sets the some fixed parameters for error.m, then calls the lsqnonlin solver and error.m for n cases.
clear all,clc,close all
load measure;
%initialization: set some constant inputs for 'error.m' and options for the
%solver
% solve nCases
for i=1:nCases
inputs.measured=measure(i);
X=lsqnonlin(@(x)error(inputs,x),x0,lb,ub,options)
save X X;
end
I want to store the values of X over the iterations, not only the solution. There are these examples in Optimization Solver Output Functions, but they use nested functions and I don't know how to adapt them to my script structure.
function stop = myoutput(x,optimvalues,state);
stop = false;
if isequal(state,'iter')
history = [history; x];
end
end
How will the output function 'myoutput' access the variable X inside the lsqnonlin?
I've tried to write the myoutput at the end of the main script, predefined history = [ ] in the for loop i=1:nCases and set options = optimset('OutputFcn', @myoutput);
However, the function 'myoutput' doesn't find th ehistory variable to append. Why is that happening?
1 comentario
Respuestas (2)
Torsten
el 18 de Jul. de 2024
Editada: Torsten
el 18 de Jul. de 2024
If you can't use the nested approach, define "history" as a persistent variable and call "myoutput" after the computation has finished with a specific value of the "state" variable (e.g. 'plot') to plot deduced results from the "history" matrix.
function stop = myoutput(x,optimvalues,state);
persistent history
if isempty(history)
history = [];
end
stop = 0;
if strcmp(state,'iter')
history = [history; x];
end
end
2 comentarios
Steven Lord
el 18 de Jul. de 2024
The ouput of the system is defined by a function file 'error.m',
I want to store the values of X over the iterations, not only the solution.
What are you hoping to do with this information? Are you trying to avoid evaluating your function repeatedly for the same inputs? If so use memoize instead of trying to build your own cache.
In fact, you could consider going the memoize route regardless of your goal. Set the CacheSize property to a sufficiently large value then ask for the stats of the MemoizedFunction returned by memoize afterwards.
f = memoize(@factorial);
clearCache(f)
f.CacheSize = 100;
for k = randi(10, 1, 20) % 20 random numbers between 1 and 10
tic
y = f(k);
t = toc;
fprintf("Computed factorial of %02d in %f seconds.\n", k, t)
end
s = stats(f);
s.Cache
s.Cache.Inputs{1}
This tells me factorial was called three times (from HitCounts) with input 5 (once initially then twice more with the result returned from the cache.) If you look at the list, you can see that the second and third calls were much faster than the first.
0 comentarios
Ver también
Categorías
Más información sobre Surrogate Optimization en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!