How to resolve an error for forward substitution code?

2 vues (au cours des 30 derniers jours)
JoBrr
JoBrr le 20 Sep 2020
Commenté : JoBrr le 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.

Réponse acceptée

Walter Roberson
Walter Roberson le 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 commentaires
Walter Roberson
Walter Roberson le 21 Sep 2020
m(i-1) or m(i) whichever fits your code.
JoBrr
JoBrr le 21 Sep 2020
chur!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by