need some help matlab code has an error
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
edward desmond muyomba
el 29 de Abr. de 2020
Comentada: edward desmond muyomba
el 29 de Abr. de 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
0 comentarios
Respuestas (1)
Tommy
el 29 de Abr. de 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
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!