MATLAB: How to save values column by column using a loop of for

for iteractionsfor loopMATLAB

I would like to save these values column by column, but the first for (j) is not working properly.
I tried a lot of different thins and could not find my error.
From the second column to the end, j=2 to j=size(temp_range), the code does not save more the values.
Could someone help me?
temp_range = 273:1:2100;
for j = 1:size(temp_range)
T_est_data = temp_range(1,j);
T_est_fit = temp_range(1,j);
for i=2:1:20
Ti = 298;
T_chama_est_fit = T_est_fit(i-1);
T_chama_est = T_est_data(i-1);
T = (T_chama_est+Ti)/2;
T_fit = (T_chama_est_fit+Ti)/2;
x=1;
y=4;
razaomist = 1;
cp_O2 = ru*(const_O2_A13_1000_5000(1) + const_O2_A13_1000_5000(2)*T + const_O2_A13_1000_5000(3)*T^2 + const_O2_A13_1000_5000(4)*T^3 + const_O2_A13_1000_5000(5)*T^4);
cp_N2 = ru*(const_N2_A13_1000_5000(1) + const_N2_A13_1000_5000(2)*T + const_N2_A13_1000_5000(3)*T^2 + const_N2_A13_1000_5000(4)*T^3 + const_N2_A13_1000_5000(5)*T^4);
cp_CO2 = ru*(const_CO2_A13_1000_5000(1) + const_CO2_A13_1000_5000(2)*T + const_CO2_A13_1000_5000(3)*T^2 + const_CO2_A13_1000_5000(4)*T^3 + const_CO2_A13_1000_5000(5)*T^4);
cp_H2O = ru*(const_H20_A13_1000_5000(1) + const_H20_A13_1000_5000(2)*T + const_H20_A13_1000_5000(3)*T^2 + const_H20_A13_1000_5000(4)*T^3 + const_H20_A13_1000_5000(5)*T^4);
cp_O2_fit = ru*(const_O2_A13_1000_5000(1) + const_O2_A13_1000_5000(2)*T_fit + const_O2_A13_1000_5000(3)*T_fit^2 + const_O2_A13_1000_5000(4)*T_fit^3 + const_O2_A13_1000_5000(5)*T_fit^4);
cp_N2_fit = ru*(const_N2_A13_1000_5000(1) + const_N2_A13_1000_5000(2)*T_fit + const_N2_A13_1000_5000(3)*T_fit^2 + const_N2_A13_1000_5000(4)*T_fit^3 + const_N2_A13_1000_5000(5)*T_fit^4);
cp_CO2_fit = ru*(const_CO2_A13_1000_5000(1) + const_CO2_A13_1000_5000(2)*T_fit + const_CO2_A13_1000_5000(3)*T_fit^2 + const_CO2_A13_1000_5000(4)*T_fit^3 + const_CO2_A13_1000_5000(5)*T_fit^4);
cp_H2O_fit = ru*(const_H20_A13_1000_5000(1) + const_H20_A13_1000_5000(2)*T_fit + const_H20_A13_1000_5000(3)*T_fit^2 + const_H20_A13_1000_5000(4)*T_fit^3 + const_H20_A13_1000_5000(5)*T_fit^4);
a = (x+(y/4))/razaomist ;
b = x ;
d = y/2;
f = ((1-razaomist)/razaomist)*(x+(y/4));
N_CxHy_reag = 1;
N_O2_reag = a;
N_N2_reag = a*3.76;
N_CO2_prod = b;
N_H2O_prod = d;
N_O2_prod = f;
N_N2_prod = 3.76*a;
%hreag = N_CxHy_reag*hstandar298_fit(10)+ N_O2_reag*hstandar298_fit(11)+ N_N2_reag*hstandar298_fit(9)
%entalp_form_co2 = hstandar298_data(7)
%entalp_form_h20 = hstandar298_data(8)
%entalp_form_n2 = hstandar298_data(9)
Tad_fit = (1/(N_CO2_prod*cp_CO2_fit+N_H2O_prod*cp_H2O_fit+N_O2_prod*cp_O2_fit+N_N2_prod*cp_N2_fit))*(N_CxHy_reag*hstandar298_fit(10)+ N_O2_reag*hstandar298_fit(11)+ N_N2_reag*hstandar298_fit(9)-N_CO2_prod*hstandar298_fit(6)- N_H2O_prod*hstandar298_fit(8)-N_O2_prod*hstandar298_fit(11)-N_N2_prod*hstandar298_fit(9)+Ti*(N_CO2_prod*cp_CO2+N_H2O_prod*cp_H2O+N_O2_prod*cp_O2+N_N2_prod*cp_N2));
Tad_data = (1/(N_CO2_prod*cp_CO2+N_H2O_prod*cp_H2O+N_O2_prod*cp_O2+N_N2_prod*cp_N2))*(N_CxHy_reag*hstandar298_data(10)+ N_O2_reag*hstandar298_data(11)+ N_N2_reag*hstandar298_data(9)-N_CO2_prod*hstandar298_data(7)- N_H2O_prod*hstandar298_data(8)-N_O2_prod*hstandar298_data(11)-N_N2_prod*hstandar298_data(9)+Ti*(N_CO2_prod*cp_CO2+N_H2O_prod*cp_H2O+N_O2_prod*cp_O2+N_N2_prod*cp_N2));
T_est_fit(i,j) = Tad_fit;
T_est_data(i,j) = Tad_data;
end
end

Best Answer

for j = 1:size(temp_range)
is not going to do what you want.
size(temp_range) is [1, 1828] and the : operator only uses the first return value of a function, 1:size(temp_range) is 1:1, so just 1.
for j = 1:numel(temp_range)
will do what you want.
Then we have,
for j = 1:numel(temp_range)
T_est_data = temp_range(1,j);
T_est_fit = temp_range(1,j);
for i = 1:20
...
T_est_fit(i,j) = Tad_fit;
T_est_data(i,j) = Tad_data;
end
end
At the beginning of each j step you completely erase the T_est_data and T_est_fit that you've created in previous steps and make them scalar. Obviously that makes no sense. You want to set the first row of the current j column. This requires indexing. Additional, for better performance the arrays should be predeclared. So:
T_est_data = zeros(20, numel(temp_range));
T_est_fit = zeros(20, numel(temp_range));
for j = 1:numel(temp_range)
T_est_data(1, j) = temp_range(j);
T_est_fit(1, j) = temp_range(j);
for i = 2:20
...
Finally, again for better performance all the constants (x, y, razaomist, a, b, etc.) should be defined outside of the loops.
Note that if you couldn't spot these problems just by reading your code, then you should have found them by debugging your code. Simply stepping through your code and looking at how the variables evolve would have shown you the problems.