File tree 2 files changed +34
-1
lines changed
2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,8 @@ pages = [
9
9
" tutorials/parameter_identifiability.md" ],
10
10
" Examples" => Any[" Basic Examples" => Any[" examples/higher_order.md" ,
11
11
" examples/spring_mass.md" ,
12
- " examples/modelingtoolkitize_index_reduction.md" ],
12
+ " examples/modelingtoolkitize_index_reduction.md" ,
13
+ " examples/parsing.md" ],
13
14
" Advanced Examples" => Any[" examples/tearing_parallelism.md" ,
14
15
" examples/sparse_jacobians.md" ,
15
16
" examples/perturbation.md" ]],
Original file line number Diff line number Diff line change
1
+ # Parsing Expressions into Solvable Systems
2
+
3
+ Many times when creating DSLs or creating ModelingToolkit extensions to read new file formats,
4
+ it can become imperative to parse expressions. In many cases, it can be easy to use ` Base.parse `
5
+ to take things to standard Julia expressions, but how can you take a ` Base.Expr ` and generate
6
+ symbolic forms from that? For example, say we had the following system we wanted to solve:
7
+
8
+ ``` @example parsing
9
+ ex = [:(y ~ x)
10
+ :(y ~ -2x + 3 / z)
11
+ :(z ~ 2)]
12
+ ```
13
+
14
+ We can use the function ` parse_expr_to_symbolic ` from Symbolics.jl to generate the symbolic
15
+ form of the expression:
16
+
17
+ ``` @example parsing
18
+ Symbolics
19
+ eqs = parse_expr_to_symbolic.(ex, (Main,))
20
+ ```
21
+
22
+ From there, we can use ModelingToolkit to transform the symbolic equations into a numerical
23
+ nonlinear solve:
24
+
25
+ ``` @example parsing
26
+ using ModelingToolkit, NonlinearSolve
27
+ vars = union(ModelingToolkit.vars.(eqs)...)
28
+ @named ns = NonlinearSystem(eqs, vars, [])
29
+
30
+ prob = NonlinearProblem(ns,[1.0,1.0,1.0])
31
+ sol = solve(prob,NewtonRaphson())
32
+ ```
You can’t perform that action at this time.
0 commit comments