MATLAB: Loop and element-by-element operation on table

element by element operationfor loop

Hello, I want to do element-by-element calculation on three tables, but I have two problems 1. with setting the loop correctly and 2. error on inner matrix dimension must agree. I will appreciate help to get my code working. Thanks.
% read constants
T0 = 0; % temperature at the surface
Tm = 1300; % temperature at base of plate
k = 0.000001; % contant
alpha = 0.0000255; % constant
rhom = 3300; % density of the lithophere
L = 95; % 0:5:120;
% read table data
h = load('t.txt');
t = load('a.txt');
rho = load('sf.txt');
% compute T iteratively
for h=1:20
for t=1:20;
for rho=1:20;
a = (exp((-k*pi^2*t)./(L^2))*(sin((pi*h)./L)))+(0.5*exp((-k*4*(pi^2)*t)./(L^2))*(sin((2*pi*h)./L)))
b = (h./L)+(2/pi)*a; % first part
T = T0 + (Tm - T0)*(d); % main equation
rho_t = alpha*rhom (Tm - T); % density calculation
rho = rho_g - rho_t; % density difference
end
end
end
fid = fopen('out.dat','w');
fprint(fid,'%6.10f %6.10f 6.10f\n', T, rho_t, rho);
fclose(fid);

Best Answer

Hey, you have first loaded the values from text file in h, t and rho and then you have assigned them the value 1,2,3...20 so these variable now don't have the value you initially assigned them instead now they are a array of 1 to 20. And you are using 'd' which is not declared anywhere.