Error in SOR function.
이전 댓글 표시
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?
function[x]=SOR(A,b,x0,omega,tol,maxn)
k=1;
x=x0;
w=omega;
%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.
D=diag(diag(A));
L= tril(-A,-1);
U=triu(-A,1);
Tw=inv(D-w*L)*((1-w)*D+w*U);
cw=w*inv(D-w*L)*b;
while k<=maxn
x(:,k+1)=Tw*x(:,k)+cw;
if norm(x(:,k+1)-x(:,k))< tol
disp('x= ');
disp(x(:,k+1));
break
end
k=k+1;
end
end
% test program
clear
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);
xe=[0.942803389915179
2.00072895774848
-1.97233753473860];
error_1 = norm(x-xe,inf)
댓글 수: 1
John D'Errico
2017년 4월 19일
Why does it seem absurd that you are using SOR to solve a problem, when in your SOR code, you use the INV function for a major part of the computation?
Using a matrix inverse INSIDE a SOR tool that is itself a way to solve a linear system of equations is just silly. It is even more silly, since you can do that computation with a simple substitution loop.
Finally, it seems even more absurd that you are using inv TWICE, inverting the same lower triangular matrix.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Multicore Processor Targets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!