-
-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy paththomas.jl
46 lines (35 loc) · 944 Bytes
/
thomas.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function thomas(a::Vector{Float64}, b::Vector{Float64}, c::Vector{Float64},
d::Vector{Float64}, n::Int64)
x = copy(d)
c_prime = copy(c)
# Setting initial elements
c_prime[1] /= b[1]
x[1] /= b[1]
for i = 2:n
# Scale factor is for c_prime and x
scale = 1.0 / (b[i] - c_prime[i-1]*a[i])
c_prime[i] *= scale
x[i] = (x[i] - a[i] * x[i-1]) * scale
end
# Back-substitution
for i = n-1:-1:1
x[i] -= (c_prime[i] * x[i+1])
end
return x
end
function main()
a = [0.0, 2.0, 3.0]
b = [1.0, 3.0, 6.0]
c = [4.0, 5.0, 0.0]
d = [7.0, 5.0, 3.0]
println(
"""The system
$(join((b[1], c[1], "", "|", d[1]), "\t"))
$(join((a[2], b[2], c[2], "|", d[2]), "\t"))
$(join(("", a[3], b[3], "|", d[3]), "\t"))
Has the solution:"""
)
soln = thomas(a, b, c, d, 3)
println(soln)
end
main()