Vectors must be same length help

21 Ansichten (letzte 30 Tage)
Hunter
Hunter am 11 Jul. 2023
Kommentiert: Menika am 11 Jul. 2023
Hello. I'm trying to make a single plot that overlays the numerical values of all the unknowns of the system of linear equations versus the iterations conducted in the Gauss-Seidel method. However, for some reason it's showing that there's an error and that vectors must be the same length, but I'm confused as to how I can solve this. Can anyone help out?
clear
clc
close all
%Definition of coefficient matrix, vector of constants, and initial solution:
A = [9 2 -3 -1 1; 0 10 -2 5 2; 0 -2 8 4 0; -1 3 0 -6 2; -1 3 2 -1 7];
b = [-34; 23; 60; -23; -39];
x = ones(1,length(b));
%Determines the number of rows (m) and columns (n) in A:
[m,n] = size(A);
%Creates an auxiliary matrix, and auxiliary vectors:
c = zeros(m,n);
d = zeros(m,1);
%Defines initial error (100%), tolerance, and initial iteration:
ea = 100;
tol = 0.01;
iter = 1;
%Prints the header of table to user:
disp('Iter x1 x2 x3 x4 Max_Error')
fprintf('%d %9.4f %9.4f %9.4f %9.4f %9.4f\n', iter-1, x(1),x(2),x(3),x(4),ea)
%Gauss-Seidel iterative method:
while ea > tol
for i = 1:n
%Computes vector of coefficients:
d(i) = b(i)/A(i,i);
%Computation of coefficient matrix "c":
for j = 1:m
if i == j
c(i,j) = 0; %Diagonal of matrix "c" is full of zeros
else
c(i,j) = A(i,j)/A(i,i); %For non-diagonal elements
end
end
%Calculates new solution:
x_new(i) = d(i) -c(i,:)*x(:);
%Calculates new error:
ea_vect(i) = abs((x_new(i) - x(i))/x_new(i)*100);
%Updates to the new solution:
x(i) = x_new(i);
end
%Determines the maximum error among the unknowns, to check convergence:
ea = max(ea_vect);
%Prints results to the user:
fprintf('%d %9.4f %9.4f %9.4f %9.4f %9.4f\n', iter, x(1),x(2),x(3),x(4),ea)
%Updates iteration:
iter = iter + 1;
end
figure;
hold on;
colors = ['r', 'g', 'b', 'm', 'c'];
labels = {'x1', 'x2', 'x3', 'x4', 'x5'};
for i = 1:size(x, 1)
plot(1:iter, x(i, :), 'Color', colors(i));
end
hold off;
grid on;
xlabel('Iterations');
ylabel('Unknowns');
legend(labels);
  1 Kommentar
Dyuman Joshi
Dyuman Joshi am 11 Jul. 2023
Bearbeitet: Dyuman Joshi am 11 Jul. 2023
The value of iter after the while loop is 14, thus 1:iter has 14 elements, whereas x has only 5 columns.
How can you plot 14 points against 5 points?
"I'm trying to make a single plot that overlays the numerical values of all the unknowns of the system of linear equations versus the iterations conducted in the Gauss-Seidel method"
Do you want to plot solution vs no of iterations taken to obtain the solution? If not, please specify / provide more information regarding this.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Menika
Menika am 11 Jul. 2023
Hi,
The error you are encountering is due to the mismatch in the dimensions of the x variable. In your code, you have initialized x as a row vector : x = ones(1,length(b));, but later in the while loop, you are updating x as a column vector x(i) = x_new(i);.
You can modify the initialization of x to be a column vector instead in line 7:
x = ones(length(b), 1);
Hope it helps!
  2 Kommentare
Hunter
Hunter am 11 Jul. 2023
So I tried that and I no longer get the error but it doesn't display the data on the plot as shown in the attachment
Menika
Menika am 11 Jul. 2023
Hi, can you specify what the desired plot will look like?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by