Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
comp-sci
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
nguyed99
comp-sci
Commits
50e5d686
Commit
50e5d686
authored
1 year ago
by
nguyed99
Browse files
Options
Downloads
Patches
Plain Diff
Update UB4
parent
fda0cd08
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
UB4/UB4.py
+103
-55
103 additions, 55 deletions
UB4/UB4.py
with
103 additions
and
55 deletions
UB4/UB4.py
+
103
−
55
View file @
50e5d686
...
...
@@ -3,7 +3,7 @@ import matplotlib.pyplot as plt
# Ex1: Numerical integrators
def
implicit_euler
(
f
,
y0
:
np
.
ndarray
,
t
:
float
,
dt
:
float
)
->
np
.
ndarray
:
def
implicit_euler
(
f
,
y0
:
np
.
ndarray
,
t
:
float
,
dt
:
float
,
**
kwargs
)
->
np
.
ndarray
:
"""
A-stable
:param f: function to be integrated
...
...
@@ -16,12 +16,12 @@ def implicit_euler(f, y0: np.ndarray, t: float, dt: float) -> np.ndarray:
y
[
0
]
=
y0
for
i
in
range
(
1
,
no_of_steps
):
y
[
i
]
=
y
[
i
-
1
]
+
f
(
y
[
i
-
1
])
*
dt
y
[
i
]
=
y
[
i
-
1
]
+
f
(
y
[
i
])
*
dt
y
[
i
]
=
y
[
i
-
1
]
+
f
(
y
[
i
-
1
]
,
**
kwargs
)
*
dt
y
[
i
]
=
y
[
i
-
1
]
+
f
(
y
[
i
]
,
**
kwargs
)
*
dt
return
y
def
explicit_euler
(
f
,
y0
:
np
.
ndarray
,
t
:
float
,
dt
:
float
)
->
np
.
ndarray
:
def
explicit_euler
(
f
,
y0
:
np
.
ndarray
,
t
:
float
,
dt
:
float
,
**
kwargs
)
->
np
.
ndarray
:
"""
:param f: function to be integrated
:param y0: initial value
...
...
@@ -33,11 +33,11 @@ def explicit_euler(f, y0: np.ndarray, t: float, dt: float) -> np.ndarray:
y
[
0
]
=
y0
for
i
in
range
(
1
,
no_of_steps
):
y
[
i
]
=
y
[
i
-
1
]
+
f
(
y
[
i
-
1
])
*
dt
y
[
i
]
=
y
[
i
-
1
]
+
f
(
y
[
i
-
1
]
,
**
kwargs
)
*
dt
return
y
def
implicit_midpoint
(
f
,
y0
:
np
.
ndarray
,
t
:
float
,
dt
:
float
)
->
np
.
ndarray
:
def
implicit_midpoint
(
f
,
y0
:
np
.
ndarray
,
t
:
float
,
dt
:
float
,
**
kwargs
)
->
np
.
ndarray
:
"""
Not L-stable, doesn
'
t decay properly - oscillation
:param f: function to be integrated
...
...
@@ -50,74 +50,122 @@ def implicit_midpoint(f, y0: np.ndarray, t: float, dt: float) -> np.ndarray:
y
[
0
]
=
y0
for
i
in
range
(
1
,
no_of_steps
):
y
[
i
]
=
y
[
i
-
1
]
+
dt
*
f
(
y
[
i
-
1
]
+
dt
/
2
*
f
(
y
[
i
-
1
]
)
)
y
[
i
]
=
y
[
i
-
1
]
+
dt
*
f
(
1
/
2
*
(
y
[
i
-
1
]
+
y
[
i
]))
y
[
i
]
=
y
[
i
-
1
]
+
dt
*
f
(
y
[
i
-
1
]
+
dt
/
2
*
f
(
y
[
i
-
1
]
,
**
kwargs
),
**
kwargs
)
y
[
i
]
=
y
[
i
-
1
]
+
dt
*
f
(
1
/
2
*
(
y
[
i
-
1
]
+
y
[
i
])
,
**
kwargs
)
return
y
# Ex2: dy/dt = lambda * y, y(0) = 1, lambda = -1
y0
=
np
.
array
([
1
])
lamda
=
-
1
t
=
100
dt
=
1e-2
t
=
[
10
,
100
]
dt
=
[
1.5
,
1
,
1e-1
,
1e-2
]
f
=
lambda
y
:
lamda
*
y
y
=
[
implicit_euler
(
f
,
y0
,
t
,
dt
),
explicit_euler
(
f
,
y0
,
t
,
dt
),
implicit_midpoint
(
f
,
y0
,
t
,
dt
)]
integrators
=
[
'
implicit euler
'
,
'
explicit euler
'
,
'
implicit midpoint
'
]
exact_solution
=
lambda
t
:
np
.
exp
(
lamda
*
t
)
*
y0
# for i in range(3):
# A-stability: the method should produce a numerical solution that does not grow in magnitude for any dt
# L-stability: stronger form of A-stability -> numerical solution decays (at least) as rapidly as the exact solution
# Expectation:
## EE: A-stable (No), L-stable (No)
## IE: A-stable (Yes), L-stable (Yes)
## IM: A-stable (Yes), L-stable (No)
# for i in range(len(dt)):
# plt.figure()
# plt.plot(y[i])
# plt.title(integrators[i])
# y_implicit_euler = implicit_euler(f, y0, t[0], dt[i])
# plt.plot(implicit_euler(f, y0, t[0], dt[i]), '--', label = "implicit euler (IE)")
# plt.plot(explicit_euler(f, y0, t[0], dt[i]), 'x', label = "explicit euler (EE)")
# plt.plot(implicit_midpoint(f, y0, t[0], dt[i]), '>', label = "implicit midpoint (IM)")
# time = np.arange(int(t[0] // dt[i]))
# plt.plot(exact_solution(time), '3', label="exact solution (ES)")
# plt.legend()
# plt.title(f't={t[0]}, dt={dt[i]}')
# plt.show()
# Ex3:
def
f
(
y
:
np
.
ndarray
,
k
:
float
=
1
)
->
np
.
ndarray
:
assert
y
.
shape
[
0
]
==
3
,
'
y has the wrong dimension. It should be 3
'
A
=
np
.
array
([[
-
1
,
1
,
0
],
[
1
,
-
1
-
k
,
k
],
[
0
,
k
,
-
k
]])
return
np
.
dot
(
A
,
y
)
dt
=
[
1
,
1e-1
,
1e-2
,
1e-3
]
t
=
[
10
,
100
]
y0
=
np
.
array
([
1
,
0
,
0
])
# short time
fig
=
plt
.
figure
()
for
dt_i
in
dt
:
y
=
implicit_euler
(
f
,
y0
,
t
[
0
],
dt_i
)
plt
.
plot
(
y
[:,
0
],
'
--
'
,
label
=
'
State 1
'
)
plt
.
plot
(
y
[:,
1
],
'
x
'
,
label
=
'
State 2
'
)
plt
.
plot
(
y
[:,
2
],
'
>
'
,
label
=
'
State 3
'
)
#### For A-stability, dt < 1
#### Observation
### t = 10, dt = 1.5 <-- Strange behaviour !
# IE grows and EE decays!
# IM is robust
### t = 10, dt = 1 <-- Strange behaviour !
# IE gives a constant function (but it doesn't blow up as time progresses!).
# However, this means it's not A-stable because it does not appropriately damp the solution.
# EE & IM decay faster than ES -> L-stable. EE gives a worse approximation in comparison to IM
### t = 10, dt = 1e-1 <-- Strange behaviour !
# They all behave similarly. They do not decay as rapidly as ES.
### t = 10, dt = 1e-2 <-- Strange behaviour !
# They all behave similarly. They do not decay as rapidly as ES.
for
i
in
range
(
len
(
dt
)):
plt
.
figure
()
y_implicit_euler
=
implicit_euler
(
f
,
y0
,
t
[
1
],
dt
[
i
])
plt
.
plot
(
implicit_euler
(
f
,
y0
,
t
[
1
],
dt
[
i
]),
'
--
'
,
label
=
"
implicit euler (IE)
"
)
plt
.
plot
(
explicit_euler
(
f
,
y0
,
t
[
1
],
dt
[
i
]),
'
x
'
,
label
=
"
explicit euler (EE)
"
)
plt
.
plot
(
implicit_midpoint
(
f
,
y0
,
t
[
1
],
dt
[
i
]),
'
>
'
,
label
=
"
implicit midpoint (IM)
"
)
time
=
np
.
arange
(
int
(
t
[
1
]
//
dt
[
i
]))
plt
.
plot
(
exact_solution
(
time
),
'
3
'
,
label
=
"
exact solution (ES)
"
)
plt
.
legend
()
plt
.
title
(
f
'
{
integrators
[
0
]
}
, dt =
{
dt_i
}
'
)
plt
.
title
(
f
'
t=
{
t
[
1
]
}
, dt=
{
dt
[
i
]
}
'
)
plt
.
show
()
#### Observation
### t = 100, dt = 1.5 <-- Strange behaviour !
# IE blows up at the end.
### t = 100, dt = 1 <-- Strange behaviour !
# Same as in the case t = 10, dt = 1
### t = 100, dt = 1e-1 <-- Strange behaviour !
# Same as in the case t = 100, dt = 1e-1
### t = 100, dt = 1e-2 <-- Strange behaviour !
# Same as in the case t = 100, dt = 1e-2
# Ex3:
# def f(y: np.ndarray, k: float) -> np.ndarray:
# assert y.shape[0] == 3, 'y has the wrong dimension. It should be 3'
# A = np.array([[-1, 1, 0],
# [1, -1-k, k],
# [0, k, -k]])
# return np.dot(A, y)
# dt = [1, 1e-1, 1e-2, 1e-3]
# t = [10,100]
# y0 = np.array([1,0,0])
# # short time
# fig = plt.figure()
# for dt_i in dt:
# y = implicit_euler(f, y0, t[0], dt_i, k=1)
# plt.plot(y[:,0], '--',label = 'State 1')
# plt.plot(y[:,1], 'x', label = 'State 2')
# plt.plot(y[:,2], '>', label = 'State 3')
# plt.legend()
# plt.title(f'{integrators[0]}, dt = {dt_i}')
# plt.show()
fig
=
plt
.
figure
()
for
dt_i
in
dt
:
y
=
explicit_euler
(
f
,
y0
,
t
[
0
],
dt_i
)
plt
.
plot
(
y
[:,
0
],
'
--
'
,
label
=
'
State 1
'
)
plt
.
plot
(
y
[:,
1
],
'
x
'
,
label
=
'
State 2
'
)
plt
.
plot
(
y
[:,
2
],
'
>
'
,
label
=
'
State 3
'
)
plt
.
legend
()
plt
.
title
(
f
'
{
integrators
[
1
]
}
, dt =
{
dt_i
}
'
)
plt
.
show
()
fig
=
plt
.
figure
()
for
dt_i
in
dt
:
y
=
implicit_midpoint
(
f
,
y0
,
t
[
0
],
dt_i
)
plt
.
plot
(
y
[:,
0
],
'
--
'
,
label
=
'
State 1
'
)
plt
.
plot
(
y
[:,
1
],
'
x
'
,
label
=
'
State 2
'
)
plt
.
plot
(
y
[:,
2
],
'
>
'
,
label
=
'
State 3
'
)
plt
.
legend
()
plt
.
title
(
f
'
{
integrators
[
2
]
}
, dt =
{
dt_i
}
'
)
plt
.
show
()
# fig = plt.figure()
# for dt_i in dt:
# y = explicit_euler(f, y0, t[0], dt_i, k=1)
# plt.plot(y[:,0], '--', label = 'State 1')
# plt.plot(y[:,1], 'x', label = 'State 2')
# plt.plot(y[:,2], '>', label = 'State 3')
# plt.legend()
# plt.title(f'{integrators[1]}, dt = {dt_i}')
# plt.show()
# fig = plt.figure()
# for dt_i in dt:
# y = implicit_midpoint(f, y0, t[0], dt_i, k=1)
# plt.plot(y[:,0], '--', label = 'State 1')
# plt.plot(y[:,1], 'x', label = 'State 2')
# plt.plot(y[:,2], '>', label = 'State 3')
# plt.legend()
# plt.title(f'{integrators[2]}, dt = {dt_i}')
# plt.show()
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