How To multiply this Formula to run this code Please

3 次查看(过去 30 天)
Dear Eng.
How I can formulate this as either vectors or numeric only to unify the folmlua
A(i,:) = A(i,:) + h(i)*[0, ones(1,i-1), 0, ones(1,n-i)];
I mean to avoid multipling Numeric by Vector
In order to apply this:
n = 3
h(1) = 0.6;
h(2) = 0.8;
h(3) = 1;
T = 4;
% Objective function: minimize total power transmitted by all users
f = ones(n, 1);
% Inequality constraints: signal-to-interference ratio must exceed threshold
A = zeros(n,n);
b = zeros(n,1);
for i = 1:n
A(i,i) = -h(i);
A(i,:) = A(i,:) + h(i)*[0, ones(1,i-1), 0, ones(1,n-i)];
b(i) = -T*h(i);
end
% Lower bounds: power transmitted by each user must be non-negative
lb = zeros(n,1);
% Solve using linprog
[x, fval] = linprog(f, A, b, [], [], lb);
% Display results
disp('Optimal power transmitted by each user:');
disp(x);
disp(['Minimum total power transmitted: ', num2str(fval)]);
Thank You so much
  2 个评论
Torsten
Torsten 2023-5-2
How do you want to set the elements of the matrix A ?
The vector
[0, ones(1,i-1), 0, ones(1,n-i)];
has length n+1, thus cannot be a row of the matrix A which has to be mxn.
Dalia ElNakib
Dalia ElNakib 2023-5-2
编辑:Dalia ElNakib 2023-5-2
To set Matrix A to be (3*3) Please
Can you help me in that please? in order to be compatible with the Length of this command
Many Thanks for your cooperation

请先登录,再进行评论。

采纳的回答

MarKf
MarKf 2023-5-2
The fact that there is the need for a loop for these operations, that b is not simply obtained with b=-T.*h, that [ones(1,i-1), 0, ones(1,n-i)] produces [0 1 1] [1 0 1] [1 1 0] for each iteration and the diagonal value is added to it, I'm guessing that OP simply needs to eliminate the first 0 from that vector. I hope this code is not used to control infrastructure or anything important.
n = 3;
h = [0.6 0.8 1];
T = 4;
A = zeros(n);
b = zeros([n,1]);
for i = 1:n
A(i,i) = -h(i);
A(i,:) = A(i,:) + h(i)*[ones(1,i-1), 0, ones(1,n-i)];
b(i) = -T*h(i);
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by