How to specify limits for lsqnonlin
24 views (last 30 days)
Show older comments
I would like to specify that variables in my lsqnonlin fit are real. The other vectors in my problem are complex so I cannot use a fully real solver.
My function has the following form. r and theta are real-valued coordinate matrices. Intensity is a complex valued matrix. p is the real-valued and positive vector (at least that is what I want to specify). Hej is the function to be minimized.
Hej= @(p)(p(1)*besselj(1,p(2)*r)+(p(4)*besselj(1,p(6)*r).*cos(theta+p(8))+p(9)*besselj(1,p(6)*r).*sin(theta+p(8)))*exp(1i*p(5))).*(r/a<=1) ...
+ (p(1)*besselj(1, p(2)*a)/besselk(1, p(3)*a)*besselk(1,p(3)*r)+...
(p(4)*besselj(1, p(6)*a)/besselk(1, p(7)*a).*cos(theta+p(8))+p(9)*besselj(1, p(6)*a)/besselk(1, p(7)*a).*sin(theta+p(8))).*exp(1i*p(5))).*(r/a>1)...
-Intensity;
opts = optimoptions(@lsqnonlin,'DiffMaxChange', 0.1,'FinDiffType', 'central', 'Display','off','MaxFunEvals',2E7,'TolFun',1E-18,'TolX',1E-24,'MaxIter',4E3);
x0 = st; % arbitrary initial guess
lb = 0.0*ones(size(st));
[p_estimated,resnorm,residuals,exitflag,output] = lsqnonlin(Hej,x0, lb,[], opts);
The initial guess is given as real-valued and positive vector.
So how do I specify that the only valid solution are positive and realvalued?
0 Comments
Accepted Answer
Matt J
on 18 Sep 2014
Edited: Matt J
on 18 Sep 2014
Implement the objective function as follows, splitting Hej into real and imaginary parts,
function out=objective(p,Intensity,r,theta)
Hej = (p(1)*besselj(1,p(2)*r)+(p(4)*besselj(1,p(6)*r).*cos(theta+p(8))+p(9)*besselj(1,p(6)*r).*sin(theta+p(8)))*exp(1i*p(5))).*(r/a<=1) + (p(1)*besselj(1, p(2)*a)/besselk(1, p(3)*a)*besselk(1,p(3)*r)+(p(4)*besselj(1, p(6)*a)/besselk(1, p(7)*a).*cos(theta+p(8))+p(9)*besselj(1, p(6)*a)/besselk(1, p(7)*a).*sin(theta+p(8))).*exp(1i*p(5))).*(r/a>1)-Intensity;
out=[real(Hej); imag(Hej)]; %Split into real and imaginary parts
end
and the minimization as
lsqnonlin(@(p)objective(p,Intensity,r,theta) , x0, lb,[], opts);
8 Comments
Matt J
on 19 Sep 2014
Way, way, way over-complicated. You have functions within functions within cells, whereas my original proposal was just two lines of numeric MATLAB operations...
More Answers (1)
Roger Wohlwend
on 18 Sep 2014
I am afraid you cannot order the optimizing function to search for a real solution. If you want a real solution you have to make sure that your function Hej returns only real values. Since you use lsqnonlin as optimizing function your goal is to minimize the sum of the squared elements of the vector Hej. In my opinion, it does not make sense to minimize the sum of squared complex numbers. It would make more sense to minimize the sum of the absolute values of the complex numbers. So perhaps you should adjust Hej in a way that it returns the absolute values of the complex numbers instead of the complex numbers. If you do that, Hej returns only real values. As a consequence lsqnonlin should search for real solutions only.
See Also
Categories
Find more on Particle Swarm 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!