maximization with fmincon which of the two codes provided do the correct job

조회 수: 1 (최근 30일)
I have the following maximization problem , solve for a_j:
s.t
k_j = 1x6, R_t= 6x1 , u = @(a) 1/(1-gamma)*a.^(1-gamma); gamma=2 ,
in the code W is 6x6 matrix , where each row correspond to k_j
code 1 : I have implemented as
clear all
W = csvread('optw.csv'); % Assuming W.csv contains a 6x6 matrix , where each row correspond to k_j
R = csvread('rt.csv'); % Assuming R.csv contains a 6x249 matrix
% Define the objective function
gamma = 2;
n = 6; % Number of variables (a_j)
u = @(x) 1/(1-gamma) * x.^(1-gamma);
obj = @(a) -sum(u(sum(a.* (W' * R), 1)));
% Set up optimization problem
Aeq = ones(1, n); % Equality constraint: sum_{j=1}^{j=6} a_j = 1
beq = 1; % Equality constraint value
lb = zeros(n, 1); % Lower bounds for variables a_j
ub = ones(n, 1); % Upper bounds for variables a_j
% Solve the optimization problem
options = optimoptions('fmincon', 'Display', 'iter'); % Set additional options if needed
[a_opt, fval] = fmincon(obj, ones(n, 1) / n, [], [], Aeq, beq, lb, ub, [], options);
% Display the optimal solution and objective value
disp("Optimal a_j:");
disp(a_opt');
disp("Objective value:");
disp(-fval);
however another objective function I implemented provide different results :
W = csvread('optw.csv'); % Assuming W.csv contains a 6x6 matrix
R = csvread('rt.csv'); % Assuming R.csv contains a 6x249 matrix
% Define the objective function
gamma = 2;
n = 6; % Number of variables (a_j)
u = @(x) 1/(1-gamma) * x.^(1-gamma);
obj = @(a) -sum(u(sum(a .* (W* R), 1)));
% Set up optimization problem
Aeq = ones(1, n); % Equality constraint: sum_{j=1}^{j=6} a_j = 1
beq = 1; % Equality constraint value
lb = zeros(n, 1); % Lower bounds for variables a_j
ub = ones(n, 1); % Upper bounds for variables a_j
% Solve the optimization problem
options = optimoptions('fmincon', 'Display', 'iter'); % Set additional options if needed
[a_opt, fval] = fmincon(obj, ones(n, 1) / n, [], [], Aeq, beq, lb, ub, [], options);
% Display the optimal solution and objective value
disp("Optimal a_j:");
disp(a_opt');
disp("Objective value:");
disp(-fval);
Can you please help me which objective function is doing the correct calculation based on the the problem I described former.
Thanks in advance
  댓글 수: 4
Torsten
Torsten 2023년 6월 18일
편집: Torsten 2023년 6월 18일
If R_t is 6x1, the k_j must be 6x1, too, for that k_j^T*R_t can be evaluated.
Az.Sa
Az.Sa 2023년 6월 18일
편집: Az.Sa 2023년 6월 18일
R at observation t : is 6x1 , R does not depend on j
the dimention of k_j: is 1x6 ( you are correct , I editted the question by removing the tranpose sympole on k_j and make the k_j: is 1x6 )
we sum the objective function evaluated at each t and maximize the sum over the vector of m_j

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2023년 6월 18일
편집: Torsten 2023년 6월 18일
I suggest using a function instead of a function handle.
I compared it with your implementation and random matrices for W and R. The second code gives the same result.
function value = obj(a,W,R)
value_gamma = 2;
u = @(x) 1/(1-value_gamma) * x.^(1-value_gamma);
vec = a.'*W;
value = 0;
for i = 1:size(R,2)
value = value + u(vec*R(:,i));
end
value = -value;
end
  댓글 수: 3
Torsten
Torsten 2023년 6월 19일
Play a little:
syms a [2,1] real
syms W [2,2] real
syms R [2,4] real
s1 = sum(a.* (W' * R))
s1 = 
size(s1)
ans = 1×2
1 4
s2 = sum(a.'* (W * R))
s2 = 
size(s2)
ans = 1×2
1 1
Az.Sa
Az.Sa 2023년 6월 19일
that is so great! Thank you very much for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by