Index in position 2 exceeds array bounds (must not exceed 5).

1 vue (au cours des 30 derniers jours)
Carrie Thomas
Carrie Thomas le 25 Mar 2020
Commenté : Carrie Thomas le 25 Mar 2020
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 commentaires
Jesus Sanchez
Jesus Sanchez le 25 Mar 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?
Carrie Thomas
Carrie Thomas le 25 Mar 2020
It says the error is in line 30

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 25 Mar 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.
  1 commentaire
Carrie Thomas
Carrie Thomas le 25 Mar 2020
I originally had the value of n calculated, but it gives the same error.

Connectez-vous pour commenter.

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by