Avoiding negative values in fminsearch
Show older comments
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
John D'Errico
on 29 Jan 2016
What is E?
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
on 24 Jan 2017
(Answers dev) Restored question.
Accepted Answer
More Answers (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).
Categories
Find more on Solver Outputs and Iterative Display 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!