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!