Assistance with N-dimensional Jacobi?
1 次查看(过去 30 天)
显示 更早的评论
Greetings all,
For my N-dimensional Jacobi code, I have the following:
function x=Jacobi1(A,b,phi,tol, NMax)
%This is the function for the Jacobi approximation to
%the solution of Ax = B
%For inputs you can choose the following:
% A - is an N x N nonsingular matrix
% B - is an N x 1 matrix
% phi - is an N x 1 matrix; the initial guess
% tol - is the tolerance for phi
% NMax - is the maximum number of iterations
sum1 = 0; %zeros(NMax,NMax);
n=length(phi);
y=zeros(n,NMax);
for i=1:length(b)
for j=1:length(b)
if j == 1 && j~=i
sum1 = sum1 + (-A(i,j)/A(i,i)*phi(j+1))-(b(i)-A(i,i));
phi(i)=sum1;
end
end
y(i,j)=phi(i)
end
Now, my A is (for this example):
A =
-3.8889 1.0000 0 1.0000 0 0 0 0 0
1.0000 -3.8889 1.0000 0 1.0000 0 0 0 0
0 1.0000 -3.8889 0 0 1.0000 0 0 0
1.0000 0 0 -3.8889 1.0000 0 1.0000 0 0
0 1.0000 0 1.0000 -3.8889 1.0000 0 1.0000 0
0 0 1.0000 0 1.0000 -3.8889 0 0 1.0000
0 0 0 1.0000 0 0 -3.8889 1.0000 0
0 0 0 0 1.0000 0 1.0000 -3.8889 1.0000
0 0 0 0 0 1.0000 0 1.0000 -3.8889
and my b is: b =
-18
-9
-18
-9
0
-9
-18
-9
-18
and my phi is just phi = ones(N+2,N+2);, which serves as an initial guess.
Now, when I use the function, this is my output:
>> Jacobi1(A,b,phi,10^-6,20)
y =
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
y =
Columns 1 through 12
0 0 0 0 0 0 0 0 1.0000 0 0 0
0 0 0 0 0 0 0 0 5.3683 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
Columns 13 through 20
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
y =
Columns 1 through 12
0 0 0 0 0 0 0 0 1.0000 0 0 0
0 0 0 0 0 0 0 0 5.3683 0 0 0
0 0 0 0 0 0 0 0 19.4794 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
Columns 13 through 20
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
y =
Columns 1 through 12
0 0 0 0 0 0 0 0 1.0000 0 0 0
0 0 0 0 0 0 0 0 5.3683 0 0 0
0 0 0 0 0 0 0 0 19.4794 0 0 0
0 0 0 0 0 0 0 0 25.9709 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
Columns 13 through 20
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
y =
Columns 1 through 12
0 0 0 0 0 0 0 0 1.0000 0 0 0
0 0 0 0 0 0 0 0 5.3683 0 0 0
0 0 0 0 0 0 0 0 19.4794 0 0 0
0 0 0 0 0 0 0 0 25.9709 0 0 0
0 0 0 0 0 0 0 0 22.0820 0 0 0
Columns 13 through 20
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
y =
Columns 1 through 12
0 0 0 0 0 0 0 0 1.0000 0 0 0
0 0 0 0 0 0 0 0 5.3683 0 0 0
0 0 0 0 0 0 0 0 19.4794 0 0 0
0 0 0 0 0 0 0 0 25.9709 0 0 0
0 0 0 0 0 0 0 0 22.0820 0 0 0
0 0 0 0 0 0 0 0 27.1931 0 0 0
Columns 13 through 20
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
y =
Columns 1 through 12
0 0 0 0 0 0 0 0 1.0000 0 0 0
0 0 0 0 0 0 0 0 5.3683 0 0 0
0 0 0 0 0 0 0 0 19.4794 0 0 0
0 0 0 0 0 0 0 0 25.9709 0 0 0
0 0 0 0 0 0 0 0 22.0820 0 0 0
0 0 0 0 0 0 0 0 27.1931 0 0 0
0 0 0 0 0 0 0 0 41.3042 0 0 0
Columns 13 through 20
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
y =
Columns 1 through 12
0 0 0 0 0 0 0 0 1.0000 0 0 0
0 0 0 0 0 0 0 0 5.3683 0 0 0
0 0 0 0 0 0 0 0 19.4794 0 0 0
0 0 0 0 0 0 0 0 25.9709 0 0 0
0 0 0 0 0 0 0 0 22.0820 0 0 0
0 0 0 0 0 0 0 0 27.1931 0 0 0
0 0 0 0 0 0 0 0 41.3042 0 0 0
0 0 0 0 0 0 0 0 46.4153 0 0 0
Columns 13 through 20
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
y =
Columns 1 through 12
0 0 0 0 0 0 0 0 1.0000 0 0 0
0 0 0 0 0 0 0 0 5.3683 0 0 0
0 0 0 0 0 0 0 0 19.4794 0 0 0
0 0 0 0 0 0 0 0 25.9709 0 0 0
0 0 0 0 0 0 0 0 22.0820 0 0 0
0 0 0 0 0 0 0 0 27.1931 0 0 0
0 0 0 0 0 0 0 0 41.3042 0 0 0
0 0 0 0 0 0 0 0 46.4153 0 0 0
0 0 0 0 0 0 0 0 60.5264 0 0 0
Columns 13 through 20
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
And obviously this isn't correct. The values are there, but it's tricky making it N-dimensional.
Any suggestions?
Thanks!
0 个评论
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!