Error in SOR function.
4 views (last 30 days)
I'm trying to create a function that performs the SOR method. Here is the code I have and the test program. When I ran the test program. There is an error " Error using - Matrix dimensions must agree. " What does it mean?
%x0 is the initial guess.
%omega is the relaxation parameter, typically larger than 1.
%tol is the tolerance for the stopping. Use the relative error.
%maxn is the maximum number of iterations allowed.
if norm(x(:,k+1)-x(:,k))< tol
% test program
A = [3 -1 1; -1 3 -1; 1 -1 3];
b = [-1; 7; -7];
omega = 1.25;
x0 = [0; 0; 0];
tol = 1e-5;
maxn = 4;
x = SOR(A,b,x0,omega,tol,maxn);
error_1 = norm(x-xe,inf)
Sam McDonald on 19 Apr 2017
I am able to execute the code you provided with no errors.
You may have another function called "SOR" on your MATLAB path that could be shadowing this "SOR" function you are intending to use. Check this by executing:
which -all SOR
I only have one listed, which is the one you provided here. MATLAB will use the function that is highest on the MATLAB path.
In general, the error "Matrix dimensions must agree" is thrown if an operation requires the dimensions of two variables to agree. A simple example is:
Error using * Inner matrix dimensions must agree.
The exact reason for the error depends on what operation you are performing. Look at what line of code the error is referencing and debug your code from there to understand what is happening.