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
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
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))
size(s1)
s2 = sum(a.'* (W * R))
size(s2)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!