Avoiding negative values in fminsearch
Mostrar comentarios más antiguos
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 comentarios
John D'Errico
el 29 de En. de 2016
What is E?
Matt J
el 5 de Feb. de 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
el 24 de En. de 2017
(Answers dev) Restored question.
Respuesta aceptada
Más respuestas (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).
Categorías
Más información sobre Solver Outputs and Iterative Display en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!