2017-11-25 67 views
0

我正在運行一個嵌套for循環的矩陣。我的問題在於,這些值出錯了,因爲循環會逐行填充矩陣。我想讓循環按列填充矩陣,以避免此問題。如何在Matlab中按列運行矩陣列?

T_i = 85;     %Initial temperature (K) 
T_inf = 20;    %Free stream temperature (K) 
h = 50;     %Convection heat transfer coefficient (W/m^2K) 
alp = 0.0000015;    %Thermal diffusivity (m^2/s) 
k = 15;     %Thermal conductivity (W/mK) 
del_x = 0.03;    %Incremental distance between center nodes (m) 
del_t = 300;    %Incremental time diference (s) 

Fo = alp*del_t/(del_x^2)  %Find the Numerical/Discretized Fourier Number 
Bi = h*del_x/k    %Find the Numerical/Discretized Biot Number 
T__vec = [85;85;85;85]  %Initial temperature vector for 4 node points. 
%T_inf_vec = 20+zeros(1:10) 


M=5    %No. of rows 
N=10    %No. of columns 


T_inf_vec = [20,20,20,20,20,20,20,20,20,20] 


A=zeros(M,N); 
A(1:4)= T__vec; 
A = [T_inf_vec;A]; 



for i=2:M 
    for j=2:N 
     T_p1=(2*Fo*A(i+1,j-1))+(2*Bi*Fo*A(i-1,j-1))+(((1-2*Fo)-(2*Bi*Fo))*A(i,j-1)) 
T_p11 = Fo*A(i-1,j-1)-2*Fo*A(i,j-1)+A(i,j-1)+Fo*A(i+1,j-1); 
      if i==2 
      A(i,j)= T_p1 
      elseif i<1 
      A(i,j)= 20 
      else 
      A(i,j)= T_p11 
      end 
     end 
    end 
+2

請仔細閱讀[在什麼情況下我想補充「緊急」或其他類似的短語我的問題,爲了獲得更快的答案?](// meta.stackoverflow.com/q/326569) - 總結是,這不是解決志願者問題的理想方式,而且可能會對獲得答案產生反作用。請不要將這添加到您的問題。 – halfer

回答

0

更改爲通過循環:

for j=2:M 
    for i=2:N 
     T_p1=(2*Fo*A(i+1,j-1))+(2*Bi*Fo*A(i-1,j-1))+(((1-2*Fo)-(2*Bi*Fo))*A(i,j-1)) 
     T_p11 = Fo*A(i-1,j-1)-2*Fo*A(i,j-1)+A(i,j-1)+Fo*A(i+1,j-1); 
     if i==2 
      A(i,j)= T_p1 
     elseif i<1 
      A(i,j)= 20 
     else 
      A(i,j)= T_p11 
     end 
     end 
    end