Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Exercise_Problems_03
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Numerics III
Exercise_Problems_03
Commits
9c6c69bf
Commit
9c6c69bf
authored
4 years ago
by
penrose
Browse files
Options
Downloads
Patches
Plain Diff
added boundary to matrix A, still an error at the corner of 1,1
parent
b7757101
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.ipynb_checkpoints/five_point_stencil-checkpoint.ipynb
+158
-42
158 additions, 42 deletions
.ipynb_checkpoints/five_point_stencil-checkpoint.ipynb
five_point_stencil.ipynb
+51
-136
51 additions, 136 deletions
five_point_stencil.ipynb
with
209 additions
and
178 deletions
.ipynb_checkpoints/five_point_stencil-checkpoint.ipynb
+
158
−
42
View file @
9c6c69bf
{
{
"cells": [
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from scipy.sparse import csr_matrix\n",
"from scipy.sparse.linalg import spsolve"
]
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": []
"source": [
"**The following poisson problem is given with Dirichlet boundary condition is given:**\n",
"$$-\\Delta u = f \\quad in \\; \\Omega = (0,1)^2$$\n",
"$$u = g \\quad on \\; \\partial \\Omega$$"
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#given exact solution\n",
"def u(x,y):\n",
" return pow(x,4)*pow(y,5)-17*np.sin(x*y)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"import numpy as np"
"#right-hand-side of the poisson equation\n",
"def fun(x,y):\n",
" return -(17*(x**2 + y**2)*np.sin(x*y) + 4*x**2*y**3+(5*x**2 + 3*y**2))"
]
]
},
},
{
{
...
@@ -25,104 +53,192 @@
...
@@ -25,104 +53,192 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
2
,
"execution_count":
77
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"def u(x,y):\n",
"def matrix_A(h):\n",
" return pow(x,4)*pow(y,5)-17*np.sin(x*y)"
" N = int(1/h)\n",
" m = pow(N-1,2)\n",
" A = pow(h,-2)*(np.zeros((m,m))-4*np.eye(m)+np.eye(m,k=1)+np.eye(m,k=-1)+np.eye(m,k=N-1)+np.eye(m,k=-(N-1)))\n",
" for i in range(N-2):\n",
" A[(i+1)*(N-1)-1][(i+1)*(N-1)] = 0\n",
" A[(i+1)*(N-1)][(i+1)*(N-1)-1] = 0\n",
" #A = csr_matrix(A)\n",
" return A"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
13
,
"execution_count":
78
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"def f(x,y):\n",
"def vector_f(h):\n",
" return -(12*pow(x,2)*pow(y,5)+20*pow(x,4)*pow(y,3)+(pow(x,2)+pow(y,2))*17*np.sin(x*y))"
" N = int(1/h)\n",
" l = pow(N-1,2)\n",
" f = np.zeros(l)\n",
" for i in range(N-1):\n",
" for k in range(N-1):\n",
" f[k+i*(N-1)]=fun((k+1)/(N),(i+1)/(N)) \n",
" return f"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"# just the initialisation of matrix B\n",
"def matrix_B(h):\n",
" N = int(1/h)\n",
" m = pow(N-1,2)\n",
" l = 4*N\n",
" B = np.zeros((m,l))\n",
" return B"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"def vector_g(h):\n",
" N = int(1/h)\n",
" l = 4*N\n",
" g = np.zeros(l)\n",
" g[-1] = u(1,1)\n",
" return g "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since the exact solution is zero at the boundary, the product B*g is zero."
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 15,
"execution_count":
1
15,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"n =
2
\n",
"n =
6
\n",
"h = pow(2,-n)\n",
"h = pow(2,-n)\n",
"N = pow(2,n)"
"N = pow(2,n)"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
29
,
"execution_count":
116
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"def matrix_A(h):\n",
"A=matrix_A(h)\n",
" N = int(1/h)\n",
"f=vector_f(h)\n",
" m = pow(N-1,2)\n",
"appr_u = np.linalg.solve(A,f)"
" A = np.zeros((m,m))\n",
" return A"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
30
,
"execution_count":
129
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"data": {
"data": {
"text/plain": [
"text/plain": [
"array([[0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
"4096.0"
" [0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0.]])"
]
]
},
},
"execution_count":
30
,
"execution_count":
129
,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
],
],
"source": [
"source": [
"
matrix_A(h)
"
"
A[3968,3905]
"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
3
1,
"execution_count": 1
30
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"def
matrix_B
(h):\n",
"def
exact_solution
(h):\n",
" N = int(1/h)\n",
" N = int(1/h)\n",
" m = pow(N-1,2)\n",
" l = pow(N-1,2)\n",
" l = 4*N\n",
" v = np.zeros(l)\n",
" B = np.zeros((m,l))\n",
" for i in range(N-1):\n",
" return B"
" for k in range(N-1):\n",
" v[k+i*(N-1)]=u((k+1)/(N),(i+1)/(N)) \n",
" return v"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 1,
"execution_count":
13
1,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"def vector_g(h):\n",
"v= exact_solution(h)"
" N = int(1/h)\n",
]
" l = 4*N\n",
},
" g = np.zeros(l)\n",
{
" g[-1] = u(1,1)\n",
"cell_type": "code",
" return g "
"execution_count": 132,
"metadata": {},
"outputs": [],
"source": [
"error = max(abs(appr_u - v))"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.015864236841443172"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"appr_u[-1]"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"13.161396848144852"
]
},
"execution_count": 134,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"error\n"
]
]
},
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": null,
...
...
%% Cell type:code id: tags:
```
python
import
numpy
as
np
from
scipy.sparse
import
csr_matrix
from
scipy.sparse.linalg
import
spsolve
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
**The following poisson problem is given with Dirichlet boundary condition is given:**
$$-
\D
elta u = f
\q
uad in
\;
\O
mega = (0,1)^2$$
$$u = g
\q
uad on
\;
\p
artial
\O
mega$$
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
import
numpy
as
np
#given exact solution
def
u
(
x
,
y
):
return
pow
(
x
,
4
)
*
pow
(
y
,
5
)
-
17
*
np
.
sin
(
x
*
y
)
```
%% Cell type:code id: tags:
```
python
#right-hand-side of the poisson equation
def
fun
(
x
,
y
):
return
-
(
17
*
(
x
**
2
+
y
**
2
)
*
np
.
sin
(
x
*
y
)
+
4
*
x
**
2
*
y
**
3
+
(
5
*
x
**
2
+
3
*
y
**
2
))
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
First, the components of the following equation will be assembled:
First, the components of the following equation will be assembled:
$$A
\u
nderline{u} =
\u
nderline{f} + B
\u
nderline{g}$$
$$A
\u
nderline{u} =
\u
nderline{f} + B
\u
nderline{g}$$
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
def
u
(
x
,
y
):
def
matrix_A
(
h
):
return
pow
(
x
,
4
)
*
pow
(
y
,
5
)
-
17
*
np
.
sin
(
x
*
y
)
N
=
int
(
1
/
h
)
m
=
pow
(
N
-
1
,
2
)
A
=
pow
(
h
,
-
2
)
*
(
np
.
zeros
((
m
,
m
))
-
4
*
np
.
eye
(
m
)
+
np
.
eye
(
m
,
k
=
1
)
+
np
.
eye
(
m
,
k
=-
1
)
+
np
.
eye
(
m
,
k
=
N
-
1
)
+
np
.
eye
(
m
,
k
=-
(
N
-
1
)))
for
i
in
range
(
N
-
2
):
A
[(
i
+
1
)
*
(
N
-
1
)
-
1
][(
i
+
1
)
*
(
N
-
1
)]
=
0
A
[(
i
+
1
)
*
(
N
-
1
)][(
i
+
1
)
*
(
N
-
1
)
-
1
]
=
0
#A = csr_matrix(A)
return
A
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
def
f
(
x
,
y
):
def
vector_f
(
h
):
return
-
(
12
*
pow
(
x
,
2
)
*
pow
(
y
,
5
)
+
20
*
pow
(
x
,
4
)
*
pow
(
y
,
3
)
+
(
pow
(
x
,
2
)
+
pow
(
y
,
2
))
*
17
*
np
.
sin
(
x
*
y
))
N
=
int
(
1
/
h
)
l
=
pow
(
N
-
1
,
2
)
f
=
np
.
zeros
(
l
)
for
i
in
range
(
N
-
1
):
for
k
in
range
(
N
-
1
):
f
[
k
+
i
*
(
N
-
1
)]
=
fun
((
k
+
1
)
/
(
N
),(
i
+
1
)
/
(
N
))
return
f
```
```
%% Cell type:raw id: tags:
# just the initialisation of matrix B
def matrix_B(h):
N = int(1/h)
m = pow(N-1,2)
l = 4
*
N
B = np.zeros((m,l))
return B
%% Cell type:raw id: tags:
def vector_g(h):
N = int(1/h)
l = 4
*
N
g = np.zeros(l)
g[-1] = u(1,1)
return g
%% Cell type:markdown id: tags:
Since the exact solution is zero at the boundary, the product B
*
g is zero.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
n
=
2
n
=
6
h
=
pow
(
2
,
-
n
)
h
=
pow
(
2
,
-
n
)
N
=
pow
(
2
,
n
)
N
=
pow
(
2
,
n
)
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
def
matrix_A
(
h
):
A
=
matrix_A
(
h
)
N
=
int
(
1
/
h
)
f
=
vector_f
(
h
)
m
=
pow
(
N
-
1
,
2
)
appr_u
=
np
.
linalg
.
solve
(
A
,
f
)
A
=
np
.
zeros
((
m
,
m
))
return
A
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
matrix_A
(
h
)
A
[
3968
,
3905
]
```
```
%% Output
%% Output
array([[0., 0., 0., 0., 0., 0., 0., 0., 0.],
4096.0
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0.]])
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
def
matrix_B
(
h
):
def
exact_solution
(
h
):
N
=
int
(
1
/
h
)
N
=
int
(
1
/
h
)
m
=
pow
(
N
-
1
,
2
)
l
=
pow
(
N
-
1
,
2
)
l
=
4
*
N
v
=
np
.
zeros
(
l
)
B
=
np
.
zeros
((
m
,
l
))
for
i
in
range
(
N
-
1
):
return
B
for
k
in
range
(
N
-
1
):
v
[
k
+
i
*
(
N
-
1
)]
=
u
((
k
+
1
)
/
(
N
),(
i
+
1
)
/
(
N
))
return
v
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
def
vector_g
(
h
):
v
=
exact_solution
(
h
)
N
=
int
(
1
/
h
)
```
l
=
4
*
N
g
=
np
.
zeros
(
l
)
%% Cell type:code id: tags:
g
[
-
1
]
=
u
(
1
,
1
)
return
g
```
python
error
=
max
(
abs
(
appr_u
-
v
))
```
%% Cell type:code id: tags:
```
python
appr_u
[
-
1
]
```
%% Output
0.015864236841443172
%% Cell type:code id: tags:
```
python
error
```
%% Output
13.161396848144852
%% Cell type:code id: tags:
```
python
``
`
``
`
%%
Cell
type
:
code
id
:
tags
:
%%
Cell
type
:
code
id
:
tags
:
```
python
```
python
```
```
...
...
This diff is collapsed.
Click to expand it.
five_point_stencil.ipynb
+
51
−
136
View file @
9c6c69bf
...
@@ -6,7 +6,9 @@
...
@@ -6,7 +6,9 @@
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"import numpy as np"
"import numpy as np\n",
"from scipy.sparse import csr_matrix\n",
"from scipy.sparse.linalg import spsolve"
]
]
},
},
{
{
...
@@ -31,13 +33,13 @@
...
@@ -31,13 +33,13 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
85
,
"execution_count":
3
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"#right-hand-side of the poisson equation\n",
"#right-hand-side of the poisson equation\n",
"def fun(x,y):\n",
"def fun(x,y):\n",
" return -(1
2*pow(x,2)*pow(y,5)+20*pow(x,4)*pow(y,3)+(pow(x,2)+pow(y,2))*17*np.sin(x*y
))"
" return -(1
7*(x**2 + y**2)*np.sin(x*y) + 4*x**2*y**3+(5*x**2 + 3*y**2
))"
]
]
},
},
{
{
...
@@ -51,7 +53,7 @@
...
@@ -51,7 +53,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 7
8
,
"execution_count": 7
7
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
...
@@ -59,12 +61,16 @@
...
@@ -59,12 +61,16 @@
" N = int(1/h)\n",
" N = int(1/h)\n",
" m = pow(N-1,2)\n",
" m = pow(N-1,2)\n",
" A = pow(h,-2)*(np.zeros((m,m))-4*np.eye(m)+np.eye(m,k=1)+np.eye(m,k=-1)+np.eye(m,k=N-1)+np.eye(m,k=-(N-1)))\n",
" A = pow(h,-2)*(np.zeros((m,m))-4*np.eye(m)+np.eye(m,k=1)+np.eye(m,k=-1)+np.eye(m,k=N-1)+np.eye(m,k=-(N-1)))\n",
" for i in range(N-2):\n",
" A[(i+1)*(N-1)-1][(i+1)*(N-1)] = 0\n",
" A[(i+1)*(N-1)][(i+1)*(N-1)-1] = 0\n",
" #A = csr_matrix(A)\n",
" return A"
" return A"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 8
1
,
"execution_count":
7
8,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
...
@@ -79,10 +85,8 @@
...
@@ -79,10 +85,8 @@
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "raw",
"execution_count": 79,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"# just the initialisation of matrix B\n",
"# just the initialisation of matrix B\n",
"def matrix_B(h):\n",
"def matrix_B(h):\n",
...
@@ -94,10 +98,8 @@
...
@@ -94,10 +98,8 @@
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "raw",
"execution_count": 80,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"def vector_g(h):\n",
"def vector_g(h):\n",
" N = int(1/h)\n",
" N = int(1/h)\n",
...
@@ -116,18 +118,18 @@
...
@@ -116,18 +118,18 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 1
07
,
"execution_count": 1
15
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"n =
3
\n",
"n =
6
\n",
"h = pow(2,-n)\n",
"h = pow(2,-n)\n",
"N = pow(2,n)"
"N = pow(2,n)"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 1
08
,
"execution_count": 1
16
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
...
@@ -138,192 +140,105 @@
...
@@ -138,192 +140,105 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 109,
"execution_count": 129,
"metadata": {},
"outputs": [],
"source": [
"def exact_solution(h):\n",
" N = int(1/h)\n",
" l = pow(N-1,2)\n",
" v = np.zeros(l)\n",
" for i in range(N-1):\n",
" for k in range(N-1):\n",
" v[k+i*(N-1)]=u((k+1)/(N),(i+1)/(N)) \n",
" return v"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'numpy.ndarray' object is not callable",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-110-a5ff9eca95bf>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mu\u001b[0m\u001b[0;34m=\u001b[0m \u001b[0mexact_solution\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mh\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-109-71aecd4cad10>\u001b[0m in \u001b[0;36mexact_solution\u001b[0;34m(h)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mN\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mN\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mv\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mN\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mu\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mN\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mN\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: 'numpy.ndarray' object is not callable"
]
}
],
"source": [
"u= exact_solution(h)"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "operands could not be broadcast together with shapes (49,) (16129,) ",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-106-a6b4164c5b68>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mabs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mappr_u\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mu\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (49,) (16129,) "
]
}
],
"source": [
"max(abs(appr_u - u))"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"data": {
"data": {
"text/plain": [
"text/plain": [
"
-13.228477586622512
"
"
4096.0
"
]
]
},
},
"execution_count":
9
1,
"execution_count": 1
29
,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
],
],
"source": [
"source": [
"
u[-1
]"
"
A[3968,3905
]"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
92
,
"execution_count":
130
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"data": {
"text/plain": [
"0.025380345287435015"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"appr_u[-1]"
"def exact_solution(h):\n",
" N = int(1/h)\n",
" l = pow(N-1,2)\n",
" v = np.zeros(l)\n",
" for i in range(N-1):\n",
" for k in range(N-1):\n",
" v[k+i*(N-1)]=u((k+1)/(N),(i+1)/(N)) \n",
" return v"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
95
,
"execution_count":
131
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"data": {
"text/plain": [
"(16129,)"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"
f.shape
"
"
v= exact_solution(h)
"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
96
,
"execution_count":
132
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [],
{
"data": {
"text/plain": [
"(16129, 16129)"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"source": [
"
A.shape
"
"
error = max(abs(appr_u - v))
"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
97
,
"execution_count":
133
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"data": {
"data": {
"text/plain": [
"text/plain": [
"
(16129,)
"
"
0.015864236841443172
"
]
]
},
},
"execution_count":
97
,
"execution_count":
133
,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
],
],
"source": [
"source": [
"u.shape"
"appr_u[-1]"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [],
"source": [
"ex=np.matmul(A,u)"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 1
00
,
"execution_count": 1
34
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"data": {
"data": {
"text/plain": [
"text/plain": [
"
434807.0641872273
"
"
13.161396848144852
"
]
]
},
},
"execution_count": 1
00
,
"execution_count": 1
34
,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
],
],
"source": [
"source": [
"e
x[-1]
"
"e
rror\n
"
]
]
},
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": null,
...
...
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
import
numpy
as
np
import
numpy
as
np
from
scipy.sparse
import
csr_matrix
from
scipy.sparse.linalg
import
spsolve
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
**The following poisson problem is given with Dirichlet boundary condition is given:**
**The following poisson problem is given with Dirichlet boundary condition is given:**
$$-
\D
elta u = f
\q
uad in
\;
\O
mega = (0,1)^2$$
$$-
\D
elta u = f
\q
uad in
\;
\O
mega = (0,1)^2$$
$$u = g
\q
uad on
\;
\p
artial
\O
mega$$
$$u = g
\q
uad on
\;
\p
artial
\O
mega$$
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
#given exact solution
#given exact solution
def
u
(
x
,
y
):
def
u
(
x
,
y
):
return
pow
(
x
,
4
)
*
pow
(
y
,
5
)
-
17
*
np
.
sin
(
x
*
y
)
return
pow
(
x
,
4
)
*
pow
(
y
,
5
)
-
17
*
np
.
sin
(
x
*
y
)
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
#right-hand-side of the poisson equation
#right-hand-side of the poisson equation
def
fun
(
x
,
y
):
def
fun
(
x
,
y
):
return
-
(
1
2
*
pow
(
x
,
2
)
*
pow
(
y
,
5
)
+
20
*
pow
(
x
,
4
)
*
pow
(
y
,
3
)
+
(
pow
(
x
,
2
)
+
pow
(
y
,
2
))
*
17
*
np
.
sin
(
x
*
y
))
return
-
(
1
7
*
(
x
**
2
+
y
**
2
)
*
np
.
sin
(
x
*
y
)
+
4
*
x
**
2
*
y
**
3
+
(
5
*
x
**
2
+
3
*
y
**
2
))
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
First, the components of the following equation will be assembled:
First, the components of the following equation will be assembled:
$$A
\u
nderline{u} =
\u
nderline{f} + B
\u
nderline{g}$$
$$A
\u
nderline{u} =
\u
nderline{f} + B
\u
nderline{g}$$
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
def
matrix_A
(
h
):
def
matrix_A
(
h
):
N
=
int
(
1
/
h
)
N
=
int
(
1
/
h
)
m
=
pow
(
N
-
1
,
2
)
m
=
pow
(
N
-
1
,
2
)
A
=
pow
(
h
,
-
2
)
*
(
np
.
zeros
((
m
,
m
))
-
4
*
np
.
eye
(
m
)
+
np
.
eye
(
m
,
k
=
1
)
+
np
.
eye
(
m
,
k
=-
1
)
+
np
.
eye
(
m
,
k
=
N
-
1
)
+
np
.
eye
(
m
,
k
=-
(
N
-
1
)))
A
=
pow
(
h
,
-
2
)
*
(
np
.
zeros
((
m
,
m
))
-
4
*
np
.
eye
(
m
)
+
np
.
eye
(
m
,
k
=
1
)
+
np
.
eye
(
m
,
k
=-
1
)
+
np
.
eye
(
m
,
k
=
N
-
1
)
+
np
.
eye
(
m
,
k
=-
(
N
-
1
)))
for
i
in
range
(
N
-
2
):
A
[(
i
+
1
)
*
(
N
-
1
)
-
1
][(
i
+
1
)
*
(
N
-
1
)]
=
0
A
[(
i
+
1
)
*
(
N
-
1
)][(
i
+
1
)
*
(
N
-
1
)
-
1
]
=
0
#A = csr_matrix(A)
return
A
return
A
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
def
vector_f
(
h
):
def
vector_f
(
h
):
N
=
int
(
1
/
h
)
N
=
int
(
1
/
h
)
l
=
pow
(
N
-
1
,
2
)
l
=
pow
(
N
-
1
,
2
)
f
=
np
.
zeros
(
l
)
f
=
np
.
zeros
(
l
)
for
i
in
range
(
N
-
1
):
for
i
in
range
(
N
-
1
):
for
k
in
range
(
N
-
1
):
for
k
in
range
(
N
-
1
):
f
[
k
+
i
*
(
N
-
1
)]
=
fun
((
k
+
1
)
/
(
N
),(
i
+
1
)
/
(
N
))
f
[
k
+
i
*
(
N
-
1
)]
=
fun
((
k
+
1
)
/
(
N
),(
i
+
1
)
/
(
N
))
return
f
return
f
```
```
%% Cell type:
code
id: tags:
%% Cell type:
raw
id: tags:
```
python
# just the initialisation of matrix B
# just the initialisation of matrix B
def matrix_B(h):
def matrix_B(h):
N = int(1/h)
N = int(1/h)
m = pow(N-1,2)
m = pow(N-1,2)
l = 4
*
N
l = 4
*
N
B = np.zeros((m,l))
B = np.zeros((m,l))
return B
return B
```
%% Cell type:
code
id: tags:
%% Cell type:
raw
id: tags:
```
python
def vector_g(h):
def vector_g(h):
N = int(1/h)
N = int(1/h)
l = 4
*
N
l = 4
*
N
g = np.zeros(l)
g = np.zeros(l)
g[-1] = u(1,1)
g[-1] = u(1,1)
return g
return g
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Since the exact solution is zero at the boundary, the product B
*
g is zero.
Since the exact solution is zero at the boundary, the product B
*
g is zero.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
n
=
3
n
=
6
h
=
pow
(
2
,
-
n
)
h
=
pow
(
2
,
-
n
)
N
=
pow
(
2
,
n
)
N
=
pow
(
2
,
n
)
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
A
=
matrix_A
(
h
)
A
=
matrix_A
(
h
)
f
=
vector_f
(
h
)
f
=
vector_f
(
h
)
appr_u
=
np
.
linalg
.
solve
(
A
,
f
)
appr_u
=
np
.
linalg
.
solve
(
A
,
f
)
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
A
[
3968
,
3905
]
```
%% Output
4096.0
%% Cell type:code id: tags:
```
python
def
exact_solution
(
h
):
def
exact_solution
(
h
):
N
=
int
(
1
/
h
)
N
=
int
(
1
/
h
)
l
=
pow
(
N
-
1
,
2
)
l
=
pow
(
N
-
1
,
2
)
v
=
np
.
zeros
(
l
)
v
=
np
.
zeros
(
l
)
for
i
in
range
(
N
-
1
):
for
i
in
range
(
N
-
1
):
for
k
in
range
(
N
-
1
):
for
k
in
range
(
N
-
1
):
v
[
k
+
i
*
(
N
-
1
)]
=
u
((
k
+
1
)
/
(
N
),(
i
+
1
)
/
(
N
))
v
[
k
+
i
*
(
N
-
1
)]
=
u
((
k
+
1
)
/
(
N
),(
i
+
1
)
/
(
N
))
return
v
return
v
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
u
=
exact_solution
(
h
)
v
=
exact_solution
(
h
)
```
```
%% Output
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-110-a5ff9eca95bf> in <module>
----> 1 u= exact_solution(h)
<ipython-input-109-71aecd4cad10> in exact_solution(h)
5 for i in range(N-1):
6 for k in range(N-1):
----> 7 v[k+i*(N-1)]=u((k+1)/(N),(i+1)/(N))
8 return v
TypeError: 'numpy.ndarray' object is not callable
%% Cell type:code id: tags:
```
python
max
(
abs
(
appr_u
-
u
))
```
%% Output
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-106-a6b4164c5b68> in <module>
----> 1 max(abs(appr_u - u))
ValueError: operands could not be broadcast together with shapes (49,) (16129,)
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
u
[
-
1
]
error
=
max
(
abs
(
appr_u
-
v
))
```
```
%% Output
-13.228477586622512
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
appr_u
[
-
1
]
appr_u
[
-
1
]
```
```
%% Output
%% Output
0.025380345287435015
0.015864236841443172
%% Cell type:code id: tags:
```
python
f
.
shape
```
%% Output
(16129,)
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
A
.
shape
error
```
```
%% Output
%% Output
(16129, 16129)
13.161396848144852
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
u
.
shape
``
`
``
`
%% Output
(16129,)
%% Cell type:code id: tags:
```
python
ex
=
np
.
matmul
(
A
,
u
)
```
%% Cell type:code id: tags:
```
python
ex
[
-
1
]
```
%% Output
434807.0641872273
%%
Cell
type
:
code
id
:
tags
:
%%
Cell
type
:
code
id
:
tags
:
```
python
```
python
```
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment