MATLAB: How to loop on multiple indicies

input dataloopvariables

I have the following code that performs the calculation, but I need to repeat that calculation for about 100 sets of input values. Specifically, I need to vary the values for paro, mmhgo, flowrate, sechii, and transit. I can use a for loop for one of the variables, but I do not know how to code for changing them all simultaneously. I imagine that the code could read one line (row) of input data from a file with each field (column) corresponding to one of my variables just mentioned. In other words, for one set of calculations the inputs would be paro=44.3, mmgho=0.022, flowrate=1.7e7, sechii=45, and transit=5. For another run, paro=49.27, mmhgo=0.09, flowrate=5.69e10, sechii=76, and transit=23. Any suggestions on how to do this are much appreciated.
mixingconst=4; %number of times in one day that a day's volume of water is homogenized
sechii=76; %in cm
flowrate=1.70E+10; %Flow rate in L day-1
paro=49.29; %surface mean monthly PAR value mol m-2 day-1
mmhgo=[0.09,0.022]; %initial MMHg concentration in ng L-1
transit=14*mixingconst; %transit days * the number of mixing times each day
kz=(1.44/(sechii/100))/10; %puts kz into units of 0.1 m-1
kd=0.011; %mean value from incubation experiments
depth=70; %this I hold constant
z = 1:depth; %each layer is 0.1 m
parz = paro*exp(-kz*z); %light penetration equation
layerconst=exp(-kd*parz); %the loss fraction at each layer
mulconstant = sum(layerconst) / depth; %the loss fraction for the whole water column
mmhgnew = mmhgo * cumprod(repmat(mulconstant, 1, transit)); %the progression of water column concentrations (cumulative product).
mmhgmnew=mmhgnew*(flowrate/1e9); %the progression of water column mass (cumulative product)
mmhgmo=mmhgo*(flowrate/1e9); %water column mass on day 0
mmhgmloss=mmhgmo-mmhgmnew(transit) %cumulative mass lost

Best Answer

Hello Peter,
The way you mentioned, having all the constants in a big matrix and looping through just the rows, would work fine. Or you can just have each constant as a vector of the same length, and loop through the indices. Something like this:
% Define parameters
paro = [44.3 49.27]; % Or however many entries you want
mmhgo = [0.022 0.09];
flowrate = [1.7e7 5.69e10];
sechii = [45 76];
transit = [5 23];
% Preallocate output variable(s)
mmhgmloss = zeros(size(paro));
% Perform calculation for each parameter set
for k = 1:numel(paro)
kz=(1.44/(sechii(k)/100))/10;
%... perform calculation indexing into each parameter ...
mmhgmloss(k) = mmhgmo-mmhgmnew(transit(k));
end
Hope that helps give you a framework to get started.
-Cam