[英]Matlab coding help matrix manupulation
I am working on this coding for my heat transfer class. 我正在为我的传热课程编写代码。 The number are suppose to be other equations but I replaced them with numbers to simply it
这个数字被认为是其他方程式,但我用数字代替它们
l=.2;
w=.2;
dx=.05;
dy=.05;
k=400;
q=500;
Nx = (l/dx+1); %nodes in the x direction
Ny = (w/dy+1); %nodes in the y direction
T=zeros(Nx,Ny);
for m = (1:Nx) %node counter in x nodes
for n = (1:Ny) %node counter in y nodes
if n==1; %left side
T(m,n)=50;
elseif m==1 && n<Ny;%Heat Flux
T(m,n)=60;
elseif m>=2 && n==Ny && m<Nx; %insulated, right side
T(m,n)=70;
elseif n>=2 && n<=Ny-1 && m==Nx ; %insulated, bottom side
T(m,n)=80;
elseif m>=((.325*l)/dx)+1 && m<=((.675*l)/dx)+1 && n>=((.325*w)/dy)+1 && n<=
((.675*w)/dy)+1;
T(m,n) = 400;%center or steam
elseif m>1 && m<Nx && n>1 && n<Ny
T(m,n) = 90;
elseif m==1 && n==Ny;
**T(m,n)=T(m+2,n)/2;%**<-------------------this wont work properly****
elseif n==Nx && m==Ny;
T(m,n)= 110;
end
end
end
I am not sure why it will not choose the correct value and divide it by 2 rather it is saying the answer is 0 when it is suppose to be T(2,5) which is 70/2=35? 我不确定为什么它不会选择正确的值并将其除以2而不是说当答案是T(2,5)时它是0(= 20 = 35)?
T =
50 60 60 60 0
50 90 90 90 70
50 90 400 90 70
50 90 90 90 70
50 80 80 80 110
any help would be appreciate it 任何帮助都会很感激
Thanks 谢谢
As I see it, 照我看来,
You are trying to access the element of the Matrix which is still initialized to zero. 您正在尝试访问仍初始化为零的Matrix元素。 T Matrix is Zero(5,5).
T矩阵为零(5,5)。
And the condition : if m==1 && n==Ny
gets executed early in the iteration, at which time, m=1, and n=5 and T(2,5)=0. 条件:
if m==1 && n==Ny
在迭代的早期执行,那时m = 1,n = 5,T(2,5)= 0。
So you have T(1,5)=0 所以你有T(1,5)= 0
So, I would suggest you debug the code and you check the content of Matrix M at the end of each iteration. 所以,我建议你调试代码,并在每次迭代结束时检查Matrix M的内容。
The reason T(1,5) is zero is that the indicated line of code is executed at a point when T(3,5) is still set to zero - it has not yet been filled with the value 70, and therefore T(1,5) is set to 0/2 = 0. T(1,5)为零的原因是所指示的代码行在T(3,5)仍被设置为零时执行 - 它尚未填充值70,因此T( 1,5)设置为0/2 = 0。
If you loop through the values of m
in reverse order, by modifying the code to be 如果以相反的顺序遍历
m
的值,则通过修改代码
for m = (Nx:-1:1)
you'll find that T(3,5) is now set to 70 before T(1,5) is set, and T(1,5) is now correctly set to 35. 你会发现T(3,5)现在在设置T(1,5)之前设置为70,而T(1,5)现在正确设置为35。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.