need some help matlab code has an error

2 views (last 30 days)
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

Answers (1)

Tommy
Tommy on 29 Apr 2020
(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

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!