Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author: Felipe Rodrigues
- // Course: Algebra Linear Computacional
- // Algorithm: Upper Triangular
- function [r] = back_substitution (A)
- [row, col] = size(A)
- r(1:row) = 0
- if ( (row ~= 1) & (col ~= 2) ) then
- for i = row:-1:1
- sum_ = 0
- for j=col-1:-1:1
- sum_=sum_+A(i,j)*r(j,1)
- end
- r(i,1)=(A(i, row+1)-sum_)/A(i,i)
- end
- end
- endfunction
- function [r] = gaussian_elimination(A)
- //Get a tuple representing matrix dimension
- [row, col] = size(A)
- if ( (row ~= 1) & (col ~= 2) ) then
- for k = 1:row
- disp(A)
- if A(k, k) ~= 0 then
- for i = k+1:row
- m = real(A(i, k)/A(k, k))
- for j = 1:col
- a = A(k, j)
- new_element = A(i, j) - m*a
- printf("Novo Elemento A(%d, %d) = %f - %f*%f = %f\n", i, j, A(i,j), m, a, new_element)
- A(i,j) = 0
- A(i,j) = new_element
- disp(A(i,j))
- end
- end
- else
- A = "Sistema Inconsistente"
- break
- end
- end
- else
- A = A(1,1)
- end
- r = A
- endfunction
- function main()
- opt = input("Digite (1)-A1, (2)-A2 ou (3)-Nova Matriz:")
- A = null
- if (opt == 1) then
- A = [-1,2,0.3,-119 ; 3,-5,7,120 ; -0.1,13,0,-139] //Expandida A1 | b
- else
- if (opt == 2) then
- A = [3.03,-12.1,14,-119 ; -3.03,12.1,-7,120 ; 6.11,-14.2,21,-139] //Expandida A2 | b
- else
- opt_2 = input("Digite (1) para n=5 ou (2) para n=15:")
- n = null
- if (opt_2 == 1) then
- n = 5
- else
- n = 15
- end
- A = zeros(n,n+1)
- for i=1:n
- for j=1:n
- A(i,j) = 1/(i+j-1)
- end
- end
- A(1:n,n+1) = 1
- disp(A)
- end
- end
- disp(A)
- [row, col] = size(A)
- b = A(1:row,col)
- [row, col] = size(A)
- if ( (row ~= 1) & (col ~= 1) ) then
- ut = gaussian_elimination(A)
- solution = back_substitution (ut)
- disp(ut)
- //disp(solution)
- return
- else
- disp(matrixx)
- end
- return
- disp(f)
- endfunction
- opt = 1
- while (opt == 1) then
- main()
- in = input("Digite (1) para continuar ou qualquer numero para encerrar:")
- if in ~= 1 then
- opt = 0
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement