Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Torsten
Torsten il 18 Giu 2023
Modificato: Torsten il 18 Giu 2023
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 il 18 Giu 2023
Modificato: Az.Sa il 18 Giu 2023
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

Accedi per commentare.

Risposta accettata

Torsten
Torsten il 18 Giu 2023
Modificato: Torsten il 18 Giu 2023
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 Commenti
Torsten
Torsten il 19 Giu 2023
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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Systems of Nonlinear Equations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by