Loop and element-by-element operation on table

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);

댓글 수: 4

What kind of data is in t.txt, a.txt and sf.txt?
Ope
Ope 2018년 5월 31일
Thanks Paridhi, 'd' = 'b', i fix that. the txt files contain a single column set of numbers each.
Jan
Jan 2018년 5월 31일
편집: Jan 2018년 5월 31일
Please post the complete error message. This is more efficient than guessing, which line causes the problem.
By the way: I do not see any tables in the code. Do you mean matrices? What is the contents of the loaded files? You overwrite the a,b,T, tho_t, rho in each iteration. In consequence you get one number from the last iteration only. Is this wanted? Seems to be a waste of time.
Ope
Ope 2018년 6월 1일
Thanks Jan. I have attached 't.txt'. It is a single column matrix which is similar in content structure to a.txt, and sf.txt. I use it for testing the code.

댓글을 달려면 로그인하십시오.

 채택된 답변

Paridhi Yadav
Paridhi Yadav 2018년 5월 31일

0 개 추천

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.

댓글 수: 5

Ope
Ope 2018년 5월 31일
see attach the test file I am using t.txt, same content as a.txt and sf.txt. Thanks
So instead of running for loop 3 times you can run it once for index i=1:20 and use index i to calculate like this
% 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 i=1:20
a = (exp((-k*pi^2*t(i))./(L^2))*(sin((pi*h(i))./L)))+(0.5*exp((-k*4*(pi^2)*t(i))./(L^2))*(sin((2*pi*h(i))./L)))
b = (h(i)./L)+(2/pi)*a; % first part
T = T0 + (Tm - T0)*(b); % main equation
rho_t = alpha*rhom (Tm - T); % density calculation
rho = rho_g - rho_t; % density difference
end
fid = fopen('out.dat','w');
fprint(fid,'%6.10f %6.10f 6.10f\n', T, rho_t, rho);
fclose(fid);
Ope
Ope 2018년 6월 1일
Thanks Paridhi. Since h and t have (i), I also include rho_g(i). I obtain single value for T, rho_t and rho. instead of a value for each (i). How can this be corrected.
Hey, you are only getting the value from last loop in T,rho_t and rho. Instead before running loop make a array of 20 for T, rho_t and rho. And use index i to store value in them just like we did to get the value of h and t.
T(i) = T0 + (Tm - T0)*(b); % main equation
rho_t(i) = alpha*rhom (Tm - T(i)); % density calculation
rho(i) = rho_g - rho_t; % density difference
Ope
Ope 2018년 6월 1일
Thanks, it finally works fine.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

Ope
2018년 5월 31일

댓글:

Ope
2018년 6월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by