Advertisement
UF6

New EAS 501 Homework I

UF6
Nov 15th, 2020
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.42 KB | None | 0 0
  1. %%%This is for part a for problem 1a.
  2. A=zeros(8,8);%%%For the column and row size of matrix A.
  3. %Finding A
  4. for j=1:8
  5. for k=1:8
  6. if j==k
  7. A(j,k)=-2;
  8. end
  9. if abs(j-k)==1
  10. A(j,k)=1;
  11. end
  12. end
  13. end
  14. %The decomposition of the matrix into L and U.
  15.   L=zeros(8,8);%%%For the column and row size of matrix L.
  16.   U=zeros(8,8);%%%For the column and row size of matrix U.
  17.   for i=1:8
  18.   % Finding L
  19.   for k=1:i-1
  20.   L(i,k)=A(i,k);
  21.   for j=1:k-1
  22.   L(i,k)= L(i,k)-L(i,j)*U(j,k);
  23.   end
  24.   L(i,k) = L(i,k)/U(k,k);
  25.   end
  26.   % Finding U
  27.   for k=i:8
  28.   U(i,k) = A(i,k);
  29.   for j=1:i-1
  30.   U(i,k)= U(i,k)-L(i,j)*U(j,k);
  31.   end
  32.   end
  33.   end
  34.   for i=1:8
  35.   L(i,i)=1;
  36.   end
  37.  % Showing U and L
  38. disp("U:")
  39. disp(U)
  40. disp("L:")
  41. disp(L)
  42.  %Used to show by in-built Matlab tools that the code is correct.
  43. %[L,U]=lu(A)
  44.  
  45. %Showing that L*U=A, thus it is correct.
  46. disp("A:")
  47. disp(L*U)
  48.  
  49. %%%This is for part b for problem 1b.
  50. temp = [1 1 1 1 1 1 1 1] %We use this as the 1x8.
  51. temp = temp' %Rotating the resulted matrix.
  52. gauss = [A temp] %Creating the new 8x9 matrix for A and temp.
  53. n=8%Is the value for n.
  54. disp(gauss)
  55. for j = 1:(n-1)
  56. for i= (j+1) : n
  57. mult = gauss(i,j)/gauss(j,j) ;
  58. for k= j:n+1
  59. gauss(i,k) = gauss(i,k) - mult*gauss(j,k) ;
  60. %Guassian Elimination
  61. end
  62. end
  63. end
  64. for p = n:-1:1
  65. for r = p+1:n
  66. x(p) = gauss(p,r)/gauss(p,r-1)
  67. end
  68. end
  69. disp("After the Gaussian Elimination the results now equal [1,1,1,1,1,1,1,1] for 1x8:")
  70. disp(gauss)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement