Difficulty calling diagonals in tri-diagonal matrix
Show older comments
Hi, this is the problem to be coded in matlab (Note that in my code I use "d" for the repeating right hand side matrix instead of "b". The entirety of my A and d matrices print correctly, but I'm having difficulty calling the tri diagonal rows in my TDMA algorithm portion of the code. Help would be greatly appreciated I'm very new to matlab. My code is attached.

9 Comments
Walter Roberson
on 1 Mar 2020
How does this differ from the previous time you asked this?
Walter Roberson
on 1 Mar 2020
You deleted your previous version of the question, including my comments and your follow up. That makes it necessary for everyone to start over without benefit of the previous discussion.
Mary Fasulo
on 1 Mar 2020
Mary Fasulo
on 1 Mar 2020
Walter Roberson
on 1 Mar 2020
In your previous version, I posted the exact syntax, and I described it again below: diag, open-bracket, name of variable that is an array, comma, integer that is the diagonal number, close-bracket.
The general rule for where to put a calculation is right before the first place that needs the result: no point in calculating something that you might not need to use.
This is only a general rule and not always true. If you will need a value multiple times in a loop, calculate it before the loop for efficiency. If you will need a value in most cases but not all special cases, then the code might be clearer to calculate the value anyhow instead of only at the places you prove you need it. This does not hold for expensive calculations however: only do those if you have to.
Walter Roberson
on 1 Mar 2020
Reposting in hopes of reaching more people, mostly annoys and confuses the volunteers. It leads the volunteers who had already contributed to either drop out of the discussion (feeling as if their assistance was unwanted) or else to nag the poster to prove that what the volunteer had been contributed earlier was wrong -- because if it wasn't wrong then it should not have been deleted.
If you want to push something on to the front page top of the list again, then just make a meaningful comment to it. Your postings do not get higher in the list or stay high longer for being a new question, only for having new material posted.
Guillaume
on 1 Mar 2020
In addition to walter comments (we really don't like people deleting their question once we've commented on them, it's waste of our time and we end up asking the same questions again and again), it's really not clear what it is you're asking.What does I'm having difficulty calling the tri diagonal rows actually mean? In matlab, and other programming languages the only things you call are functions.
Mary Fasulo
on 1 Mar 2020
Guillaume
on 1 Mar 2020
"When I run the code I get errors"
Well, yes I get "Index exceeds the number of array elements (1)." on line 23, since you try to access a(k) with k = 2 but you've defined a as a scalar value (a = -1; on line 5).
From the comment in the code, it sounds like the a on line 23 should be a different a than the a on line 5, and should be a diagonal of the A matrix, in which case:
- Walter has already told you in his answer how to extract a diagonal of the matrix. It's the diag function
- You actually need to extract the diagonal. Matlab doesn't read your comments to figure out what it should do
- To avoid bugs like this, don't reuse variable names. You're not limited to the 26 letters of the alphabet. Variables names can use more than one letter. I'd recommend you use complete words that actually explain what's in the variable, e.g. subdiagonal would be a much better name than a.
Answers (1)
Walter Roberson
on 1 Mar 2020
0 votes
Diagonals are not "rows".
You can extract a diagonal by calling diag() passing the rectangular matrix as the first parameter and passing the diagonal number as the second parameter.
You would do that at the point at which you needed the information.
If you are looping row by row for something like row reduction calculation, then the three elements you need are A(row_number, row_number-1:row_number+1) for the second row to the second-last row.
1 Comment
Walter Roberson
on 2 Mar 2020
a = diag(A, -1);
b = diag(A, 0);
c = diag(A, +1);
Just before you need to use the contents of a, b, c
You also have a problem that your a and c are shorter than b; your current code will lead to an index out of range because of that.
Categories
Find more on Operating on Diagonal Matrices 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!