How store evolution of x over the iterations of lsqnonlin?
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 4×1 and returns a vector of residuals with size 8×1.
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?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 4×1 and returns a vector of residuals with size 8×1.
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? 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 4×1 and returns a vector of residuals with size 8×1.
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? lsqnonlin, output, iteration, optimization MATLAB Answers — New Questions