Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.52 KB | None | 0 0
  1. %Linear equation system 'Ax=r' by Gauss elimination method.
  2. %Written by: "Sobhan Rostami"
  3. %MSc student of structure engineering of Azad university of kerman.
  4. clc
  5. clear all
  6. %=================================================================
  7. disp('Solution of N-equation "[A][X]=[r]"')
  8. A   = [2 2 -1 1; -1 1 2 3; 3 -1 4 -1; 1 4 -2 2]
  9. b   = [7 3 31 2]
  10. r   = b'
  11. n   = length(A)
  12. %-----------------------------------------------------------------
  13. %create upper triangular matrix
  14. s=0;
  15. for j=1:n
  16.     if A(j,j)==0
  17.         k=j;
  18.         for k=k+1:n
  19.             if A(k,j)==0
  20.                 continue
  21.             end
  22.             break
  23.         end
  24.         B=A(j,:); C=r(j)
  25.         A(j,:)=A(k,:); r(j)=r(k);
  26.         A(k,:)=B; r(k)=C;
  27.     end
  28.     for i=1+s:n-1
  29.         L=A(i+1,j)/A(j,j);
  30.         A(i+1,:)=A(i+1,:)-L*A(j,:);
  31.         r(i+1)=r(i+1)-L*r(j);
  32.     end
  33.     s=s+1;
  34. end
  35. %-----------------------------------------------------------------
  36. %Solution of equations
  37. x(n)=r(n)/A(n,n);
  38. for i=n-1:-1:1
  39.     sum=0;
  40.     for j=i+1:n
  41.         sum=sum+A(i,j)*x(j);
  42.     end
  43.     x(i)=(1/A(i,i))*(r(i)-sum);
  44. end
  45. %------------------------------
  46. %Checking with matlab functions
  47. p=inv(A)*r;
  48. disp(A)
  49. %------------------------------
  50. %Output
  51. disp('@----------------------------------------------------------@')
  52. disp('Output [B][x]=[b]')
  53. disp('Upper riangular Matrix [B] =');disp(A)
  54. disp('Matrix [b] =');disp(r)
  55. disp('solution of linear equations :');disp(x')
  56. disp('solve with matlab functions(for checking):');disp(p)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement