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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter10 Elliptical Partial Differential Equations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 10.1 page 441"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"u:\n",
"[[ 0. 0. 0. 0. 0. ]\n",
" [ 0. 6.25 12.5 18.75 25. ]\n",
" [ 0. 12.5 25. 37.5 50. ]\n",
" [ 0. 18.75 37.5 56.25 75. ]\n",
" [ 0. 25. 50. 75. 100. ]]\n"
]
}
],
"source": [
"from __future__ import division\n",
"from numpy import arange,zeros\n",
"from math import ceil\n",
"\n",
"h = 1/4\n",
"xf = 1\n",
"yf = 1\n",
"x = arange(0,h+xf,h)\n",
"y = arange(0,h+yf,h)\n",
"m = len(y)-1\n",
"n = len(x)-1\n",
"\n",
"u = zeros([m+1,n+1])\n",
"u[m,:] = [100*xx for xx in x]\n",
"u[:,n] = [100*yy for yy in y] # dash transpose\n",
"u0 = u\n",
"\n",
"I = int(ceil(m/2)) #\n",
"J = int(ceil(n/2)) #\n",
"\n",
"u[J,I] = (u0[J-2,I-2] + u0[J-2,I+2] + u0[J+2,I-2] + u0[J+2,I+2]) / 4\n",
"\n",
"for j in [J-1,J+1]:\n",
" for i in [I-1,I+1]:\n",
" u[j,i] = (u[j-1,i-1] + u[j-1,i+1] + u[j+1,i-1] + u[j+1,i+1]) / 4\n",
" \n",
"\n",
"# j and i may have loose 1\n",
"j1 = [J-1,J,J,J+1]\n",
"i1 = [I,I-1,I+1,I]\n",
"for k in range(0,4):\n",
" i = i1[(k)]\n",
" j = j1[(k)]\n",
" u[j,i] = (u[j,i-1] + u[j,i+1] + u[j-1,i] + u[j+1,i]) / 4\n",
"\n",
"\n",
"print \"u:\\n\",u"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 10.3 page 442"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" r u11 u21 u31 u12 u22 u32 u13 u23 u33\n",
" ------------------------------------------------------------------------------------------------\n",
" 1 6.2500 9.3750 6.2500 18.7500 25.0000 18.7500 43.7500 53.1250 43.7500 \n",
" 1 7.0312 9.5703 7.0801 18.9453 25.0977 18.9819 43.0176 52.9663 42.9871 \n",
" 1 7.1289 9.8267 7.2021 18.8110 25.1465 18.8339 42.9443 52.7695 42.9008 \n"
]
}
],
"source": [
"from numpy import zeros\n",
"m = 5\n",
"n = 5\n",
"u = zeros([m,n])\n",
"u[m-1,:] = [50,100, 100, 100, 50]\n",
"u0 = u\n",
"I = int(ceil(m/2))-1\n",
"J = int(ceil(n/2))-1\n",
"\n",
"u[J,I] = (u0[J-2,I-2] + u0[J-2,I+2] + u0[J+2,I-2] + u0[J+2,I+2]) / 4\n",
"\n",
"for j in [J-1,J+1]:\n",
" for i in [I-1,I+1]:\n",
" u[j,i] = (u[j-1,i-1] + u[j-1,i+1] + u[j+1,i-1] + u[j+1,i+1]) / 4\n",
" \n",
"\n",
"j1 = [J-1, J ,J ,J+1]\n",
"i1 = [I ,I-1, I+1, I]\n",
"for k in range(0,4):\n",
" i = i1[(k)]\n",
" j = j1[(k)]\n",
" u[j,i] = (u[j,i-1] + u[j,i+1] + u[j-1,i] + u[j+1,i]) / 4\n",
"\n",
"\n",
"kf = 2\n",
"tab = zeros([kf+1,(m-2)*(n-2)])\n",
"row = []\n",
"for j in range(1,n-1):\n",
" for xx in u[j,range(1,m-1)]:\n",
" row.append(xx)\n",
"\n",
"tab[0,:] = row;\n",
"for k in range(0,kf):\n",
" row = [];\n",
" for j in range(1,n-1):\n",
" for i in range(1,m-1):\n",
" u[j,i] = (u[j,i-1] + u[j,i+1] + u[j-1,i] + u[j+1,i]) / 4;\n",
" for xx in u[j,range(1,m-1)]: \n",
" row.append(xx)\n",
" \n",
" row = [round(roww*10**4,4)/10**4 for roww in row]\n",
" tab[k+1,:] = row\n",
"\n",
"print \"%4s %9s %9s %9s %9s %10s %10s %10s %10s %10s\"%('r','u11','u21','u31','u12','u22','u32','u13','u23','u33')\n",
"print ' ','-'*96\n",
"for tabb in tab:\n",
" i=1;\n",
" print ' ',i,' ',\n",
" for tt in tabb:\n",
" print ' %0.4f'%(tt),' ',\n",
" i+=1;\n",
" print"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 10.4 page 444"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" r u11 u21 u12 u22\n",
" 1 1.3889 1.7670 2.0170 2.6466 \n",
" 2 2.3349 2.6651 2.9151 3.0957 \n",
" 3 2.7840 2.8897 3.1397 3.2079 \n",
" 4 2.8962 2.9458 3.1958 3.2360 \n",
" 5 2.9243 2.9598 3.2098 3.2430 \n"
]
}
],
"source": [
"from numpy import arange\n",
"h = 1/3\n",
"x = arange(0,h+1,h)\n",
"y = arange(0,h+1,h)\n",
"m = len(y)\n",
"n = len(x)\n",
"u = zeros([m,n])\n",
"u[m-1,range(1,n-1)] = 1\n",
"\n",
"kf = 5\n",
"tab = zeros([kf,(m-2)*(n-2)])\n",
"for k in range(0,kf):\n",
" row = []\n",
" for j in range(1,n-1):\n",
" for i in range(1,m-1):\n",
" constant = 10/9* (5 + 1/9*(i-1)**2 + 1/9*(j-1)**2)\n",
" u[j,i] = (u[j,i-1] + u[j,i+1] + u[j-1,i] + u[j+1,i] + constant) / 4\n",
" for xx in u[j,range(1,m-1)]:\n",
" row.append(xx)\n",
" \n",
" row = [round(roww*10**4)/10**4 for roww in row]\n",
" tab[k,:] = row\n",
"\n",
"print \"%4s %9s %9s %9s %9s\"%('r','u11','u21','u12','u22')\n",
"i=1;\n",
"for tabb in tab:\n",
" \n",
" print ' ',i,' ',\n",
" for tt in tabb:\n",
" print ' %0.4f'%(tt),' ',\n",
" i+=1;\n",
" print"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 10.5 page 446"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"u:\n",
"[[ 0. 0. 0. 0. 0. ]\n",
" [ 0. 4. 8. 12. 16. ]\n",
" [ 0. 6.5 16. 22.5 32. ]\n",
" [ 0. 6. 15. 30. 48. ]\n",
" [ 0. 1. 8. 27. 64. ]]\n",
"\n",
"\n",
"\t u11 u21 u31 u12 u22 u32 u13 u23 u33\n",
" 1 4.0000 8.0000 12.0000 6.5000 16.0000 22.5000 6.0000 15.0000 30.0000 \n",
" 2 3.5605 7.8712 11.5228 6.3712 12.4085 21.3079 5.5228 13.8079 27.1039 \n",
" 3 3.5606 6.7013 10.9128 5.2013 11.6421 20.2611 4.9128 12.7611 26.9886 \n",
" 4 2.8750 6.2983 10.5929 4.7983 10.9244 20.1033 4.5929 12.6033 26.9160 \n",
" 5 2.7568 6.0290 10.5228 4.5290 10.7975 20.0515 4.5228 12.5515 26.8981 \n",
" 6 2.6193 5.9773 10.5045 4.4773 10.7587 20.0384 4.5045 12.5384 26.8935 \n",
" 7 2.6127 5.9675 10.5010 4.4675 10.7520 20.0363 4.5010 12.5363 26.8931 \n",
" 8 2.6081 5.9649 10.5002 4.4649 10.7504 20.0358 4.5002 12.5358 26.8929 \n",
" 9 2.6073 5.9644 10.5000 4.4644 10.7501 20.0357 4.5000 12.5357 26.8929 \n",
" 10 2.6072 5.9643 10.5000 4.4643 10.7500 20.0357 4.5000 12.5357 26.8929 \n",
" 11 2.6072 5.9643 10.5000 4.4643 10.7500 20.0357 4.5000 12.5357 26.8929 \n"
]
}
],
"source": [
"from math import ceil\n",
"from numpy import cos,pi,sqrt,zeros\n",
"x = range(0,5)\n",
"y = range(0,5)\n",
"m = len(y)\n",
"n = len(x)\n",
"u = zeros([m,n])\n",
"u[m-1,:] = [xx**3 for xx in x]\n",
"u[:,n-1] = [16*yy for yy in y]\n",
"u0 = u\n",
"\n",
"I = int(ceil(m/2))-1\n",
"J = int(ceil(n/2))-1\n",
"\n",
"u[J,I] = (u0[J-2,I-2] + u0[J-2,I+2] + u0[J+2,I-2] + u0[J+2,I+2]) / 4\n",
"\n",
"for j in [J-1,J+1]:\n",
" for i in [I-1,I+1]:\n",
" u[j,i] = (u[j-1,i-1] + u[j-1,i+1] + u[j+1,i-1] + u[j+1,i+1]) / 4\n",
" \n",
"\n",
"\n",
"j1 = [J-1,J ,J ,J+1]\n",
"i1 = [I, I-1, I+1, I]\n",
"for k in range(0,4):\n",
" i = i1[(k)]\n",
" j = j1[(k)]\n",
" u[j,i] = (u[j,i-1] + u[j,i+1] + u[j-1,i] + u[j+1,i]) / 4\n",
"\n",
"print \"u:\\n\",u\n",
"\n",
"p = m-1\n",
"q = n-1\n",
"c = cos(pi/p) + cos(pi/q)\n",
"w = 4/(2+sqrt(4-c**2))\n",
"w = round(w*10**3)/10**3\n",
"\n",
"kf = 10\n",
"tab = zeros([kf+1,(m-2)*(n-2)])\n",
"row = []\n",
"for j in range(1,n-1):\n",
" for xx in u[j,range(1,m-1)]:\n",
" row.append(xx)\n",
"\n",
"tab[0,:] = row\n",
"for k in range(0,kf):\n",
" row = []\n",
" for j in range(1,n-1):\n",
" for i in range(1,m-1):\n",
" u[j,i] = (u[j,i-1] + u[j,i+1] + u[j-1,i] + u[j+1,i]) *w/4 + (1-w)*u[j,i]\n",
" for xx in u[j,range(1,m-1)]:\n",
" row.append(xx)\n",
" \n",
" row = [round(roww*10**4)/10**4 for roww in row]\n",
" tab[k+1,:] = row\n",
"\n",
"print \"\\n\\n\\t%8s %9s %10s %10s %9s %10s %10s %9s %9s\"%('u11','u21','u31','u12','u22','u32','u13','u23','u33')\n",
"i=1;\n",
"for tabb in tab:\n",
" \n",
" print ' ',i,' ',\n",
" for tt in tabb:\n",
" print ' %0.4f'%(tt),' ',\n",
" i+=1;\n",
" print\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|