need some help matlab code has an error

조회 수: 3 (최근 30일)
edward desmond muyomba
edward desmond muyomba 2020년 4월 29일
댓글: edward desmond muyomba 2020년 4월 29일
A = [6 2 1 1;2 7 1 2;3 2 8 1;1 2 6 9];
B = [6;4;5;3];
maxitr = 100;
tolx = 1.0e-5;
n = length(A);
x = zeros(n,1);
err = zeros(n,1);
for itr = 1:maxitr
for i = 1:n
xold = x(i);
x (i) = (b(i) - (A(i,1:i-1) *x(1:i-1) +A(i,i+1:n)*x(i+1:n))) /A(i,i);
err(i) = abs(x(i) - xold);
end
if (max(err)<tolx)
fprint('\n Error @ last iteration = %f',max(err));
fprint('\n\n solution found @ iteration no. =%d ',itr);
fprint('n\n solution Vector : \n');
disp(x);
break
end
end

답변 (1개)

Tommy
Tommy 2020년 4월 29일
(1) Use B where you have b.
(2) Change fprint to fprintf.
A = [6 2 1 1;2 7 1 2;3 2 8 1;1 2 6 9];
B = [6;4;5;3];
maxitr = 100;
tolx = 1.0e-5;
n = length(A);
x = zeros(n,1);
err = zeros(n,1);
for itr = 1:maxitr
for i = 1:n
xold = x(i);
x (i) = (B(i) - (A(i,1:i-1) *x(1:i-1) +A(i,i+1:n)*x(i+1:n))) /A(i,i);
err(i) = abs(x(i) - xold);
end
if (max(err)<tolx)
fprintf('\n Error @ last iteration = %f',max(err));
fprintf('\n\n solution found @ iteration no. =%d ',itr);
fprintf('n\n solution Vector : \n');
disp(x);
break
end
end

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by