how to run different for loops in MATLAB

1 view (last 30 days)
PEMAX = [10 20 25 30 35 40];
CNOM = [50 75 90 100 125 150];
PBMAX = [20 30 40 50 60 70];
for k=1:length(PEMAX)
Pemax=PEMAX(k);
for z=1:length(PBMAX)
Pbmax=PBMAX(z);
for q=1:length(CNOM)
Cnom=CNOM(q);
Matlaboptimization
end
end
end
Hello everyone, I am trying to make the "Matlaboptimization" program work for the different values of Pemx, Pbmax and Cnom that I have defined. The problem I have encountered so far is that the program only works for the first 3 values of my variables, without continuing with all the other simulations (being 3 variables with 6 possible values I should have 6*6*6 simulations performed). Also, I don't know how to save every result of my simulations, I would like to save each of these separately, is that feasible? thanks for your help

Accepted Answer

Alan Weiss
Alan Weiss on 26 Nov 2021
Usually, people write functions that accept arguments and return arguments, such as
result(k,z,q) = Matlaboptimization(Pemax,Pbmax,Cnom);
You would put that line inside your nested for loops, and at the end you would have a result array holding all the results. You should also write a line just before your nested for loops allocating the result array, such as
result = zeros(length(PEMAX),length(PBMAX),length(CNOM));
Alan Weiss
MATLAB mathematical toolbox documentation

More Answers (1)

Voss
Voss on 24 Nov 2021
I'm not sure how Matlaboptimization.m gets called at all; you should call it in your code without the .m extension, i.e.:
PEMAX = [10 20 25 30 35 40];
CNOM = [50 75 90 100 125 150];
PBMAX = [20 30 40 50 60 70];
for k=1:length(PEMAX)
Pemax=PEMAX(k);
for z=1:length(PBMAX)
Pbmax=PBMAX(z);
for q=1:length(CNOM)
Cnom=CNOM(q);
Matlaboptimization
end
end
end
I assume Matlaboptimization is a script that relies on variables called Pemax, Pbmax, and Cnom. One way to save all the results separately is to add some code immediately after the call to Matlaboptimization that does that, saving to a .mat file with a file name like "results_Pemax=10_Pbmax=20_Cnom=50.mat", for instance. Whatever name makes sense to you and will allow you to know which run it corresponds to when you have to use them later. Another way would be to have Matlaboptimization do the saving itself.
Regardless, I think you should get 6*6*6 = 216 sets of results, not 6! = 720.
  1 Comment
lorenzo vallerini
lorenzo vallerini on 24 Nov 2021
Yes you right, I'm sorry. When I launch the program I do not use "Matlaboptimization.m" but the script, I inserted "Matlaboptimization.m" them only because it is a long enough script and I'm not sure if I can post it. AlsoI have already verified that it works, in fact if I use only 1 value for Pemx, Pbmax and Cnom I get results. what I'm trying to do now is get 6 * 6 * 6 results, saving each of these individually. Using what I initially wrote, the program only uses the first values ​​of PEMX, PBMX and CNOM, then I would like to understand how to make the optimization be carried out for each value I consider.
In order to save all the result I would have to create a .mat file for the 216 results or Is it possible to do it mechanically?

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!