need some help matlab code has an error

2 vues (au cours des 30 derniers jours)
edward desmond muyomba
edward desmond muyomba le 29 Avr 2020
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

Réponses (1)

Tommy
Tommy le 29 Avr 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

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by