Advertisement
Kimossab

Jacobi Method

Mar 12th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.80 KB | None | 0 0
  1. %Metodo de Jacobi
  2.  
  3. clear;
  4. clc;
  5.  
  6. format long
  7.  
  8. disp('Método de Jacobi')
  9. disp('')
  10.  
  11. A=input('Introduza a matriz A= ');
  12.  
  13. [m,n]=size(A);
  14.  
  15. b=input('Introduza o vector b(vector linha 1*n)=');
  16. x0=input('Introduza a aproximação inicial x0 (vector linha 1*n)= ');
  17. erro=input('Qual o erro max pretendido?  ');
  18. iter=input('Qual o numero max de iteraçoes? ');
  19. disp('')
  20. it=1;
  21. e=erro+1;
  22. x=x0;
  23. while (e>erro && it<=iter)
  24.  
  25.     for i=1:m;
  26.         soma=0;
  27.         for j=1:n
  28.             if j~=i
  29.                 soma=soma+A(i,j)*x(j);
  30.             end
  31.         end
  32.         x(i)=(1/A(i,i))*(b(i)-soma);
  33.     end
  34.    
  35.     e=norm(x-x0, inf);
  36.    
  37.     disp([num2str(it),'ª iteração: Solução aproximada = ', mat2str(x,5), 'com estimativa de erro = ', num2str(e)])
  38.    
  39.     it=it+1;
  40.     x0=x;
  41.    
  42. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement