How to resolve an error for forward substitution code?

2 Ansichten (letzte 30 Tage)
JoBrr
JoBrr am 20 Sep. 2020
Kommentiert: JoBrr am 21 Sep. 2020
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 21 Sep. 2020
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 Kommentare
Walter Roberson
Walter Roberson am 21 Sep. 2020
m(i-1) or m(i) whichever fits your code.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by