need some help matlab code has an error

2 次查看(过去 30 天)
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 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by