setting up code to run matrix of parameter values

2 views (last 30 days)
Hi,
I've written the code I've put below to run two functions sequentially. I'm trying to see what the effect of changing the parameters I've made global is on the output of the function, so I'd like to set the code up such that it will run combinations of the parameters' possible values. Preferably, I'd love it if it would run all of the possible permutations at once and put them into a .csv file, but that may be too complicated, so running the combinations in sets put into a .csv file would work well too. I've put all their possible values in matrices below, but I have no idea how to go about setting up a for loop so that it will run permutations of these values. As of now, I can get one set of parameter values to successfully run with the final values of the sets of equations be put into a .csv file. Any help would be much appreciated!
global F_1 F_2 R_R1 R_R2 V_1 V_2
%flow rates
F_1 = 0.25 %[0.25 0.5 1.25 2.5];
F_2 = 0.25 %[0.25 0.5 1.25 2.5];
%resource
R_R1 = 1e-5 %[3e-7 1e-5];
R_R2 = 1e-5 %[3e-7 1e-5];
%culture vessel volume
V_1 = 0.5 %[0.25 0.5];
V_2 = 0.5 %[0.25 0.5];
for i=1
soloalgae
tworesource_soloalgae_algaedaphnia
end
global equil
csvwrite('MATLAB.csv',equil,0,0)

Accepted Answer

Walter Roberson
Walter Roberson on 26 May 2011
global F_1 F_2 R_R1 R_R2 V_1 V_2
global equil
F1list = [0.25 0.5 1.25 2.5];
F2list = [0.25 0.5 1.25 2.5];
R1list = [3e-7 1e-5];
R2list = [3e-7 1e-5];
V1list = [0.25 0.5];
V2list = [0.25 0.5];
[F1grid, F2grid, R1grid, R2grid, V1grid, V2grid] = ...
ndgrid(F1list, F2list, R1list, R2list, V1list, V2list];
N = numel(F1grid);
equils = cell(N,1);
for K = 1:N
F_1 = F1grid(K);
F_2 = F2grid(K);
R_R1 = R1grid(K);
R_R2 = R2grid(K);
V_1 = V1grid(K);
V_2 = V2grid(K);
soloalgae
tworesource_soloalgae_algaedaphnia
equils{K} = equil;
end
After that the task just becomes one of writing out all the values of the cell matrix equils. You did not indicate the type or shape of equil, and you did not indicate how you wanted the results to be written out relative to each other. (Do the results include the input parameters? If not then you would want the inputs written too so you know what you are looking at.)
csvwrite('MATLAB.csv', cell2mat(equils));
might be enough.
  3 Comments
Walter Roberson
Walter Roberson on 27 May 2011
Replace
equils{K} = equil;
with
equils{K} = [F_1,F_2,R_R1,R_R2,V_1,V_2,equil];
There is no apparent way to prevent csvwrite from overwriting the data, so I suggest that you use dlmwrite() directly (csvwrite() calls dlmwrite() to do the actual work) using the '-append' flag of dlmwrite().
LS
LS on 27 May 2011
It's working! Thank you so much!

Sign in to comment.

More Answers (1)

Clemens
Clemens on 27 May 2011
I would do that by nested for loops:
global F_1 F_2 R_R1 R_R2 V_1 V_2
global equil
datamat = [];
for F_1 = [0.25 0.5 1.25 2.5]
for F_2 = [0.25 0.5 1.25 2.5]
for R_R1 = [3e-7 1e-5]
for R_R2 = [3e-7 1e-5]
for V_1 = [0.25 0.5]
for V_2 = [0.25 0.5]
soloalgae
tworesource_soloalgae_algaedaphnia
datamat = [datamat; [F_1,F_2,R_R1,R_R2, V_1,V_2,equil]];
end,end,end,end,end,end
gives you a nice datamat in the format of the penultimate line. Write with
csvwrite('result.csv',datamat)
  2 Comments
LS
LS on 27 May 2011
Thank you!! This is also very helpful! But I'm having the same problem with writing the CSV file - after I let the model run a few times, I stop the program to check the CSV file and the only thing in it is one set of parameters. I tried making a little for loop for it, so Matlab would write the data in decreasing rows, but it didn't work:
for i=1:N
csvwrite('MATLAB.csv',datamat, N, 0)
end
LS
LS on 27 May 2011
Never-mind, I got it to work - thank you!!

Sign in to comment.

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!