How to resolve an error for forward substitution code?

2 ビュー (過去 30 日間)
JoBrr
JoBrr 2020 年 9 月 20 日
コメント済み: JoBrr 2020 年 9 月 21 日
Hi everyone,
I am currently trying to do some forward substitution to get the c matrix for a tridiagonal matrix using the following code:
%Define tridiagonal matrix
A=[5 1 0 0; 3 6 1 0; 0 2 8 3; 0 0 1 8; 0 0 0 2];
b=[0;0;0;1;10]; %column vector of constants
m=[0.6000; 0.3704; 0.1311; 0.2629]; %sub diagonal of lower triangular matrix of A
n=5; %number of rows
c(1)=b(1);
for i=2:n
c(i)=b(i)-m(i,i-1)*c(i-1)
end
When I run this code I get an error saying "Index in position 2 exceeds array bounds (must not exceed 1)", and this error is in the code c(i)=b(i)-m(i,i-1)*c(i-1).
What does this error mean and how can I resolve it?
Any form of help would be appreciated.

採用された回答

Walter Roberson
Walter Roberson 2020 年 9 月 21 日
m=[0.6000; 0.3704; 0.1311; 0.2629]; %sub diagonal of lower triangular matrix of A
That is a column vector, one column.
c(i)=b(i)-m(i,i-1)*c(i-1)
That attempts to access m(i,i-1). When i becomes 3, that would be m(3,2) but m only has one column.
m(i,i-1) would be code you would use to extract the first sub-diagonal from a full matrix, and would not be used to access values that were already extracted from the sub-diagonal.
  3 件のコメント
Walter Roberson
Walter Roberson 2020 年 9 月 21 日
m(i-1) or m(i) whichever fits your code.
JoBrr
JoBrr 2020 年 9 月 21 日
chur!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by