How to make MATLAB loop not use the same number?
Afficher commentaires plus anciens
i ask about matlab give me the same bus , i want location be for example 2,19,25
N=50; % Number of search agents
Max_iter=100; % Maximum number of iterations
lb=[2 975 2 975 2 975 ];
ub=[33 975 33 975 33 975 ];
dim=6;
fobj= @(x) Sphere(x);
but it give me loc 2 2 2
X_opt_loc =
2 2 2
X_opt_size =
975 975 975
I would be glad to give me solution for my problem.
37 commentaires
Dyuman Joshi
le 11 Mar 2023
"How to make MATLAB loop not use the same number?
Where is the loop?
What are bus and location supposed to mean? What are you trying to do?
This looks like an incomplete code, there is no meaning of these lines of code. Show the full code and specify what you are trying to do. Only then can somebody help you.
Rose Toto
le 11 Mar 2023
Dyuman Joshi
le 11 Mar 2023
You still did not answer my question - What is location supposed to mean?
How are you obtaining X_opt_loc and X_opt_size? What is the use of other variables?
Show all relevant code.
Rose Toto
le 11 Mar 2023
Walter Roberson
le 11 Mar 2023
Are you calling fmincon? Are you calling surrogateopt()? Are you using problem based optimization?
We need to see your code.
Rose Toto
le 12 Mar 2023
Rose Toto
le 12 Mar 2023
Walter Roberson
le 12 Mar 2023
The error is on line 19.
Steven Lord
le 12 Mar 2023
It is impossible to answer your question with the limited information you have provided. It's like you complained that when you followed a recipe you didn't get what you wanted. That could be because you didn't follow the recipe correctly (your oven temperature was too high), because you used an incorrect ingredient (sugar instead of salt), or you followed the wrong recipe entirely (you wanted bread but instead followed a recipe for making candy canes)!
- Post the code you've written.
- Describe the problem you're experiencing as though we are completely unfamiliar with the problem you're trying to solve.
- Ask a specific question.
If you don't do that I'm not sure we will be able to help you.
Rose Toto
le 13 Mar 2023
Modifié(e) : Walter Roberson
le 13 Mar 2023
Rose Toto
le 13 Mar 2023
Rose Toto
le 13 Mar 2023
Rose Toto
le 13 Mar 2023
Rose Toto
le 13 Mar 2023
Walter Roberson
le 13 Mar 2023
Rose Toto
le 13 Mar 2023
Walter Roberson
le 13 Mar 2023
No, you must be using a different function. The WHO there has
if size(ub,1)==1
ub=ones(1,dim)*ub;
lb=ones(1,dim)*lb;
end
You are passing in row vectors for ub and lb, so size(ub,1)==1 is true for those, so the code would attempt to do matrix multiplication between two row vectors, which is going to fail. The code posted in that contribution cannot work with the code you posted.
If you make ub and lb into column vectors instead of row vectors then you can get past that error message, but the code that assigns random positions to the stallions and the foals is wrong when the ub and lb are column vectors.
I cannot test further without the code for Sphere though.
Rose Toto
le 13 Mar 2023
Rose Toto
le 13 Mar 2023
Rose Toto
le 13 Mar 2023
Rose Toto
le 13 Mar 2023
Walter Roberson
le 14 Mar 2023
I cannot test without your Sphere.m
You agreed that you are using https://www.mathworks.com/matlabcentral/fileexchange/90787-wild-horse-optimizer so I installed WHO from there.
Rose Toto
le 14 Mar 2023
Modifié(e) : Walter Roberson
le 14 Mar 2023
Rose Toto
le 14 Mar 2023
Rose Toto
le 14 Mar 2023
Modifié(e) : Walter Roberson
le 14 Mar 2023
Walter Roberson
le 14 Mar 2023
Okay but we need the files 'loaddata331bus.m' and 'linedata331bus.m' to test with.
Jan
le 14 Mar 2023
By the way, you can simplify your code:
% Original:
for i=1:nob
P(i,1)=(loadbus(i,2)/(1000*MVAb));
Q(i,1)=(loadbus(i,3)/(1000*MVAb));
end
% Easier without a loop:
P = loadbus(:, 2) / (1000 * MVAb);
Q = loadbus(:, 3) / (1000 * MVAb);
% Original:
%% EVCS 1
optimal_loc_1=round(x(1));
P(optimal_loc_1,1)=P(optimal_loc_1,1)+x(2)/(1000*MVAb);
%% EVCS 2
optimal_loc_2=round(x(3));
P(optimal_loc_2,1)=P(optimal_loc_2,1)+x(4)/(1000*MVAb);
%% EVCS 3
optimal_loc_3=round(x(5));
P(optimal_loc_3,1)=P(optimal_loc_3,1)+x(6)/(1000*MVAb);
%% DG 1
optimal_loc_4=round(x(7));
P(optimal_loc_4,1)=P(optimal_loc_4,1)-x(8)/(1000*MVAb);
%% DG 2
optimal_loc_5=round(x(9));
P(optimal_loc_5,1)=P(optimal_loc_5,1)-x(10)/(1000*MVAb);
%% DG 3
optimal_loc_6=round(x(11));
P(optimal_loc_6,1)=P(optimal_loc_6,1)-x(12)/(1000*MVAb);
% Easier with a loop:
for k = [1,3,5,7,9,11]
signx = 1 - 2*(k >= 7);
optimal_loc = round(x(k));
P(optimal_loc) = P(optimal_loc) + signx * x(k+1) / (1000*MVAb);
end
These lines have no effect and are confusing clutter only:
R;X;P;Q;
C;
e=1;
endnode;
g;
srn;
srn;
srn;
srno;
bc;
Lc;
bc;
Vb;
va;
Without a pre-allocation letting an array grow iteratively requires a lot of ressources:
for i=1:nob
va(i,2:3)=vbp(i,1:2);
end
for i=1:nob
va(i,1)=i;
end
Wither create va with a zeros() command before the loops, of easier without a loop:
va = [(1:nob).', vbp(:, 1:2)];
Note: Simpler code is faster and easier to debug.
Rose Toto
le 15 Mar 2023
Modifié(e) : Walter Roberson
le 15 Mar 2023
Rose Toto
le 15 Mar 2023
Modifié(e) : Walter Roberson
le 15 Mar 2023
Rose Toto
le 15 Mar 2023
Jan
le 15 Mar 2023
I do not understand the meaning of these 3 comments.
Rose Toto
le 15 Mar 2023
Jan
le 15 Mar 2023
@Rose Toto: What does this sentence mean: "those lines on sphere?" On which sphere? Please add a verb to this sentence or explain in in other words.
I do not know what "this is line data33" means also and this is a puzzle also:
load data
1 0 0 0
2 100 60 0
...
What is the connection of this list of numbers to the problem?
The code line:
va;
does nothing. So it is a waste of time only. You use this repeatedly, but why?
Walter Roberson
le 15 Mar 2023
Jan, they posted the contents of two needed data files that for unknown reason have been named as .m files. Those are the missing files needed to run.
Rose Toto
le 16 Mar 2023
Jan
le 16 Mar 2023
Sorry, as long as I cannot guess what "those lines on sphere" means, I'm out of this discussion, because I cannot understand the problem.
Walter Roberson
le 16 Mar 2023
Jan, the OP is asking why you are saying that those particular lines in the function "sphere" are useless clutter.
Réponses (0)
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!