Matrix "index in position 1 exceeds array bounds (must not exceed 1)"
2 views (last 30 days)
Show older comments
I made a code to solve systems of equations with the Gauss Seidel method and I get this error on line 27 "index in position 1 exceeds array bounds (must not exceed 1)"
clear;
close all;
clc;
clearvars;
i=1;
X1=0;
X2=0;
X3=0;
error_X1(i)=9999;
error_X2(i)=9999;
error_X3(i)=9999;
A=input("Escribe tu matriz A: ");
disp("Tu matriz A es: ");
disp(A);
b=input("Escribe tu matriz de resultado como columna: ");
disp("Tu matriz de resultado es: ")
disp(b);
error=input("Ingrese la tolerancia: ");
while (error_X1(i)>=error &&error_X2(i)>=error && error_X3(i) >= error)
X1(i+1)=(b(1,1)-A(1,2)*X2(i)-A(1,3)*X3(i))/A(1,1);
X2(i+1)=(b(2,1)-A(2,1)*X1(i+1)-A(2,3)*X3(i))/A(2,2);%here is the error
X3(i+1)=(b(3,1)-A(3,1)*X1(i+1)-A(3,2)*X2(i+1))/A(3,3);
error_X1(i+1)= abs((X1(i+1)-X1(i))/X1(i+1))*100;
error_X2(i+1)= abs((X2(i+1)-X2(i))/X2(i+1))*100;
error_X3(i+1)= abs((X3(i+1)-X3(i))/X3(i+1))*100;
i=i+1;
end
dimRes=size(X1);
numCol=dimRes(1,2);
disp("Tu resultado X1 es: ");
disp(X1(1,numCol));
disp("Tu resultado X2 es: ");
disp(X2(1,numCol));
disp("Tu resultado X3 es: ");
disp(X3(1,numCol));
0 Comments
Answers (1)
Torsten
on 10 Mar 2023
Edited: Torsten
on 10 Mar 2023
i=1;
X1(i)=0;
X2(i)=0;
X3(i)=0;
error_X1(i)=9999;
error_X2(i)=9999;
error_X3(i)=9999;
instead of
i=1;
X1=0;
X2=0;
X3=0;
error_X1(i)=9999;
error_X2(i)=9999;
error_X3(i)=9999;
And use
while (error_X1(i)>=error || error_X2(i)>=error || error_X3(i) >= error)
instead of
while (error_X1(i)>=error && error_X2(i)>=error && error_X3(i) >= error)
2 Comments
Torsten
on 10 Mar 2023
Works as expected:
i=1;
X1(i)=0;
X2(i)=0;
X3(i)=0;
error_X1(i)=9999;
error_X2(i)=9999;
error_X3(i)=9999;
%A=input("Escribe tu matriz A: ");
A = [2 1 0;1 2 1;0 1 2];
disp("Tu matriz A es: ");
disp(A);
%b=input("Escribe tu matriz de resultado como columna: ");
b = [0;1;2];
disp("Tu matriz de resultado es: ")
disp(b);
%error=input("Ingrese la tolerancia: ");
error = 1e-6;
while (error_X1(i)>=error ||error_X2(i)>=error || error_X3(i) >= error)
X1(i+1)=(b(1,1)-A(1,2)*X2(i)-A(1,3)*X3(i))/A(1,1);
X2(i+1)=(b(2,1)-A(2,1)*X1(i+1)-A(2,3)*X3(i))/A(2,2);%here is the error
X3(i+1)=(b(3,1)-A(3,1)*X1(i+1)-A(3,2)*X2(i+1))/A(3,3);
error_X1(i+1)= abs((X1(i+1)-X1(i))/X1(i+1))*100;
error_X2(i+1)= abs((X2(i+1)-X2(i))/X2(i+1))*100;
error_X3(i+1)= abs((X3(i+1)-X3(i))/X3(i+1))*100;
i=i+1;
end
A*[X1(end);X2(end);X3(end)]-b
dimRes=size(X1);
numCol=dimRes(1,2);
disp("Tu resultado X1 es: ");
disp(X1(1,numCol));
disp("Tu resultado X2 es: ");
disp(X2(1,numCol));
disp("Tu resultado X3 es: ");
disp(X3(1,numCol));
See Also
Categories
Find more on Embedded Coder 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!