Avoiding negative values in fminsearch

22 views (last 30 days)
Hello, I am doing optimisation of a guassian function to calculate the midpoint and spread when the function below goes to zero. But when using fminsearch i get negative values.What i am supposed to get is 24 and 4 respectively please help!!
AA=1.580740308;
BB=0.854284221;
format long g
invalues=[1,10];
fun1=@(x)((AA-sum(A.*real(exp(-((E-x(1)).^2/x(2).^2)))))^2+...
(BB-sum(B.*real(exp(-((E-x(1)).^2/x(2).^2)))))^2);
[x,fval]=fminsearch(fun1,invalues)
  3 Comments
Matt J
Matt J on 5 Feb 2016
Your attachment didn't make it. Make sure you confirm the upload.
Also, it would help if you show the output that you did get. Realize that x(2)=+4 and x(2)=-4 produce the same value of fun1, so they are equivalent.
Rena Berman
Rena Berman on 24 Jan 2017
(Answers dev) Restored question.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 5 Feb 2016
All of your terms involving x are squared, so there is no way to tell a negative value of the term from a positive value of the term. Negative x are valid solutions.
It is not possible to constrain fminsearch to prevent it from using negative x. The closest you could get would be to add a penalty, such as
fun1=@(x)((AA-sum(A.*real(exp(-((E-x(1)).^2/x(2).^2)))))^2 + ...
(BB-sum(B.*real(exp(-((E-x(1)).^2/x(2).^2)))))^2) + ...
10^100 * any(x < 0);
  3 Comments
Walter Roberson
Walter Roberson on 6 Feb 2016
You need a global minimizer if you are getting caught in local minima. fmincon and fminunc are not able to escape sufficiently deep local minima either.
The approach I would use would be something like using the fun1 with penalty, and selecting a range of values to be probed, and
x1min = 0; x1max = 50;
x2min = 0; x2max = 50;
[X1, X2] = ndgrid( linspace(x1min, x1max, 20), linspace(x2min, x2max, 20));
[xvals, fvals] = arrayfun(@(x1,x2) fminsearch(fun1,[x1,x2], struct('TolX', 1e-12, 'MaxFunEvals', 2000, 'MaxIter', 2000)), X1, X2, 'Uniform', 0);
[bestfval, minidx] = min(cell2mat(fvals(:)));
bestx1x2 = xvals{minidx};
Now the minima is at x1 = bestx1x2(1), x2 = bestx1x2(2), with value bestfval
If the value were still too high (which it isn't) then you would increase the "20" to a higher number to get a more refined starting grid.
If you try this using the f1 without penalty you end up with an even better minima at -120.423695515953 -96.8967338642581
Walter Roberson
Walter Roberson on 8 Feb 2016
If this solves the problem, please Accept the answer.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 9 Feb 2016
Edited: Matt J on 9 Feb 2016
Applying abs() to x(1) in the objective function will effectively constrain it to be positive,
fun1=@(x)((AA-sum(A.*real(exp(-((E-abs(x(1))).^2/x(2).^2)))))^2+...
(BB-sum(B.*real(exp(-((E-abs(x(1))).^2/x(2).^2)))))^2);
With the objective function written this way, even if fminsearch returns a negative solution x, a positive solution can be immediately derived from it as abs(x).

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!