Problem to display a matrix?

1 view (last 30 days)
Ming Ki Lee
Ming Ki Lee on 23 Jan 2017
Edited: Niels on 24 Jan 2017
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!

Answers (3)

Niels
Niels on 23 Jan 2017
Edited: Niels on 23 Jan 2017
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 Comments
Ming Ki Lee
Ming Ki Lee on 23 Jan 2017
In my program, I want to write a matrix. mx, Xs is a variable and I want the first row of A is 0 except when j at Xs, Xs=Cs. A(k,j-1),A(k,j+1) and A(k,j) are in the middle of the matrix. The last row and the last two element of A will be A(mx,mx-1) and A(mx,mx). I change the code but still can't run it.
for j=1:mx
if A(1,j) == A(1,Xs)
A(1,j) = Cs;
else
A(1,j)=0;
end
end
for k=2:mx-1
for j=2:mx
A(k,j+1) = D*dt/(dx)^(2)-U*dt/(2*dx);
A(k,j) = -2*D*dt/(dx)^(2)+1;
A(k,j-1) = D*dt/(dx)^(2)+U*dt/(2*dx);
end
end
A(mx,mx-1) = (U*dt/dx);
A(mx,mx) = (-U*dt/dx+1);
disp(A)
end
Niels
Niels on 24 Jan 2017
Edited: Niels on 24 Jan 2017
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

Sign in to comment.


Stephen23
Stephen23 on 23 Jan 2017
Edited: Stephen23 on 23 Jan 2017
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:

Walter Roberson
Walter Roberson on 24 Jan 2017
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.

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!