Problem to display a matrix?
1 次查看(过去 30 天)
显示 更早的评论
L=100;
tmax=60;
U=0.8;
D=1.6;
Xs=8;
Cs=10;
Xflux=36;
mx=40;
dt=100;
dx=10;
n = 0;
i = 1;
A = zeros(mx,mx);
B = zeros(mx,0);
C = zeros(n,i);
if (U*dx)/(2*D)<=1 && (D*dt)/dx^(2)<=2
for k=1:length(mx)
for j=1:length(mx)
A(k,j-1) = D*dt/(dx)^(2)+U*dt/(2*dx);
A(k,j+1) = D*dt/(dx)^(2)-U*dt/(2*dx);
A(k,j) = -2*D*dt/(dx)^(2)+1;
end
end
A(0,j) = 0;
A(0,Xs) = Cs;
A(mx,mx-1) = (U*dt/dx);
A(mx,mx) = (-U*dt/dx+1);
disp(A)
Above is my code. I want to display A but I don't know what went wrong. Hope someone can help!
0 个评论
回答(3 个)
Niels
2017-1-23
编辑:Niels
2017-1-23
here is whats gone wrong:
k=1:length(mx)
j=1:length(mx)
length(mx) = 1 since mx = 40, maybe cancel length
but even if you do you will get 2 more errors because if you start with j=1 A(1,0) is no valid index. And probably you dont want A to be a 40x41 matrix, what it will be if j=1:40 => A(k,j+1) for j=40. Additionally even if you let j run from 2:39, you will overwrite the your elements constantly... you may think again about what do you really want
2 个评论
Niels
2017-1-24
编辑:Niels
2017-1-24
you should just have set the condition as you explained it to me...
for j=1:mx
if j == Xs
A(1,j) = Cs;
else
A(1,j)=0;
end
end
2 more things.
1.: the middle of the matrix is so far ok, but you still overwrite kind of each element. think about it:
%first loop
k=2
second loop for
j=2
A(2,3) & A(2,2) & A(2,1) = something
% next iteration:
j=3 % k still =2
A(2,4) & A(2,3) & A(2,2) = something
as you can see you may have some kind of mistake there (to overwrite the previous set elements might not be your intention)
2.: the last row: except from the last two elements, none of the other elements is given any value by you - since k=2:xm-1 the last row is skipped
they will probably be filled with 0s but you shouldnt programm like this
Stephen23
2017-1-23
编辑:Stephen23
2017-1-23
This is a good example of how badly formatted code is makes writing bugs easy, and finding bugs hard. The code is badly aligned, but this once we align it consistently (ctrl+a then ctrl+i) then the bug is easy to find:
L = 100;
tmax = 60;
U = 0.8;
D = 1.6;
Xs = 8;
Cs = 10;
Xflux = 36;
mx = 40;
dt = 100;
dx = 10;
%
n = 0;
i = 1;
A = zeros(mx, mx);
B = zeros(mx, 0);
C = zeros(n, i);
%
if (U * dx) / (2 * D) <= 1 && (D * dt) / dx ^ (2) <= 2
for k = 1:length(mx)
for j = 1:length(mx)
A(k, j - 1) = D * dt / (dx) ^ (2) + U * dt / (2 * dx);
A(k, j + 1) = D * dt / (dx) ^ (2) - U * dt / (2 * dx);
A(k, j) = - 2 * D * dt / (dx) ^ (2) + 1;
end
end
A(0, j) = 0;
A(0, Xs) = Cs;
A(mx, mx - 1) = (U * dt / dx);
A(mx, mx) = (- U * dt / dx + 1);
disp(A)
Do you notice that there is no end to match the if ? I have no idea how the code should be fixed because its algorithm is not clear: is loop variable j intentionally accessed after the loop, or is this a mistake? There are other bugs too, but without any clues as to what the code should do...
In any case, beginners need to learn that those grumpy academics who keep telling students to correctly format their code are not doing it be annoying or for some sadistic pleasure. They keep giving the same advice "Format your code consistently" because badly formatted code is buggy (as this code clearly shows). For the same reason: always write code comments, use meaningful names, etc:
0 个评论
Walter Roberson
2017-1-24
In addition:
(U*dx)/(2*D)<=1
is false, so your if body is not entered.
If it were entered you would fail because A(0, j) = 0 attempts to index an array at index 0; indexes start from 1 in MATLAB.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!