Avoiding negative values in fminsearch
Afficher commentaires plus anciens
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
John D'Errico
le 29 Jan 2016
What is E?
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.
Rena Berman
le 24 Jan 2017
(Answers dev) Restored question.
Réponse acceptée
Plus de réponses (1)
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).
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!