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
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
felixhenneke
Exercise_Problems_03
Commits
13f39215
Commit
13f39215
authored
4 years ago
by
penrose
Browse files
Options
Downloads
Patches
Plain Diff
matrix A assembled
parent
32deb0b8
No related branches found
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
+71
-17
71 additions, 17 deletions
five_point_stencil.ipynb
with
71 additions
and
17 deletions
five_point_stencil.ipynb
+
71
−
17
View file @
13f39215
...
@@ -35,7 +35,7 @@
...
@@ -35,7 +35,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
1
3,
"execution_count": 3,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
...
@@ -45,54 +45,46 @@
...
@@ -45,54 +45,46 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 1
5
,
"execution_count": 1
8
,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"n =
2
\n",
"n =
3
\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":
2
9,
"execution_count":
1
9,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"def matrix_A(h):\n",
"def matrix_A(h):\n",
" N = int(1/h)\n",
" N = int(1/h)\n",
" m = pow(N-1,2)\n",
" m = pow(N-1,2)\n",
" A = np.zeros((m,m))\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",
" return A"
" return A"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count":
30
,
"execution_count":
25
,
"metadata": {},
"metadata": {},
"outputs": [
"outputs": [
{
{
"data": {
"data": {
"text/plain": [
"text/plain": [
"array([[0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
"(49, 49)"
" [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":
25
,
"metadata": {},
"metadata": {},
"output_type": "execute_result"
"output_type": "execute_result"
}
}
],
],
"source": [
"source": [
"matrix_A(h)"
"matrix_A(h)
.shape
"
]
]
},
},
{
{
...
@@ -123,6 +115,68 @@
...
@@ -123,6 +115,68 @@
" return g "
" return g "
]
]
},
},
{
"cell_type": "code",
"execution_count": 11,
"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"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"np.eye?"
]
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": null,
...
...
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
import
numpy
as
np
import
numpy
as
np
```
```
%% 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
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
def
f
(
x
,
y
):
def
f
(
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
))
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:code id: tags:
```
python
```
python
n
=
2
n
=
3
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
):
def
matrix_A
(
h
):
N
=
int
(
1
/
h
)
N
=
int
(
1
/
h
)
m
=
pow
(
N
-
1
,
2
)
m
=
pow
(
N
-
1
,
2
)
A
=
np
.
zeros
((
m
,
m
))
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
return
A
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
matrix_A
(
h
)
matrix_A
(
h
)
.
shape
```
```
%% Output
%% Output
array([[0., 0., 0., 0., 0., 0., 0., 0., 0.],
(49, 49)
[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
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:code id: tags:
```
python
```
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:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
np
.
eye
?
```
%% Output
%% 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