Skip to content

Commit e8a3aae

Browse files
Merge pull request #3732 from fchen121/master
Documentation for Change of Variables
2 parents 9d0b7ef + c09883e commit e8a3aae

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

docs/src/API/model_building.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ symbolic analysis.
196196

197197
```@docs
198198
liouville_transform
199+
change_of_variables
199200
stochastic_integral_transform
200201
Girsanov_transform
201202
change_independent_variable

src/ModelingToolkit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbanc
297297
hasunit, getunit, hasconnect, getconnect,
298298
hasmisc, getmisc, state_priority
299299
export liouville_transform, change_independent_variable, substitute_component,
300-
add_accumulations, noise_to_brownians, Girsanov_transform, changeofvariables
300+
add_accumulations, noise_to_brownians, Girsanov_transform, change_of_variables
301301
export PDESystem
302302
export Differential, expand_derivatives, @derivatives
303303
export Equation, ConstrainedEquation

src/systems/diffeqs/basic_transformations.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,34 +67,33 @@ using ModelingToolkit, OrdinaryDiffEq, Test
6767
# Change of variables: z = log(x)
6868
# (this implies that x = exp(z) is automatically non-negative)
6969
70-
@parameters t α
70+
@independent_variables t
71+
@parameters α
7172
@variables x(t)
7273
D = Differential(t)
7374
eqs = [D(x) ~ α*x]
7475
7576
tspan = (0., 1.)
76-
u0 = [x => 1.0]
77-
p = [α => -0.5]
77+
def = [x => 1.0, α => -0.5]
7878
79-
@named sys = ODESystem(eqs; defaults=u0)
80-
prob = ODEProblem(sys, [], tspan, p)
79+
@mtkcompile sys = System(eqs, t;defaults=def)
80+
prob = ODEProblem(sys, [], tspan)
8181
sol = solve(prob, Tsit5())
8282
8383
@variables z(t)
8484
forward_subs = [log(x) => z]
8585
backward_subs = [x => exp(z)]
86-
87-
@named new_sys = changeofvariables(sys, forward_subs, backward_subs)
86+
new_sys = change_of_variables(sys, t, forward_subs, backward_subs)
8887
@test equations(new_sys)[1] == (D(z) ~ α)
8988
90-
new_prob = ODEProblem(new_sys, [], tspan, p)
89+
new_prob = ODEProblem(new_sys, [], tspan)
9190
new_sol = solve(new_prob, Tsit5())
9291
9392
@test isapprox(new_sol[x][end], sol[x][end], atol=1e-4)
9493
```
9594
9695
"""
97-
function changeofvariables(
96+
function change_of_variables(
9897
sys::System, iv, forward_subs, backward_subs;
9998
simplify=true, t0=missing, isSDE=false
10099
)

test/changeofvariables.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ sol = solve(prob, Tsit5())
2626
@variables z(t)
2727
forward_subs = [log(x) => z]
2828
backward_subs = [x => exp(z)]
29-
new_sys = changeofvariables(sys, t, forward_subs, backward_subs)
29+
new_sys = change_of_variables(sys, t, forward_subs, backward_subs)
3030
@test equations(new_sys)[1] == (D(z) ~ α)
3131

3232
new_prob = ODEProblem(new_sys, [], tspan)
@@ -48,7 +48,7 @@ def = [x=>1., α => 1.]
4848
forward_subs = [t + α/(x+t) => z ]
4949
backward_subs = [ x => α/(z-t) - t]
5050

51-
new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true, t0=0.)
51+
new_sys = change_of_variables(sys, t, forward_subs, backward_subs; simplify=true, t0=0.)
5252
# output should be equivalent to
5353
# t^2 + α - z^2 + 2 (but this simplification is not found automatically)
5454

@@ -86,7 +86,7 @@ z = reshape(z, 3, 1)
8686
forward_subs = vec(T_inv*x .=> z)
8787
backward_subs = vec(x .=> T*z)
8888

89-
new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=true)
89+
new_sys = change_of_variables(sys, t, forward_subs, backward_subs; simplify=true)
9090

9191
new_prob = ODEProblem(new_sys, [], tspan)
9292
new_sol = solve(new_prob, Tsit5())
@@ -115,7 +115,7 @@ def = [x=>0., μ => 2., σ=>1.]
115115
@mtkcompile sys = System(eqs, t; defaults=def)
116116
forward_subs = [log(x) => y]
117117
backward_subs = [x => exp(y)]
118-
new_sys = changeofvariables(sys, t, forward_subs, backward_subs)
118+
new_sys = change_of_variables(sys, t, forward_subs, backward_subs)
119119
@test equations(new_sys)[1] == (D(y) ~ μ - 1/2*σ^2)
120120
@test noise_eqs(new_sys)[1] === value(σ)
121121

@@ -131,7 +131,7 @@ forward_subs = [log(x) => z, y^2 => w, log(u) => v]
131131
backward_subs = [x => exp(z), y => w^.5, u => exp(v)]
132132

133133
@mtkcompile sys = System(eqs, t; defaults=def)
134-
new_sys = changeofvariables(sys, t, forward_subs, backward_subs)
134+
new_sys = change_of_variables(sys, t, forward_subs, backward_subs)
135135
@test equations(new_sys)[1] == (D(z) ~ μ - 1/2*σ^2)
136136
@test equations(new_sys)[2] == (D(w) ~ α^2)
137137
@test equations(new_sys)[3] == (D(v) ~ μ - 1/2*^2 + σ^2))
@@ -144,7 +144,7 @@ new_sys = changeofvariables(sys, t, forward_subs, backward_subs)
144144

145145
# Test for Brownian instead of noise
146146
@named sys = System(eqs, t; defaults=def)
147-
new_sys = changeofvariables(sys, t, forward_subs, backward_subs; simplify=false)
147+
new_sys = change_of_variables(sys, t, forward_subs, backward_subs; simplify=false)
148148
@test simplify(equations(new_sys)[1]) == simplify((D(z) ~ μ - 1/2*σ^2 + σ*Bx))
149149
@test simplify(equations(new_sys)[2]) == simplify((D(w) ~ α^2 + 2*α*w^.5*By))
150150
@test simplify(equations(new_sys)[3]) == simplify((D(v) ~ μ - 1/2*^2 + σ^2) + σ*Bx + α*By))

0 commit comments

Comments
 (0)