MATLAB: Index calculations

index

Hi, I need to do index calculations. I've a vertical column divided into thin layers, for which properties are considered constant. The temperature T(i + 1) and the heat flow Q(i + 1), at the bottom of each layer, are determined from the temperature T(i) and the heat flow Q(i) at the top of each layer by
T(i+1) = T(i) + (Q(i).*Dz(i))./k(i) - (A(i).*Dz(i).^2)./2.*k(i)
and
Q(i+1) = Q(i) - A(i).*Dz(i)
where A(i) and k(i) are known for each layer and Dz(i) is the layer thickness and set to 1. Somebody can suggests me how implement in matlab this operation? thanks Gianluca

Best Answer

%define depth range and intervals [m]
z = (0:100:10000)'; % No addition [ and ] around vectors
%define variables
% k = zeros(size(z)); % No pre-allocation required
% A = zeros(size(z)); % No pre-allocation required
Q = zeros(size(z));
T = zeros(size(z));
To = 18; %surface temperature [°C]
Qo = 0.07; %surface heat flow [W/m2]
k = 2 + 0.00005 .* z; % Spaces around operators increase readability
A = 2.5e-6 .* exp(-0.0001 .* z); % 2.5e-6 is faster than 2.5*10^-6
diffz = diff(z);
Q(1) = Qo; % Once outside the loop
for i = 1:(length(z)-1)
Q(i+1) = Q(i) - A(i) .* diffz(i);
end
% Faster, but needs more memory:
% Q = cumsum([Qo; -A(1:end-1) .* diffz])
T(1) = To; % Once outside the loop
for i = 1:(length(z)-1)
T(i+1) = T(i) + (Q(i) .* diffz(i)) ./ ...
k(i) - (A(i) .* diffz(i) .^ 2) ./ 2 .* k(i);
end
plot(z,T)
The spaces around the operators are not required, but this can help to avoid ambiguities, compare:
2.*3
2. *3
2 .* 3
Related Question