Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
nmm_2020_site
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Erik Strand
nmm_2020_site
Commits
ce65245f
Commit
ce65245f
authored
5 years ago
by
Erik Strand
Browse files
Options
Downloads
Patches
Plain Diff
Start writing notes on differentiable simulation
parent
c3248461
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
_code/notes/diffsim/derivations.py
+28
-0
28 additions, 0 deletions
_code/notes/diffsim/derivations.py
_notes/differentiable_simulation.md
+62
-0
62 additions, 0 deletions
_notes/differentiable_simulation.md
with
90 additions
and
0 deletions
_code/notes/diffsim/derivations.py
0 → 100644
+
28
−
0
View file @
ce65245f
from
sympy
import
*
from
sympy.assumptions.assume
import
global_assumptions
def
print_expr
(
name
,
expr
):
print
(
name
+
"
:
"
)
print
(
latex
(
expr
))
print
(
""
)
t
=
Symbol
(
'
t
'
,
real
=
True
)
dt
=
Symbol
(
'
\Delta t
'
,
real
=
True
)
x
=
Symbol
(
'
x
'
,
real
=
True
)
xn
=
Function
(
'
x_n
'
,
real
=
True
)
f
=
Function
(
'
f
'
,
real
=
True
)
def
euler
():
expr
=
xn
(
x
)
+
dt
*
f
(
t
,
xn
(
x
))
return
simplify
(
Derivative
(
expr
,
x
))
def
rk4
():
k1
=
dt
*
f
(
t
,
xn
(
x
))
k2
=
dt
*
f
(
t
+
dt
/
2
,
xn
(
x
)
+
k1
/
2
)
k3
=
dt
*
f
(
t
+
dt
/
2
,
xn
(
x
)
+
k2
/
2
)
k4
=
dt
*
f
(
t
+
dt
,
xn
(
x
)
+
k3
)
expr
=
xn
(
x
)
+
k1
/
6
+
k2
/
3
+
k3
/
3
+
k4
/
6
return
simplify
(
Derivative
(
expr
,
x
))
print_expr
(
"
euler
"
,
euler
())
print_expr
(
"
rk4
"
,
rk4
())
This diff is collapsed.
Click to expand it.
_notes/differentiable_simulation.md
0 → 100644
+
62
−
0
View file @
ce65245f
---
title
:
Differentiable Simulation
---
## ODEs
Let's consider ODEs of the form $$y'(x) = f(x, y)$$. Assuming a fixed step size $$
\D
elta x$$, the
most common integration schemes are basically functions $$g$$ such that
$$
y(x +
\D
elta x)
\a
pprox g(x, y)
$$
For instance, Euler integration uses $$g(x, y) = y + (
\D
elta x) f(x, y)$$.
The function $$g$$ is applied recursively to produce a series of points, starting from some initial
condition $$y_0$$.
$$
\b
egin{aligned}
y_1 &= g(0, y_0)
\\
y_2 &= g(
\D
elta t, y_1)
\\
y_3 &= g(2
\D
elta t, y_2)
\\
\v
dots
\e
nd{aligned}
$$
We can write this more compactly as the recurrence relation
$$
y_{n+1} = g(n
\D
elta t, y_n)
$$
By differentiating both sides of this relation and applying the chain rule, we find that
$$
\f
rac{
\p
artial}{
\p
artial y_0} y_{n+1} =
\f
rac{
\p
artial}{
\p
artial y_n} g(n
\D
elta t, y_n)
\f
rac{
\p
artial}{
\p
artial y_0} y_n
$$
Assuming that we can compute $$
\p
artial g /
\p
artial y$$ on demand, this gives a convenient
recurrence relation for $$
\p
artial y_{n+1} /
\p
artial y_0$$ in terms of $$
\p
artial y_{n} /
\p
artial
y_0$$. So to differentiate through such a simulation, all we need to do is figure out $$
\p
artial g /
\p
artial y$$, and compute the series of derivate values $$
\p
artial y_{n} /
\p
artial y_0$$ alongside
the values $$y_n$$.
### Euler
As mentioned above, for Euler integration $$g(x, y) = y + (
\D
elta x) f(x, y)$$. Thus
$$
\f
rac{
\p
artial}{
\p
artial y} g(x, y) = 1 + (
\D
elta x)
\f
rac{
\p
artial}{
\p
artial y} f(x, y)
$$
And
$$
\b
egin{aligned}
\f
rac{
\p
artial}{
\p
artial y_0} y_{n+1}
&=
\f
rac{
\p
artial}{
\p
artial y_n} g(n
\D
elta t, y_n)
\f
rac{
\p
artial}{
\p
artial y_0} y_n
\\
&=
\l
eft( 1 + (
\D
elta x)
\f
rac{
\p
artial}{
\p
artial y} f(x, y)
\r
ight)
\f
rac{
\p
artial}{
\p
artial y_0} y_n
\\
\e
nd{aligned}
$$
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