Just for reference, A is a 5x5 and b is a 1x5
Index in position 2 exceeds array bounds (must not exceed 5).
1 view (last 30 days)
Show older comments
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 Comments
Jesus Sanchez
on 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?
Answers (1)
Steven Lord
on 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.
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!