Index exceeds matrix dimensions
Show older comments
Hey guys I'm facing a problem with my codes. I keep getting the message 'index exceeds matrix dimensions' and I can't seem to find where the problem is. I'm very new to Matlab. These are the codes I used:
Tinf = 5;
dx = 0.25;
dy = 0.25;
h = input('input a value for h: ');
k = input('input a value for k: ');
iter = input('specify the number of iterations, n: ');
C1 = 2/k;
C2 = 2/(h*dx);
C3 = 1/k;
C4 = 1/(h*dx);
T1 = 20;
T2 = 22;
T3 = 26;
T4 = 32;
T5 = 36;
T6 = 40;
T7 = 42;
T8 = 45;
T9 = 48;
T10 = 50;
T11 = 20;
T12 = 30;
T13 = 35;
T14 = 40;
T15 = 45;
T16 = 50;
T17 = 55;
T18 = 60;
T19 = 70;
T20 = 80;
T21 = 20;
T22 = 56;
T23 = 60;
T24 = 65;
T25 = 72;
T26 = 76;
T27 = 80;
T28 = 84;
T29 = 88;
T30 = 92;
for n=1:iter
T1(n+1) = (((T2(n)/C1) + (5/C2)) / ((1/C1) + (1/C2)));
T2(n+1) = (((T1(n+1)/C1) + (T3(n)/C1) + (T12(n)/C3) + (5/C4)) / ((1/C1) + (1/C1) + (1/C3) + (1/C4)));
T3(n+1) = (((T2(n+1)/C1) + (T4(n)/C1) + (T13(n)/C3) + (5/C4)) / ((1/C1) + (1/C1) + (1/C3) + (1/C4)));
T4(n+1) = (((T3(n+1)/C1) + (T5(n)/C1) + (T14(n)/C3) + (5/C4)) / ((1/C1) + (1/C1) + (1/C3) + (1/C4)));
T5(n+1) = (((T4(n+1)/C1) + (T6(n)/C1) + (T15(n)/C3) + (5/C4)) / ((1/C1) + (1/C1) + (1/C3) + (1/C4)));
T6(n+1) = (((T5(n+1)/C1) + (T7(n)/C1) + (T16(n)/C3) + (5/C4)) / ((1/C1) + (1/C1) + (1/C3) + (1/C4)));
T7(n+1) = (((T6(n+1)/C1) + (T8(n)/C1) + (T17(n)/C3) + (5/C4)) / ((1/C1) + (1/C1) + (1/C3) + (1/C4)));
T8(n+1) = (((T7(n+1)/C1) + (T9(n)/C1) + (T18(n)/C3) + (5/C4)) / ((1/C1) + (1/C1) + (1/C3) + (1/C4)));
T9(n+1) = (((T8(n+1)/C1) + (T10(n)/C1) + (T19(n)/C3) + (5/C4)) / ((1/C1) + (1/C1) + (1/C3) + (1/C4)));
T10(n+1) = (((T9(n+1)/C1) + (100/C1) + (T20(n)/C3) + (5/C4)) / ((1/C1) + (1/C1) + (1/C3) + (1/C4)));
T12(n+1) = (((T11(n+1)/C3) + (T13(n)/C3) + (T22(n)/C3) + (T2(n+1)/C3)) / (4*(1/C3)));
T13(n+1) = (((T12(n+1)/C3) + (T14(n)/C3) + (T23(n)/C3) + (T3(n+1)/C3)) / (4*(1/C3)));
T14(n+1) = (((T13(n+1)/C3) + (T15(n)/C3) + (T24(n)/C3) + (T4(n+1)/C3)) / (4*(1/C3)));
T15(n+1) = (((T14(n+1)/C3) + (T16(n)/C3) + (T25(n)/C3) + (T5(n+1)/C3)) / (4*(1/C3)));
T16(n+1) = (((T15(n+1)/C3) + (T17(n)/C3) + (T26(n)/C3) + (T6(n+1)/C3)) / (4*(1/C3)));
T17(n+1) = (((T16(n+1)/C3) + (T18(n)/C3) + (T27(n)/C3) + (T7(n+1)/C3)) / (4*(1/C3)));
T18(n+1) = (((T17(n+1)/C3) + (T19(n)/C3) + (T28(n)/C3) + (T8(n+1)/C3)) / (4*(1/C3)));
T19(n+1) = (((T18(n+1)/C3) + (T20(n)/C3) + (T29(n)/C3) + (T9(n+1)/C3)) / (4*(1/C3)));
T20(n+1) = (((T19(n+1)/C3) + (100/C3) + (T30(n)/C3) + (T10(n+1)/C3)) / (4*(1/C3)));
T22(n+1) = (((T21/C3) + (T23(n)/C3) + (T12(n+1)/C3)) / (4*(1/C3)));
T23(n+1) = (((T22(n+1)/C3) + (T24(n)/C3) + (T13(n+1)/C3)) / (4*(1/C3)));
T24(n+1) = (((T23(n+1)/C3) + (T25(n)/C3) + (T14(n+1)/C3)) / (4*(1/C3)));
T25(n+1) = (((T24(n+1)/C3) + (T26(n)/C3) + (T15(n+1)/C3)) / (4*(1/C3)));
T26(n+1) = (((T25(n+1)/C3) + (T27(n)/C3) + (T16(n+1)/C3)) / (4*(1/C3)));
T27(n+1) = (((T26(n+1)/C3) + (T28(n)/C3) + (T17(n+1)/C3)) / (4*(1/C3)));
T28(n+1) = (((T27(n+1)/C3) + (T29(n)/C3) + (T18(n+1)/C3)) / (4*(1/C3)));
T29(n+1) = (((T28(n+1)/C3) + (T30(n)/C3) + (T19(n+1)/C3)) / (4*(1/C3)));
T30(n+1) = (((T29(n+1)/C3) + (100/C3) + (100/C3) + (T20(n+1)/C3)) / (4*(1/C3)));
end
T1
T2
T3
T4
T5
T6
T7
T8
T9
T10
T11
T12
T13
T14
T15
T16
T17
T18
T19
T20
T21
T22
T23
T24
T25
T26
T27
T28
T29
T30
Can you guys please help me find where the problem is. Thank you
5 Comments
John D'Errico
on 8 Nov 2018
Perhaps this should be a good lesson then? Teaching you not to create such huge lists of numbered variables? Instead, learn to use vectors and arrays.
Finding a bug in that code would make Cthulhu himself want to run screaming from the room.
Elias Youwakim
on 8 Nov 2018
"Can you guys please help me find where the problem is"
The problem is using lots of numbered variables, which forces you to write complex, buggy code. Instead, use vectors, matrices, and arrays. Then you can write less code. Less code is easier to follow and understand. Code that is easier to follow and understand is less buggy.
Image Analyst
on 8 Nov 2018
Read this link first, then attach the full error message (ALL THE RED TEXT), not just a small snippet from it like you did.
Elias Youwakim
on 8 Nov 2018
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!