Please, I have limited time for a project. I'll like to know what this code does
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Nganyu Derick
el 7 de Jun. de 2015
Comentada: Nganyu Derick
el 8 de Jun. de 2015
variables = {'as','aw','ah'};
N = length(variables);
%create variables for indexing
for v = 1:N
eval([variables{v},' = ', num2str(v),';']);
end
%Defining the lower bounds
lb = zeros(size(variables));
lb([as,aw,ah]) = [0,0,0];
%Defining the upper bounds
ub = Inf(size(variables));
ub([as,aw,ah]) = [Ns,Nw,Nh];
%Entrying linear inequality constraints
A = zeros(2,3);
A(1,[as,aw,ah]) = [-Ps(i),-Pw(i),-Ph(i)];
b(1) = -P_load(i);
A(2,[as,aw,ah]) = [Ps(i),Pw(i),Ph(i)];
b(2) = P_max;
%Linear Equality Constraints
Aeq=[];
beq=[];
%Objective Function
f = zeros(size(variables));
f([as aw ah]) = [Cus*Ps(i)*T Cuw*Pw(i)*T Cuh*Ph(i)*T];
%Solving the problem with linprog
[x fval] = linprog(f,A,b,Aeq,beq,lb,ub);
for d = 1:N
fprintf('%12.2f \t%s\n',x(d),variables{d});
end;
aso(i)=x(1), awo(i)=x(2), aho(i)=x(3);
P_Supply(i)=aso(i)*Ps(i)+awo(i)*Pw(i)+aho(i)*Ph(i);
cost(i)=fval/P_Supply(i);
2 comentarios
John D'Errico
el 7 de Jun. de 2015
Editada: John D'Errico
el 7 de Jun. de 2015
http://www.mathworks.com/matlabcentral/answers/29922-why-your-question-is-not-urgent-or-an-emergency
If you have limited time, then perhaps you should have started sooner. Regardless, it is difficult to know what some random piece of lightly commented code does, given no real information. It is an optimization, but if you need to know more, then talk to the person you got it from.
Jan
el 7 de Jun. de 2015
@Nganyu Derick: Of course you have a limited time for your project. This is the nature of projects. If you want to get a useful answer soon, care for a meaningful question. Your question is very general and it is not clear if you need an explanation of the "=" operator or of the command length(). You've posted code, which does not run due to several errors. Therefore we do not have teh faintest chance to guess reliably, what it should do. In consequence posting this question consumes the time of you and the readers without the possibility to obtain a useful answer.
Respuesta aceptada
Jan
el 7 de Jun. de 2015
The code is very strange. It is overly complicated and near to be obfuscated.
Original:
variables = {'as','aw','ah'};
N = length(variables);
%create variables for indexing
for v = 1:N
eval([variables{v},' = ', num2str(v),';']);
end
Simplified (as Walter has posted already):
as = 1;
aw = 2;
ah = 3;
Original:
lb = zeros(size(variables));
lb([as,aw,ah]) = [0,0,0];
ub = Inf(size(variables));
ub([as,aw,ah]) = [Ns,Nw,Nh];
Simplified:
lb = zeros(1, 3);
ub = [Ns,Nw,Nh]; % The values of Ns, Nw and Nh do not appear in your code
Original:
A = zeros(2,3);
A(1,[as,aw,ah]) = [-Ps(i),-Pw(i),-Ph(i)];
b(1) = -P_load(i);
A(2,[as,aw,ah]) = [Ps(i),Pw(i),Ph(i)];
b(2) = P_max;
Simplified:
A = [-Ps(i),-Pw(i),-Ph(i); ...
Ps(i),Pw(i),Ph(i)];
b = [-P_load(i), P_max];
But notice, that "i" is not defined in the posted code.
Original:
%Objective Function
f = zeros(size(variables));
f([as aw ah]) = [Cus*Ps(i)*T Cuw*Pw(i)*T Cuh*Ph(i)*T];
Simplified:
f = [Cus*Ps(i), Cuw*Pw(i), Cuh*Ph(i)] * T;
Seeing the original code let me assume, that the author does not want the program to be understandable on purpose. The missing variables i,Ns,Nw,Nh let me think, that this program will not run at all. The actual command linprog(f,A,b,Aeq,beq,lb,ub) is not reached.
2 comentarios
Walter Roberson
el 7 de Jun. de 2015
I think the poster extracted from the original. The original is odd.
Más respuestas (2)
Ayush
el 7 de Jun. de 2015
Editada: John D'Errico
el 7 de Jun. de 2015
@Nganyu Derick
ERROR: Undefined function or variable 'Ns'.
PLease re-check your code
2 comentarios
John D'Errico
el 7 de Jun. de 2015
Editada: John D'Errico
el 7 de Jun. de 2015
So? I gave an answer, as much as it deserved. I told them to ask the source. Perhaps you should give them a complete dissertation on what this code does, since you were so willing to volunteer my time.
Nganyu Derick
el 7 de Jun. de 2015
Editada: Walter Roberson
el 7 de Jun. de 2015
Walter Roberson
el 7 de Jun. de 2015
It appears to me that it does some kind of load-flow optimization for an electrical system with three suppliers.
I think I must be missing some of what it is doing, as it looks to me as if the problem should be mechanically solvable without using linprog by using the least expensive source first up to its maximum capacity, the next most expensive source next, and so on, until the load has been met. My mind is a bit cloudy at the moment so I do not have confidence in that analysis.
3 comentarios
Walter Roberson
el 7 de Jun. de 2015
The first set of lines is a fancy way of making the assignment
as = 1; aw = 2; ah = 3;
lb is then set to a 1 x 3 vector of 0s, and then those locations in lb are overwritten by 0 (a useless step).
ub is set to a 1 x 3 vector of infinities, and then those values are overwritten by [Ns, Nw, Nh], leading to the same result as if you had just done ub = [Ns, Nw, Nh]
The code you quote here could be replaced with
lb = zeros(1,3);
ub = [Ns, Nw, Nh];
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!