Find the optimized values for which the function is maximum ?

1 view (last 30 days)
I want to find the values of variables V1, V2 and V3 for which the function f gives the maximum value, the constraint for variables are V1 + V2 = 1 and 0<=V3<=2*pi
My function is as follows
if true
function f = myfun(x)
Eout = Em.*exp(angle(fftshift(ifft2(fftshift...
(sqrt(x(1))*Emmf11a+sqrt(x(2))*Emmf11b.*exp...
(-1i*(x(3)*pi))))))); % 1080 X 1080 matrix
Pow_Eout = sum(sum(abs(Eout).^2));
f = (abs(abs(sum(sum(E_LP.*conj(Eout))))^2)/1.2123/Pow_Eout);
end
I have used the fminsearch algorithm as follows but i am not getting the correct values for V1, V2 and V3.
if true
clear all;
close all;
load Emmf_GI_LP_g20_50um_72m_LCOS % LP-modes of GI-MMF
Emmf11a = E(:,:,5); % 1080 X 1080 matrix
Emmf11b = E(:,:,6); % 1080 X 1080 matrix
load EField_9um_2m_LCOS_100; %E-field of SMF
E_LP = E; % 1080 X 1080 matrix
u1 = 0.5;
u2 = 0.5;
u3 = 0.0;
Emmf_u = sqrt(u1).*Emmf11a + sqrt(u2).*Emmf11b.*exp(1i*(pi*u3));
Emmf_u = fftshift(Emmf_u);
Em = ifft2(Emmf_u); % 1080 X 1080 matrix
[x,fval] = fminsearch(@(x)myfun(x,Em,Emmf11a,Emmf11b,E_LP),[0;0;0],...
optimset('TolX',1e-8));
end
is there any other algorithm that i can use and how can i enter the contraints also
  1 Comment
Miroslav Balda
Miroslav Balda on 4 Jun 2013
I am affraid that your code will not work because of a strange application of the function myfun in fmincon. It is also unclear why your codes are surrounded by the useless "if true, ..., end" condition. You say that you are searching variables V1, V2, V3, however, they do not appear in your code at all.
You are asking whether there is another algorithm which may solve your problem. Being the author of the function LMFnlsq, I would use it. It solves a set of nonlinear equations by minimizing a sum of squares of their residuals, i.e . differences of left and right hand sides. If you are looking for a maximum of the function, you simply change theirs signs. Constrains to the solution is introduced by additional residuals. The possible form of the function evaluating residuals might be as follows:
function residuals = myfun(x)
global "list of all variables but x that are needed to evaluate residuals"
V = x.*V0; % for scaling purposes. V0 should appear in global list.
residuals = ...
-["a code for the normal residuals (may be several lines)"
(V(1)+V(2)-1)*w(1)
(V(3)<0)*V(3)*w(2)
(V(3)>2*pi)*V(3)*w(3)
];
The last three equations are artificial residuals = penalties for a violation of constraints put on the solution V. Variables "w" are user chosen weights of particular residuals. They should appear in the global list. The function myfun is needed by the function LMFnlsq that can be found in the File Exchange collection under
www.mathworks.com/matlabcentral/fileexchange/17534
The main program module could have the following structure:
clear all;
close all;
global "list of all variables but V that are needed to evaluate residuals"
"initial part of the program with loading needed matrices"
V0 = [v1; v2; v3]; % Initial guess of the solution
[x,ssq,cnt] = LMFnlsq(@myfun,ones(size(V0)), 'Display',-10)
V = x.*V0; % Scale the solution
:
It is all I can tell you at a moment.
Good luck!
Mira

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!