Avoiding negative values in fminsearch
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Percy Nebah
le 29 Jan 2016
Commenté : Rena Berman
le 24 Jan 2017
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 commentaires
Matt J
le 5 Fév 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.
Réponse acceptée
Walter Roberson
le 5 Fév 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 commentaires
Walter Roberson
le 6 Fév 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
Plus de réponses (1)
Matt J
le 9 Fév 2016
Modifié(e) : Matt J
le 9 Fév 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).
0 commentaires
Voir également
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!