You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/examples/perturbation.md
+19-12Lines changed: 19 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,21 +2,27 @@
2
2
3
3
## Prelims
4
4
5
-
In the previous tutorial, [Mixed Symbolic-Numeric Perturbation Theory](https://symbolics.juliasymbolics.org/stable/examples/perturbation/), we discussed how to solve algebraic equations using **Symbolics.jl**. Here, our goal is to extend the method to differential equations. First, we import the following helper functions that were introduced in [Mixed Symbolic/Numerical Methods for Perturbation Theory - Algebraic Equations](@ref perturb_alg):
5
+
In the previous tutorial, [Mixed Symbolic-Numeric Perturbation Theory](https://symbolics.juliasymbolics.org/stable/examples/perturbation/), we discussed how to solve algebraic equations using **Symbolics.jl**. Here, our goal is to extend the method to differential equations. First, we import the following helper functions that were introduced in [Mixed Symbolic/Numerical Methods for Perturbation Theory - Algebraic Equations](https://symbolics.juliasymbolics.org/stable/examples/perturbation/):
6
6
7
7
```julia
8
8
using Symbolics, SymbolicUtils
9
9
10
-
def_taylor(x, ps) =sum([a * x^i for (i, a) inenumerate(ps)])
11
-
def_taylor(x, ps, p₀) = p₀ +def_taylor(x, ps)
10
+
def_taylor(x, ps) =sum([a * x^(i -1) for (i, a) inenumerate(ps)])
12
11
13
12
functioncollect_powers(eq, x, ns; max_power =100)
14
13
eq =substitute(expand(eq), Dict(x^j =>0for j in (last(ns) +1):max_power))
15
14
16
15
eqs = []
17
16
for i in ns
18
17
powers =Dict(x^j => (i == j ?1:0) for j in1:last(ns))
19
-
push!(eqs, substitute(eq, powers))
18
+
e =substitute(eq, powers)
19
+
20
+
# manually remove zeroth order from higher orders
21
+
if0in ns && i !=0
22
+
e = e - eqs[1]
23
+
end
24
+
25
+
push!(eqs, e)
20
26
end
21
27
eqs
22
28
end
@@ -46,20 +52,21 @@ with the initial conditions $x(0) = 0$, and $\dot{x}(0) = 1$. Note that for $\ep
46
52
47
53
```julia
48
54
using ModelingToolkit: t_nounits as t, D_nounits as D
49
-
n =3
50
-
@variables ϵ y[1:n](t) ∂∂y[1:n](t)
55
+
order =2
56
+
n = order +1
57
+
@variables ϵ (y(t))[1:n] (∂∂y(t))[1:n]
51
58
```
52
59
53
60
Next, we define $x$.
54
61
55
62
```julia
56
-
x =def_taylor(ϵ, y[3:end], y[2])
63
+
x =def_taylor(ϵ, y)
57
64
```
58
65
59
66
We need the second derivative of `x`. It may seem that we can do this using `Differential(t)`; however, this operation needs to wait for a few steps because we need to manipulate the differentials as separate variables. Instead, we define dummy variables `∂∂y` as the placeholder for the second derivatives and define
60
67
61
68
```julia
62
-
∂∂x =def_taylor(ϵ, ∂∂y[3:end], ∂∂y[2])
69
+
∂∂x =def_taylor(ϵ, ∂∂y)
63
70
```
64
71
65
72
as the second derivative of `x`. After rearrangement, our governing equation is $\ddot{x}(t)(1 + \epsilon x(t))^{-2} + 1 = 0$, or
@@ -71,7 +78,7 @@ eq = ∂∂x * (1 + ϵ * x)^2 + 1
71
78
The next two steps are the same as the ones for algebraic equations (note that we pass `1:n` to `collect_powers` because the zeroth order term is needed here)
72
79
73
80
```julia
74
-
eqs =collect_powers(eq, ϵ, 1:n)
81
+
eqs =collect_powers(eq, ϵ, 0:order)
75
82
```
76
83
77
84
and,
@@ -99,8 +106,8 @@ unknowns(sys)
99
106
```julia
100
107
# the initial conditions
101
108
# everything is zero except the initial velocity
102
-
u0 =zeros(2n+2)
103
-
u0[3] =1.0#y₀ˍt
109
+
u0 =zeros(2order+2)
110
+
u0[2] =1.0#yˍt₁
104
111
105
112
prob =ODEProblem(sys, u0, (0, 3.0))
106
113
sol =solve(prob; dtmax =0.01)
@@ -109,7 +116,7 @@ sol = solve(prob; dtmax = 0.01)
109
116
Finally, we calculate the solution to the problem as a function of `ϵ` by substituting the solution to the ODE system back into the defining equation for `x`. Note that `𝜀` is a number, compared to `ϵ`, which is a symbolic variable.
110
117
111
118
```julia
112
-
X = 𝜀 ->sum([𝜀^(i -1) * sol[y[i]]fori ineachindex(y)])
119
+
X = 𝜀 ->sum([𝜀^(i -1) * sol[yi]for(i, yi) inenumerate(y)])
113
120
```
114
121
115
122
Using `X`, we can plot the trajectory for a range of $𝜀$s.
0 commit comments