这个函数应该使用gauss-seidel方法返回线性方程组的答案,并返回迭代过程中的差值图。到目前为止,我已经能够得到返回正确答案的函数,而图表是空白的。任何提示都将不胜感激。
function[x] = Gauss_Seidel(A,b)
[m, n] = size(A);
[g, v] = size(b);
x= zeros(m, 1);
error = zeros(m, 1);
for iterations = 1:300
for k = 1:m
value = 0;
for j = 1:m
if j~=k
value = value + A(k,j)*x(j);
end
end
x(k) = (b(k)-value)/A(k,k);
end
end
tdiff = 0;
for j = 1:m
LHS=0;
for k=1:m
LHS=LHS+A(j,k)*x(k);
end
difference(j) = abs(b(j)-LHS);
tdiff = tdiff+difference(j);
end
if tdiff<=error
iterations
plot(tdiff,j)
return
end
end
调用plot时,tdiff和j似乎不是数组。
环路之前
tdiff = zeros(1,m+1);
steps = zeros(1,m+1); % you need some increment array for your plot
在环
tdiff(j+1) = tdiff(j)+difference(j);
steps(j+1)
只需注意步骤和tdiff从0 开始