Fixing Value in lsqnonlin
Show older comments
I have seen that based off the lsqnonlin algorithm, that the upper and lower bounds are not strictly respected for different iterations of lsqnonlin. I have a case where I am attempting to optimize a general N by N square array where some of the values should occassionally be fixed to 0. The issue is illustrated by the function used to optimize, shown below.
An example K can have the form [a, 0; b, c]. If I don't include the 0 in the optimization, the code runs with no problem. However, I would like to make this function general to any N by N array of K. The issue arrises when lsqnonlin changes the 0 to something like 1e-8 even though my upper and lower bounds are both 0, which in turn gets blown up as expm(1e8). Is there a good way to fix the value of the 0s?
Secondary question: I only inverted the K inside the optimzation function because the fit didn't work very well when I was optimizing the small decimal values. It seems that lsqnonlin thinks that a change of lets say 1/500 vs 1/600 is less significant than a change between 500 and 600. If this is something I can tune in the options that I have not discovered, I would happily optimize the non-inverted numbers.
Thanks!
function xout = Global_SAS_1(T,Y,K)
%if Y is n1 by n2, T needs to be size 1 x n2
%K can be an arbitrary matrix size. Will have units as T^-1.
Tm = zeros(numel(K),length(T));
Kinv = 1./K;
Kinv(isinf(Kinv)) = 0; % in matlab, 1/0 = inf
for i = 1:length(T)
A = expm(Kinv*T(i));
Tm(:,i) = A(:);
end
Gm = real(Y)*pinv(Tm);
xout = Gm*Tm - Y
5 Comments
The issue arrises when lsqnonlin changes the 0 to something like 1e-8 even though my upper and lower bounds are both 0
How old is your Matlab version? In a recent version of Matlab, that really shouldn't happen. When you make one of the lb(i)=ub(i), both lsqnonlin and lsqcurvefit internally reformulate the problem so that x(i) is treated as a known variable. I recommend you post a full enough version of your code that we can run the optimization.
Matt J
on 27 Aug 2019
Very kind of you, Adam!
Shawn Z
on 27 Aug 2019
Note also this related thread,
You may have to explicitly reshape K to have the original NXN dimensions in your model function if you set one or more lb(i)=ub(i).
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!