Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. // Author: Felipe Rodrigues
  2. // Course: Algebra Linear Computacional
  3. // Algorithm: Upper Triangular
  4.  
  5.  
  6. function [r] = back_substitution (A)
  7. [row, col] = size(A)
  8. r(1:row) = 0
  9. if ( (row ~= 1) & (col ~= 2) ) then
  10. for i = row:-1:1
  11. sum_ = 0
  12. for j=col-1:-1:1
  13. sum_=sum_+A(i,j)*r(j,1)
  14. end
  15. r(i,1)=(A(i, row+1)-sum_)/A(i,i)
  16. end
  17.  
  18. end
  19.  
  20.  
  21. endfunction
  22.  
  23.  
  24. function [r] = gaussian_elimination(A)
  25. //Get a tuple representing matrix dimension
  26. [row, col] = size(A)
  27.  
  28. if ( (row ~= 1) & (col ~= 2) ) then
  29.  
  30. for k = 1:row
  31. disp(A)
  32. if A(k, k) ~= 0 then
  33.  
  34. for i = k+1:row
  35. m = real(A(i, k)/A(k, k))
  36.  
  37. for j = 1:col
  38. a = A(k, j)
  39.  
  40. new_element = A(i, j) - m*a
  41.  
  42. printf("Novo Elemento A(%d, %d) = %f - %f*%f = %f\n", i, j, A(i,j), m, a, new_element)
  43.  
  44. A(i,j) = 0
  45. A(i,j) = new_element
  46.  
  47. disp(A(i,j))
  48.  
  49. end
  50. end
  51. else
  52. A = "Sistema Inconsistente"
  53. break
  54. end
  55. end
  56. else
  57. A = A(1,1)
  58. end
  59. r = A
  60.  
  61.  
  62. endfunction
  63.  
  64.  
  65. function main()
  66.  
  67. opt = input("Digite (1)-A1, (2)-A2 ou (3)-Nova Matriz:")
  68. A = null
  69.  
  70. if (opt == 1) then
  71. A = [-1,2,0.3,-119 ; 3,-5,7,120 ; -0.1,13,0,-139] //Expandida A1 | b
  72. else
  73. if (opt == 2) then
  74. A = [3.03,-12.1,14,-119 ; -3.03,12.1,-7,120 ; 6.11,-14.2,21,-139] //Expandida A2 | b
  75. else
  76. opt_2 = input("Digite (1) para n=5 ou (2) para n=15:")
  77. n = null
  78.  
  79. if (opt_2 == 1) then
  80. n = 5
  81. else
  82. n = 15
  83. end
  84.  
  85. A = zeros(n,n+1)
  86.  
  87. for i=1:n
  88. for j=1:n
  89. A(i,j) = 1/(i+j-1)
  90. end
  91. end
  92. A(1:n,n+1) = 1
  93. disp(A)
  94.  
  95. end
  96.  
  97. end
  98.  
  99.  
  100. disp(A)
  101.  
  102. [row, col] = size(A)
  103. b = A(1:row,col)
  104.  
  105.  
  106. [row, col] = size(A)
  107.  
  108.  
  109. if ( (row ~= 1) & (col ~= 1) ) then
  110. ut = gaussian_elimination(A)
  111. solution = back_substitution (ut)
  112. disp(ut)
  113. //disp(solution)
  114.  
  115. return
  116. else
  117. disp(matrixx)
  118. end
  119.  
  120. return
  121.  
  122. disp(f)
  123.  
  124.  
  125. endfunction
  126.  
  127. opt = 1
  128. while (opt == 1) then
  129. main()
  130. in = input("Digite (1) para continuar ou qualquer numero para encerrar:")
  131. if in ~= 1 then
  132. opt = 0
  133. end
  134. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement