Jacobi iterative method problem
이전 댓글 표시
I've implemented the Jacobi method in matlab but when i try it , the function give me wrongs results.. I don't know what I'm wrong. I post here my code and results.
function [x,niter,resrel] = jacobiSolver(a, b, varargin)
narginchk(2, 5); % check if the function receives the right number of input parameters
nargoutchk(0,3); % check if the function receives the right number of output parameters
checkAB(a,b) % priority control
% OPTIONAL PARAMETER CONTROL
switch nargin
case 4; [TOL, NMAX] = CheckNMAX(varargin{1}, varargin{2});
case 3; [TOL, NMAX] = CheckTOL(varargin{1}, 500);
case 2; TOL = 10^-6; NMAX = 500;
otherwise
end
% codifichiamo l'algoritmo => vedere se dobbiamo sostituire con spdiags
c = b./diag(a);
B = (-1./diag(a)')' .*(a-diag(diag(a)));
x0=zeros(length(b),1);
x=c;
nit = 0;
%TOLX = max(TOL*norm(x,1),realmin);
TOLX = TOL;
while norm(x-x0,1)>=TOLX && nit<NMAX
x0 = x ;
x = B*x0 + c;
TOLX = max(realmin, TOL*norm(x0,1));
nit = nit+1
end
% IF THE NMAX VALUE IS REACHED THEN THE USER WILL BE NOTIFIED WITH A WARNING
if(nit==NMAX)
warning("Limit reached, probably not convergence... calculation stopped and resrel = "+0);
end
if nargout > 1
niter = nit;
if nargout == 3
resrel = 0;
end
end
end
results obteined with simple matrix a and b:
Warning: Limit reached, probably not convergence... calculation stopped and resrel = 0
> In jacobiSolver (line 32)
ans =
1.0e+295 *
0.3636
1.2137
0.7255
0.9811
1.0341
correct results:
ans =
20.2162
-43.2162
-56.8378
102.1081
-21.7838
also are there better ways to implement this algorithm?
thanks a lot for helping!
댓글 수: 7
Walter Roberson
2019년 4월 24일
Please post sample a and b and optional arguments for us to test with.
Agostino Dorano
2019년 4월 24일
Walter Roberson
2019년 4월 24일
checkAB() ?
Agostino Dorano
2019년 4월 24일
Walter Roberson
2019년 4월 24일
편집: Walter Roberson
2019년 4월 25일
You calculate a mostly negative B and a positive c, and you have a recursion like
f(n) = B*f(n-1) + c
and you stop when norm(f(n)-f(n-1),1) < tolerance
but f(n) - f(n-1) is going to be B*f(n-1)+c - f(n-1) = (B-1)*f(n-1) + c and the 1 norm is the sum of the absolute value of the components, so sum( abs((B(K)-1)*x0(K) + c(K)) ) < tolerance . An x0 that would be true for would be x0 = -c./(B-1) or close by.
Anyhow, you start with an x0 and head for smaller and smaller x0 looking for one that will have the B vs c balance. However, your code with its 1-norm cannot tell which side of the balance you are on, so when you overshoot the balance, you just keep going in the same direction, getting further and further from the balance.
valerio auricchio
2019년 4월 25일
Sorry Walter Roberson can you explain bettere this "An x0 that would be true for would be x0 = -c./B or close by.
Anyhow, you start with an x0 and head for smaller and smaller x0 looking for one that will have the B vs c balance. However, your code with its 1-norm cannot tell which side of the balance you are on, so when you overshoot the balance, you just keep going in the same direction, getting further and further from the balance." I don't think I caught well.
Walter Roberson
2019년 4월 25일
If x0 exactly equaled -c./(B-1) then each abs((B(K)-1)*x0(K)+c(K)) component would be 0, and the sum would be 0. But you do not need an exact 0, so the x0 values can potentially be close to -c./(B-1) and still have the 1-norm less than the tolerance.
(Note: I made a correction for the -1)
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Model Order Reduction에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!