MATLAB Answers

Jacobi method not working

조회 수: 2(최근 30일)
Joe Smith
Joe Smith 9 Jul 2016
Not sure what's wrong with my code. It does the first iteration correctly, then goes off in the wrong direction.
if true
% function x = Playing(A,b)
n = length(b);
x = zeros(1,n)';
y = x;
for k = 1:20
for i = 1:n
for j = 1:i-1,i+1:n
y(i) = y(i) + A(i,j)*x(j)
end
y(i) = (b(i) - y(i))/A(i,i)
end
end
end
end

  댓글 수: 0

댓글을 달려면 로그인하십시오.

답변(1개)

Azzi Abdelmalek
Azzi Abdelmalek 9 Jul 2016
What is this: for j = 1:i-1,i+1:n ?
Maybe you mean for j = [1:i-1,i+1:n]

  댓글 수: 2

Joe Smith
Joe Smith 9 Jul 2016
I've tried both, it didn't make a difference.
Azzi Abdelmalek
Azzi Abdelmalek 9 Jul 2016
for ii= 1:n
s=0
for jj = setdiff(1:n,ii)
s = s + A(ii,jj)*y(jj);
end
y(ii) = (b(ii) - s)/A(ii,ii);
end
and don't forget the initial guess y=ones(n,1) for example

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by