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
b7757101
Commit
b7757101
authored
4 years ago
by
penrose
Browse files
Options
Downloads
Patches
Plain Diff
error is still big
parent
13f39215
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
five_point_stencil.ipynb
+227
-80
227 additions, 80 deletions
five_point_stencil.ipynb
with
227 additions
and
80 deletions
five_point_stencil.ipynb
+
227
−
80
View file @
b7757101
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": 1,
...
...
@@ -18,9 +13,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"
First, the components of the following equation will be assembled:
\n",
"\n",
"$$
A \\underline{u} = \\underline{f} + B\\underline{g}
$$"
"
**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
$$"
]
},
{
...
...
@@ -29,34 +24,34 @@
"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
,
"execution_count":
85
,
"metadata": {},
"outputs": [],
"source": [
"def f(x,y):\n",
"#right-hand-side of the poisson equation\n",
"def fun(x,y):\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))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"
n = 3
\n",
"
h = pow(2,-n)
\n",
"
N = pow(2,n)
"
"
First, the components of the following equation will be assembled:
\n",
"\n",
"
$$A \\underline{u} = \\underline{f} + B\\underline{g}$$
"
]
},
{
"cell_type": "code",
"execution_count":
19
,
"execution_count":
78
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -69,30 +64,27 @@
},
{
"cell_type": "code",
"execution_count":
25
,
"execution_count":
81
,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(49, 49)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"matrix_A(h).shape"
"def vector_f(h):\n",
" 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": "code",
"execution_count":
31
,
"execution_count":
79
,
"metadata": {},
"outputs": [],
"source": [
"# just the initialisation of matrix B\n",
"def matrix_B(h):\n",
" N = int(1/h)\n",
" m = pow(N-1,2)\n",
...
...
@@ -103,7 +95,7 @@
},
{
"cell_type": "code",
"execution_count":
1
,
"execution_count":
80
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -115,66 +107,221 @@
" 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",
"execution_count": 11,
"execution_count": 107,
"metadata": {},
"outputs": [],
"source": [
"n = 3\n",
"h = pow(2,-n)\n",
"N = pow(2,n)"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [],
"source": [
"A=matrix_A(h)\n",
"f=vector_f(h)\n",
"appr_u = np.linalg.solve(A,f)"
]
},
{
"cell_type": "code",
"execution_count": 109,
"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": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[0;31mSignature:\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meye\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mN\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mM\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0;32mclass\u001b[0m \u001b[0;34m'float'\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0morder\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'C'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDocstring:\u001b[0m\n",
"Return a 2-D array with ones on the diagonal and zeros elsewhere.\n",
"\n",
"Parameters\n",
"----------\n",
"N : int\n",
" Number of rows in the output.\n",
"M : int, optional\n",
" Number of columns in the output. If None, defaults to `N`.\n",
"k : int, optional\n",
" Index of the diagonal: 0 (the default) refers to the main diagonal,\n",
" a positive value refers to an upper diagonal, and a negative value\n",
" to a lower diagonal.\n",
"dtype : data-type, optional\n",
" Data-type of the returned array.\n",
"order : {'C', 'F'}, optional\n",
" Whether the output should be stored in row-major (C-style) or\n",
" column-major (Fortran-style) order in memory.\n",
"\n",
" .. versionadded:: 1.14.0\n",
"\n",
"Returns\n",
"-------\n",
"I : ndarray of shape (N,M)\n",
" An array where all elements are equal to zero, except for the `k`-th\n",
" diagonal, whose values are equal to one.\n",
"\n",
"See Also\n",
"--------\n",
"identity : (almost) equivalent function\n",
"diag : diagonal 2-D array from a 1-D array specified by the user.\n",
"\n",
"Examples\n",
"--------\n",
">>> np.eye(2, dtype=int)\n",
"array([[1, 0],\n",
" [0, 1]])\n",
">>> np.eye(3, k=1)\n",
"array([[0., 1., 0.],\n",
" [0., 0., 1.],\n",
" [0., 0., 0.]])\n",
"\u001b[0;31mFile:\u001b[0m ~/miniconda3/envs/Manhatten/lib/python3.7/site-packages/numpy/lib/twodim_base.py\n",
"\u001b[0;31mType:\u001b[0m function\n"
"-13.228477586622512"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "display_data"
"output_type": "execute_result"
}
],
"source": [
"u[-1]"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.025380345287435015"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"appr_u[-1]"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(16129,)"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f.shape"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(16129, 16129)"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A.shape"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(16129,)"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"u.shape"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [],
"source": [
"ex=np.matmul(A,u)"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"434807.0641872273"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"
np.eye?
"
"
ex[-1]
"
]
},
{
...
...
%% Cell type:markdown id: tags:
%% Cell type:code id: tags:
```
python
import
numpy
as
np
```
%% Cell type:markdown id: tags:
First, the components of the following equation will be assembled:
$$
A
\u
nderline{u} =
\u
nderline{f} + B
\u
nderline{g}
$$
**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:
```
python
#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
def
f
(
x
,
y
):
#right-hand-side of the poisson equation
def
fun
(
x
,
y
):
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
))
```
%% Cell type:
code
id: tags:
%% Cell type:
markdown
id: tags:
```
python
n
=
3
h
=
pow
(
2
,
-
n
)
N
=
pow
(
2
,
n
)
```
First, the components of the following equation will be assembled:
$$A
\u
nderline{u} =
\u
nderline{f} + B
\u
nderline{g}$$
%% Cell type:code id: tags:
```
python
def
matrix_A
(
h
):
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
)))
return
A
```
%% Cell type:code id: tags:
```
python
matrix_A
(
h
).
shape
def
vector_f
(
h
):
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
```
%% Output
(49, 49)
%% Cell type:code id: tags:
```
python
# 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:code id: tags:
```
python
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:
```
python
n
=
3
h
=
pow
(
2
,
-
n
)
N
=
pow
(
2
,
n
)
```
%% Cell type:code id: tags:
```
python
A
=
matrix_A
(
h
)
f
=
vector_f
(
h
)
appr_u
=
np
.
linalg
.
solve
(
A
,
f
)
```
%% Cell type:code id: tags:
```
python
def
exact_solution
(
h
):
N
=
int
(
1
/
h
)
l
=
pow
(
N
-
1
,
2
)
v
=
np
.
zeros
(
l
)
for
i
in
range
(
N
-
1
):
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:
```
python
u
=
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:
```
python
u
[
-
1
]
```
%% Output
-13.228477586622512
%% Cell type:code id: tags:
```
python
appr_u
[
-
1
]
```
%% Output
0.025380345287435015
%% Cell type:code id: tags:
```
python
f
.
shape
```
%% Output
(16129,)
%% Cell type:code id: tags:
```
python
A
.
shape
```
%% Output
(16129, 16129)
%% Cell type:code id: tags:
```
python
u
.
shape
```
%% Output
(16129,)
%% Cell type:code id: tags:
```
python
ex
=
np
.
matmul
(
A
,
u
)
```
%% Cell type:code id: tags:
```
python
np
.
eye
?
ex
[
-
1
]
```
%% Output
434807.0641872273
%% Cell type:code id: tags:
```
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