Advertisement
Tr0n

Trabalho CFD

Mar 21st, 2017
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 3.01 KB | None | 0 0
  1. ##########################################################################
  2. # Trabalho 1 - TRANSMISSÃO DE CALOR E MECÂNICA DOS FLUIDOS COMPUTACIONAL #
  3. #                           Equipe: Levi Pio                             #
  4. ##########################################################################
  5.  
  6. function sist_ger(n)
  7.         i = 1
  8.         j = 1
  9.         somatorio = 0.0
  10.  
  11.         sist = rand(n,n)
  12.         for i in 1:n
  13.             for j in 1:n
  14.                 somatorio = somatorio + abs(sist[i,j])
  15.             end
  16.  
  17.             if sist[i,i] < (somatorio - abs(sist[i,i]))
  18.                 sist[i,i] = somatorio
  19.                 somatorio = 0.0
  20.             end
  21.         end
  22.  
  23.         return sist
  24. end
  25.  
  26. function term_ind(n)
  27.         b = rand(n,1)
  28.         return b
  29. end
  30.  
  31. n = 10
  32.  
  33. A = sist_ger(n)
  34. J = copy(A)
  35. mat = copy(A)
  36. b = term_ind(n)
  37. c = copy(b)
  38. ind = copy(b)
  39.  
  40. function main()
  41.  
  42.     x = zeros(n)
  43.     v = zeros(n)
  44.     y = zeros(n)
  45.     k = zeros(n)
  46.     e = 1e-4
  47.    
  48.     res = inv(A)*b
  49.  
  50.     function gauss_seidel(n)
  51.  
  52.         i = 1
  53.         j = 1
  54.  
  55.         for i in 1:n
  56.             r = 1/A[i,i]
  57.             for j in 1:n
  58.                 if i!=j
  59.                     A[i,j] = A[i,j]*r
  60.                 end
  61.             end
  62.             b[i] = b[i]*r
  63.             x[i] = b[i]
  64.         end
  65.  
  66.         i = 1
  67.         j = 1
  68.  
  69.         while true
  70.            
  71.             for i in 1:n
  72.                 soma = 0.0
  73.                 for j in 1:n
  74.                     if i!=j
  75.                         soma = soma + A[i,j]*x[j]
  76.                     end
  77.                 end
  78.                 v[i] = x[i]
  79.                 x[i] = b[i] - soma
  80.             end
  81.            
  82.             norma = maximum(abs(x - v))/maximum(abs(x))
  83.  
  84.             if norma <= e
  85.                 break
  86.             end
  87.         end
  88.  
  89.         return x
  90.  
  91.     end
  92.  
  93.     function gauss_jacobi(n)
  94.         i = 1
  95.         j = 1
  96.  
  97.         for i in 1:n
  98.             t = 1/J[i,i]
  99.             for j in 1:n
  100.                 if i!=j
  101.                     J[i,j] = J[i,j]*t
  102.                 end
  103.             end
  104.             c[i] = c[i]*t
  105.             y[i] = c[i]
  106.         end
  107.  
  108.         i = 1
  109.         j = 1
  110.  
  111.         while true
  112.            
  113.             for i in 1:n
  114.                 soma = 0.0
  115.                 for j in 1:n
  116.                     if i!=j
  117.                         soma = soma + J[i,j]*y[j]
  118.                     end
  119.                 end
  120.                 k[i] = c[i] - soma
  121.             end
  122.            
  123.             norma = maximum(abs(y - k))/maximum(abs(y))
  124.  
  125.             for i in 1:n
  126.                 y[i] = k[i]
  127.             end
  128.  
  129.             if norma <= e
  130.                 break
  131.             end
  132.         end
  133.  
  134.         return y
  135.  
  136.     end
  137.  
  138.     col1 = res
  139.     col2 = gauss_seidel(n)
  140.     col3 = gauss_jacobi(n)
  141.    
  142.     println("                           (A)                                                                                 (b)         x(dir.)     x(G.S.)     x(G.J.))")  
  143.     return [mat ind col1 col2 col3]
  144.  
  145. end
  146.  
  147. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement