How to resolve an error for forward substitution code?

2 visualizaciones (últimos 30 días)
JoBrr
JoBrr el 20 de Sept. de 2020
Comentada: JoBrr el 21 de Sept. de 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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 21 de Sept. de 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 comentarios
Walter Roberson
Walter Roberson el 21 de Sept. de 2020
m(i-1) or m(i) whichever fits your code.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by