Index in position 2 exceeds array bounds (must not exceed 5).
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I'm trying to write a code to evaluate a system of equations, but keep getting the above error. Any help would be greatly appreciated!
function X = GaussPivot(A, b, n)
C = [A b];
scale = max(max(C));
C = (1/scale) * C;
for k = 1 : (n-1) %partial pivoting
p = k;
big = abs(C(k,k));
for ii = (k+1) : n
if abs (C(ii,k)) > big
big = abs(C(ii,k));
p = ii;
end
end
if p ~= k
for jj = k : (n+1)
dum = C(p,jj);
C(p,kk) = C(k,jj);
C(k,jj) = dum;
end
end
% forward elimination
for i = (k+1) : n
factor = A(i,k) / A(k,k);
for j = (k+1) : (n+1)
A(i,j) = A(i,j) - (factor * A(k,j));
end
end
end
% Back substitution
X(n) = A(n,n+1) / A(n,n);
for i = (n-1) : 1 : -1
SUM = A(i,n+1);
for j = i+1 : n
SUM = SUM - (A(i,j) * X(j));
end
X(i) = SUM / A(i,j);
end
end
[SL: formatted question as text not code]
3 comentarios
Jesus Sanchez
el 25 de Mzo. de 2020
What is your value for n? If its larger than 5, this code gives error, as A is 5x5 matrix. If this is not the case, could you specify in which line is the error located?
Respuestas (1)
Steven Lord
el 25 de Mzo. de 2020
Rather than trusting your user to enter a value of n compatible with the sizes of A and b, compute it from A and/or b using size.
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!