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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
| program Curs
implicit none
interface
subroutine threediagmatrix(C,Ans,n,Out)
integer n
real C(n),Ans(n),Out(n)
end subroutine
subroutine threediagsolve(A,B,C,Ans,n,Out)
integer n
real A(2:n),B(1:n-1),C(n),Ans(n),Out(n)
end subroutine threediagsolve
subroutine oddevensolve(C,F,size,vsize,n,Out)
integer size,vsize,n
real F(0:vsize,size),C(size,size),Out(0:vsize,size)
end subroutine oddevensolve
subroutine findC(C,l,k,sz,Out)
integer l,k,sz
real C(sz,sz),Out(sz,sz)
end subroutine findC
function findAlpha(l,k)
integer l,k
end function
real function k(x)
real x
end function
real function g1(x)
real x
end function
real function g2(x)
real x
end function
real function g3(x)
real x
end function
real function g4(x)
real x
end function
real function u(x,y)
real x,y
end function
real function func(x,y)
real x,y
end function
end interface
! Variables
integer, parameter :: length = 4
! number of y-values integer, parameter :: vlength = 4
! number of y-values real maxmodmaxmod
integer, parameter :: vlength = 4
real maxmod
integer i, j
real F(0:vlength, 0:length - 1), C(0:length - 1, 0:length - 1), Rr(0:vlength, 0:length - 1)
real hx,hy,x0,y0,xn,yn,hi
!initialization
x0 = 0
y0 = 0
xn = 4
yn = 4
hx = (xn - x0) / length
hy = (yn - y0) / vlength
C = 0
hi =1
F = 0
do i = 1,length - 2
C(i-1,i) = -(hy**2)/(hx**2)*k(x0 + (i - 0.5)*hx)
C(i,i) = (hy**2)/(hx**2)*k(x0 + (i - 0.5)*hx) + (hy**2)/(hx**2)*k(x0 + (i + 0.5)*hx) + 2
C(i+1,i) = -(hy**2)/(hx**2)*k(x0 + (i + 0.5)*hx)
enddo
C(length - 2,length - 1) = -(hy**2)/(hx**2)*k(xn - 1.5*hx)
C(length - 1, length - 1) = (hy**2)/(hx**2)*k(xn - 1.5*hx) + (hy**2)/(hx**2)*k(xn - 0.5*hx) + 2
do j = 1, vlength - 1
F(j,length - 1) = (hy**2) * func(xn - hx,y0 + j * hy) + (hy**2)/(hx**2) * g2(y0+j * hy) * k(xn - 0.5*hx)
enddo
C(0,0) = 2*((hy**2)/(hx**2) * k(x0 + 0.5*hx) + hi*hy/hx + 1)
C(1,0) = - 2*(hy**2)/(hx**2)*k(x0 + 0.5*hx)
do j = 1, vlength - 1
F(j,0) = (hy**2) * func(x0,y0 + j * hy) + 2*(hy**2)/(hx**2) * g1(y0+j * hy)
enddo
do j = 1,vlength - 1
do i = 1,length - 2
F(j,i) = (hy**2) * func(x0 + i*hx,y0 + j*hy)
enddo
enddo
print *, " "
do i = 0, length - 1
F(0,i) = g3(x0 + i*hx)
F(vlength,i) = g4(x0 + i*hx)
enddo
call oddevensolve(C,F,length,vlength,int(log(float(vlength))/log(2.0)),Rr) ! call of solving procedure
! finding error value maxmod = 0
do j = 1, vlength-1
do i = 1,length-2
!print *, "i = ",i," j = ",j
!print *, Rr(j,i) ," ----- ", u(x0 + i*hx,y0 + j*hy)
if (abs(Rr(j,i) - u(x0 + i*hx,y0 + j*hy)) > maxmod) then
maxmod = abs(Rr(j,i) - u(x0 + i*hx,y0 + j*hy))
endif
enddo
enddo
print *,"Max is ",maxmod
end program Curs
subroutine findC(C,l,k,sz,Out)
integer l,k,sz
real C(sz,sz),Out(sz,sz)
real Pi
real E(sz,sz)
integer i
Pi = 4*atan(1.0)
E = 0
do i = 1,sz
E(i,i) = 1
enddo
Out = C - E * 2*cos(((2*float(l) - 1.0) * Pi)/2**(float(k)+1.0))
end subroutine
function findAlpha(l,k)
real Pi
integer l,k
pi = 4*atan(1.0)
findAlpha = (((-1)**(float(l)+1.0))/(2**float(k)))*sin(((2*(l- 1.0) * Pi))/(2**(float(k)+1.0)))
! findAlpha = (((-1)**(float(l)+1.0))/(2**float(k)))*sin(((2*float(l- 1.0) * Pi)/(2**(float(k)+1.0))))
end function
subroutine oddevensolve(C,F,size,vsize,n,Out)
integer n
integer size,vsize
real F(0:vsize,size),P(vsize-1,size),C(0:size-1,0:size- 1),Fi(size),Psi(size),V(vsize,size),Y(0:vsize,size)
real Out(0:vsize,size)
real A(2:size),B(1:size-1),Cc(size),Tmp(size,size)
integer k,j,l
integer i
do i = 1,vsize-1
P(i,:) = F(i,:)
enddo
! straight walk
do k = 1, n - 1
do j = 2**k,vsize - 2**k,2**k
print *, j
Fi = P(j - 2**(k-1),:) + P(j + 2**(k-1),:)
do l = 1,2**(k-1)
call findC(C,l,k-1,size,Tmp)
call threediagmatrix(Tmp,Fi * findAlpha(l,k-1),size,V(l,:))
P(j,:) = P(j,:) + V(l,:)
enddo
P(j,:) = 0.5 * P(j,:)
enddo
enddo
! reverse walk Y(0,:) = F(0,:)
Y(vsize,:) = F(vsize,:)
do k = n,1,-1
do j = 2**(k-1),vsize - 2**(k-1),2**k
print *, j
Fi = Y(j - 2**(k-1),:) + Y(j + 2**(k-1),:)
Psi = P(j,:)
Y(j,:) = 0
do l = 1,2**(k-1)
call findC(C,l,k-1,size,Tmp)
call threediagmatrix(Tmp,Psi + Fi * findAlpha(l,k-1),size,V(l,:))
Y(j,:) = Y(j,:) + V(l,:)
enddo
enddo
enddo
Out = Y
end subroutine oddevensolve
subroutine threediagmatrix(C,Ans,n,Out)
integer n
real C(n,n),Ans(n),Out(n)
real A(2:n),B(1:n-1),CC(n)
integer i,j
do i = 1,n
do j = 1,n
if (i == j) then
Cc(j) = C(i,j)
else if (i - j == 1) then
B(j) = C(i,j)
else if(j - i == 1) then
A(j) = C(i,j)
endif
enddo
enddo
call threediagsolve(A,B,Cc,Ans,N,Out)
return
end subroutine
subroutine threediagsolve(A,B,C,Ans,n,Out)
!input parameters
integer n
real A(2:n),B(1:n-1),C(n),Ans(n),Out(n)
!work parameters
real alpha(2:n),beta(2:n),X(n)
!body of method alpha(2) = -B(1)/C(1)
beta(2) = ans(1)/C(1)
do i = 2,n-1
alpha(i+1) = (-B(i))/(A(i)*alpha(i) + C(i))
beta(i+1) = (ans(i)-A(i)*beta(i))/(A(i)*alpha(i) + C(i))
enddo
X(n) = (ans(n) - A(n)*beta(n))/(C(n)+A(n)*alpha(n))
do i = 1,n-1
X(n-i) = alpha(n-i+1)*X(n-i+1) + beta(n-i+1)
enddo
Out = X
end subroutine threediagsolve
real function k(x)
real x
k = x
end function
real function func(x,y)
real x,y
func = -4*x - 2
end function
real function g1(x)
real x
g1 = x**2
end function
real function g2(x)
real x
g2 = 16 + x**2
end function
real function g3(x)
real x
g3 = x**2
end function
real function g4(x)
real x
g4 = 16 + x**2
end function
real function u(x,y)
real x,y
u = x**2 + y**2
end function |