@@ -5,7 +5,7 @@ modeling system for the Julia SciML ecosystem. It adds a high-level interactive
5
5
for the numerical solvers which can make it easy to symbolically modify and generate
6
6
equations to be solved. The basic form of using ModelingToolkit looks as follows:
7
7
8
- ``` @example mtk
8
+ ``` julia
9
9
using ModelingToolkit, NonlinearSolve
10
10
11
11
@variables x y z
@@ -30,20 +30,20 @@ sol = solve(prob, NewtonRaphson())
30
30
As a symbolic system, ModelingToolkit can be used to represent the equations and derive new
31
31
forms. For example, let's look at the equations:
32
32
33
- ``` @example mtk
33
+ ``` julia
34
34
equations (ns)
35
35
```
36
36
37
37
We can ask it what the Jacobian of our system is via ` calculate_jacobian ` :
38
38
39
- ``` @example mtk
39
+ ``` julia
40
40
calculate_jacobian (ns)
41
41
```
42
42
43
43
We can tell MTK to generate a computable form of this analytical Jacobian via ` jac = true `
44
44
to help the solver use efficient forms:
45
45
46
- ``` @example mtk
46
+ ``` julia
47
47
prob = NonlinearProblem (ns, u0, ps, jac = true )
48
48
sol = solve (prob, NewtonRaphson ())
49
49
```
@@ -54,7 +54,7 @@ One of the major reasons for using ModelingToolkit is to allow structural simpli
54
54
the systems. It's very easy to write down a mathematical model that, in theory, could be
55
55
solved more simply. Let's take a look at a quick system:
56
56
57
- ``` @example mtk
57
+ ``` julia
58
58
@variables u1 u2 u3 u4 u5
59
59
eqs = [0 ~ u1 - sin (u5), 0 ~ u2 - cos (u1), 0 ~ u3 - hypot (u1, u2),
60
60
0 ~ u4 - hypot (u2, u3), 0 ~ u5 - hypot (u4, u1)]
@@ -63,47 +63,47 @@ eqs = [0 ~ u1 - sin(u5), 0 ~ u2 - cos(u1), 0 ~ u3 - hypot(u1, u2),
63
63
64
64
If we run structural simplification, we receive the following form:
65
65
66
- ``` @example mtk
66
+ ``` julia
67
67
sys = structural_simplify (sys)
68
68
```
69
69
70
- ``` @example mtk
70
+ ``` julia
71
71
equations (sys)
72
72
```
73
73
74
74
How did it do this? Let's look at the ` observed ` to see the relationships that it found:
75
75
76
- ``` @example mtk
76
+ ``` julia
77
77
observed (sys)
78
78
```
79
79
80
80
Using ModelingToolkit, we can build and solve the simplified system:
81
81
82
- ``` @example mtk
82
+ ``` julia
83
83
u0 = [u5 .=> 1.0 ]
84
84
prob = NonlinearProblem (sys, u0)
85
85
sol = solve (prob, NewtonRaphson ())
86
86
```
87
87
88
88
We can then use symbolic indexing to retrieve any variable:
89
89
90
- ``` @example mtk
90
+ ``` julia
91
91
sol[u1]
92
92
```
93
93
94
- ``` @example mtk
94
+ ``` julia
95
95
sol[u2]
96
96
```
97
97
98
- ``` @example mtk
98
+ ``` julia
99
99
sol[u3]
100
100
```
101
101
102
- ``` @example mtk
102
+ ``` julia
103
103
sol[u4]
104
104
```
105
105
106
- ``` @example mtk
106
+ ``` julia
107
107
sol[u5]
108
108
```
109
109
0 commit comments