MATLAB: How to input a table of variables into a for loop and output the results to a table

for loopinput dataMATLAB

My working code is below. It spits out a sequence of values for mmhgnew that represent the new concentration each day. What I want to do from here is calculate these mmhgnew values using many input values for flowrate, paro, mmhgo, and transit. I would like to have these input values read off of a table. I'm just not sure the best way to proceed. Any help appreciated. Thanks!
flowrate=5.79e10; %Flow rate in L day-1, this is a monthly averaged value
paro=49.29369; %surface mean monthly PAR value mol m-2 day-1
mmhgo=0.09; %initial MMHg concentration
transit=10
mmhgmo=mmhgo*flowrate; %initial MMHg mass in 1 day's worth of water, entire depth profile
for d=1:transit %d is the number of days of transit from source to export site
for z = 1:70 %z is the number of depth layers, 0.1 m each down to 7 m
parz(z)=paro*exp(-0.189*z) % the kz value in (0.1 m-1)
end
mmhgz=mmhgo*exp(-0.011*parz) %calcalates the concentration of MMHg in each 0.1 m layer using an average kd value
mmhgmz=flowrate*mmhgz/70 %calculates the mass of MMHg in each layer in ng day-1 (assumes water mass is evenly distributed with depth
mmhgmsum=sum(mmhgmz) %returns the mass of MMHg for the entire water column in ng
mmhgmloss=mmhgmo-mmhgmsum %returns the mass of MMHg lost by comparing to the initial MMHg mass
mmhgmo=mmhgmo-mmhgmloss %returns the new mass of MMHg in the water column after each day
mmhgo=mmhgmo/flowrate %returns the new concentration of MMHg in the water column after each day
mmhgnew(d)=mmhgo %outputs the new MMHg concentration for each day of transit
end

Best Answer

Assuming that you've not made any error in your formulas, each iteration of the d loop is just multiplying mmhgo by a constant, namely:
sum(exp(-0.011*parz)) / 70 %the flowrate cancels out
So you could simply your whole code to:
paro = 49.29369; %surface mean monthly PAR value mol m-2 day-1
mmhgo = 0.09; %initial MMHg concentration
transit = 10;
z = 1:70; %z is the number of depth layers, 0.1 m each down to 7 m
parz = paro*exp(-0.189*z); % the kz value in (0.1 m-1)
mulconstant = sum(exp(-0.011*parz)) / 70;
mmhgnew = mmhgo * cumprod(repmat(mulconstant, 1, transit))
If you wanted to repeat the calculation for various values of starting mmhgo or paro values, you could loop the above over these values or you could just put them into vectors along different dimensions and calculate the whole lot in one go. e.g:
%vectors along different dimension (but not rows)
startingmmhgo = [0.09, 0.08, 0.05]'; %column vector (along 1st dimension)
paro = permute([49.29369, 61.5], [1 3 2]); %along 3rd dimension
%calculate constants:
z = 1:70;
parz = paro .* exp(-0.189*z); %.* essential
mulconstant = sum(exp(-0.011*parz), 2) / 70; %sum(x, 2) essential to sum the columns together
%apply the formula to all inputs at once:
mmhgnew = startingmmhgo .* cumprod(repmat(mulconstant, 1, transit), 2); %cumprod(x, 2) essential
The column of the resulting mmhgnew matrix are as usual the evolution of mmhgo, the rows correspond to the various starting mmhgo and the 3rd dimension to the different paro values.
Unfortunately, you can't do the same for different transit values as this would generate different number of columns for each value. You will have to loop for that