# Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 1.23 KB  |  hits: 10  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. -- SOR Algorithm implemented through Lua
2.
3. function Normalize(m)
4.         local n = 0
5.         for i,v in ipairs(m) do
6.         n = n + v^2
7.         end
8.         return math.sqrt(n)
9. end
10.
11.
12. local A = {
13. {-4,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,-11},
14. {1,-4,1,0,0,1,0,0,0,0,0,0,0,0,0,0,-3},
15. {0,1,-4,1,0,0,1,0,0,0,0,0,0,0,0,0,-3},
16. {0,0,1,-4,0,0,0,1,0,0,0,0,0,0,0,0,-11},
17. {1,0,0,0,-4,1,0,0,1,0,0,0,0,0,0,0,-8},
18. {0,1,0,0,1,-4,1,0,0,1,0,0,0,0,0,0,0},
19. {0,0,1,0,0,1,-4,1,0,0,1,0,0,0,0,0,0},
20. {0,0,0,1,0,0,1,-4,0,0,0,1,0,0,0,0,-8},
21. {0,0,0,0,1,0,0,0,-4,1,0,0,1,0,0,0,-8},
22. {0,0,0,0,0,1,0,0,1,-4,1,0,0,1,0,0,0},
23. {0,0,0,0,0,0,1,0,0,1,-4,1,0,0,1,0,0},
24. {0,0,0,0,0,0,0,1,0,0,1,-4,0,0,0,1,-8},
25. {0,0,0,0,0,0,0,0,1,0,0,0,-4,1,0,0,-10},
26. {0,0,0,0,0,0,0,0,0,1,0,0,1,-4,1,0,-2},
27. {0,0,0,0,0,0,0,0,0,0,1,0,0,1,-4,1,-2},
28. {0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,-4,-10},
29. }
30.
31.
32. local MAXITER = 10000
33. local N=table.getn(A)
34. local t = MAXITER
35. local w = 1.86
36. local ACCURACY = 1E-6
37. local x = {}
38.         for k = 1,N do x[k]=0 end
39.
40. repeat
41. t=t-1
42.
43. local q = Normalize(x)
44.         for i=1,N do
45.         local sum=A[i][N+1]
46.                 for j=1,N do
47.                         if (i~=j) then
48.                         sum = sum-A[i][j]*x[j]
49.                         end
50.                 end
51.         x[i]=(1-w)*x[i]+w*sum/A[i][i]
52.         end
53.         local p = Normalize(x)
54.         local d = math.abs(p-q)
55. until (d<ACCURACY) or (t==0)
56.
57. print( 'Num of iter:',MAXITER-t)
58. for i=1,N do
59. print('x['..i..']='..x[i])
60. end