How to resolve an error for forward substitution code?

2 次查看(过去 30 天)
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by