フィルターのクリア

tridiagonal matrix with a corner entry from upper diagonal

9 ビュー (過去 30 日間)
ivordes greenleaf
ivordes greenleaf 2015 年 4 月 22 日
回答済み: Jacob Lane 2019 年 4 月 16 日
I am trying a construct a matlab code such that it will solve an almost tridiagonal matrix. The input I want to put in is the main diagonal (a), the upper diagonal (b) and the lower diagonal and the RHS vector (r). The matrix I want to solve looks like this:
| a(1) b(1) |
| c(1) a(2) b(2) |
| c(2) a(3) b(3) |
| . . . |
| . . . |
| . . b(n-1) |
| b(n) c(n) a(n) |
I can construct a code that works for the tridiagonal, but that corner entry got me, especially when it is supposed to come from the original input c.
This is what I got so far:
function y = tridiagonal ( c, a, b, r )
n = length ( a );
for i = 1 : n-1 b(i) = b(i) / a(i); a(i+1) = a(i+1) - c(i) * b(i); end
r(1) = r(1) / a(1); for i = 2 : n r(i) = ( r(i) - c(i-1) * r(i-1) ) / a(i); end
for i = n-1 : -1 : 1 r(i) = r(i) - r(i+1) * b(i); end
if ( nargout == 0 ) disp ( r ) else y = r; end
Thanks for any input!
  1 件のコメント
Guillaume
Guillaume 2015 年 4 月 22 日
The c entry on the last row should be c(n-1) not c(n). Where does c(n) go? The last column of the first row?

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2015 年 4 月 22 日
There is actually a function in matlab to construct tridiagonal matrices. It's well hidden in the gallery function (with the tridiag option). I would construct the matrix this way:
a = 1:10; b = 101:110; c = 201:210; %demo data
m = full(gallery('tridiag', b, [a 0], c));
m(end-1, 1) = m(end-1, end); %move b(n) in first column
m(1, end-1) = m(end, end-1); %move c(n) in first row
m = m(1:end-1, 1:end-1) %get rid of last row and column
I assumed c(n) went in the last column of the first row, since it's c(n-1) that ends up on the last row.
  2 件のコメント
ivordes greenleaf
ivordes greenleaf 2015 年 4 月 22 日
Thank you for this, it is quite helpful, but just a short question, don't I have to use the notation ":" to enter all my input matrix, so my coefficients cannot be free-style, they have to be like a sequence (based on the demo)? And also I did try it, was the gallery function set to only take input in this notation?
Thank you again!
Guillaume
Guillaume 2015 年 4 月 22 日
No, the coefficients can be anything. a, b and c just have to be vectors (of the same length). For example:
a = rand(1, 20); b = ones(1, 20); c = randperm(20);
work just as well.

サインインしてコメントする。

その他の回答 (2 件)

Ahmet Cecen
Ahmet Cecen 2015 年 4 月 22 日
Check out spdiags and diag functions for a much easier way to do this.

Jacob Lane
Jacob Lane 2019 年 4 月 16 日
I am having trouble with trying to write a function for a tridiagonal matrix. I am just trying to prove that one is in fact tridiagonal. Can anyone help me?

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by