1. 程式人生 > >Guass-seidel 迭代法 matlab實現

Guass-seidel 迭代法 matlab實現

clc
clear
n = input('請輸入矩陣階數:\n');
for i = 1:n
    fprintf('請輸入矩陣第%d行\n',i);
    A(i,:) = input('');
end
 A
B(:,1) = input('請輸入B向量:\n');
B
for i = 1:n
    x(i) = 0;
end
for i =1:4
    for j = 1:n
        sum = 0;
        for k = 1:n
            if j ~= k
                sum = sum-A(j,k)*x(k);
            end
        end
        sum=sum + B(j);
        x(j)=sum/A(j,j);
    end
   
    fprintf('第%d次迭代結果:\n',i);
      x
end