필터 지우기
필터 지우기

Difficulty calling diagonals in tri-diagonal matrix

조회 수: 2 (최근 30일)
Mary Fasulo
Mary Fasulo 2020년 3월 1일
댓글: Walter Roberson 2020년 3월 2일
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
Mary Fasulo
Mary Fasulo 2020년 3월 1일
If you open up my code, you'll see that I have to utilize each value of the diagonals of the matrix in my TDMA algorithm. When I run the code I get errors and it is not doing what the function is supposed to. And like I've stated multiple times I am extremely new to matlab so my wording of things isn't perfect.
Guillaume
Guillaume 2020년 3월 1일
"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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 3월 1일
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
Walter Roberson
Walter Roberson 2020년 3월 2일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by