Hi all,
I am trying to find a way of storing the values of Error (err) for each iteration of the while-loop so I can plot the error against number of iterations.
The problem with the code below, is that I only get to see the error of the last iteration only.
Your help would really be appreciated.
while (max(err) >= tol) && (it < max_it)
it = it +1;
x_old = x;
for i = 1:n_eq
x(i) = (b(i) - (A(i,:) * x)) / A(i,i) + x(i);
end
err = abs((x - x_old) ./ x); %Calculates the error
end

 채택된 답변

Jan
Jan 2017년 10월 29일
편집: Jan 2017년 10월 29일

1 개 추천

it = 0;
errOut = zeros(numel(x), max_it); % Pre-allocate
while (max(err) >= tol) && (it < max_it)
it = it + 1;
x_old = x;
for i = 1:n_eq
x(i) = (b(i) - (A(i,:) * x)) / A(i,i) + x(i);
end
err = abs((x - x_old) ./ x); %Calculates the error
errOut(:, it) = err(:);
end
errOut = errOut(:, 1:it);

댓글 수: 1

Mohammed Al Muqbel
Mohammed Al Muqbel 2017년 10월 29일
Amazing ! It worked!
Thanks very much for your time sir!

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2017년 10월 29일

댓글:

2017년 10월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by