# How to (properly) use the solution of a simulation in ModelingToolkit

**URL:** https://discourse.julialang.org/t/how-to-properly-use-the-solution-of-a-simulation-in-modelingtoolkit/59686
**Category:** Modelling & Simulations
**Created:** [April 20, 2021, 6:39pm UTC](https://discourse.julialang.org/t/how-to-properly-use-the-solution-of-a-simulation-in-modelingtoolkit/59686 "2021-04-20T18:39:28Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![CodeLenz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/codelenz/32/3419_2.png) [@CodeLenz](https://discourse.julialang.org/u/CodeLenz)
#### Post date: [April 20, 2021, 6:39pm UTC](https://discourse.julialang.org/t/how-to-properly-use-the-solution-of-a-simulation-in-modelingtoolkit/59686/1 "2021-04-20T18:39:28Z")

</div>

Hi there.

I am starting to use the amazing ModellingToolkit, but I am afraid I am missing something important. So I apologize if my questions is silly.

Suppose I have a model consisting of two differential equations, two dependent variables x1 and x2 and an independent variable t. For example:

```julia
 
 @parameters t a1 a2;
 @variables x1(t) x2(t);

 D = Differential(t);
 D2 = Differential(t)^2;

 f(t) = 0.1*sin(2*pi*t);
 T1 = a1*f(t)
 T2 = a2*f(t)

 eq = [ D2(x1) ~ T1 - D(x2),
            D2(x2) ~ T2 - D(x1) ];
 sys = ODESystem(eq);
 sys = ode_order_lowering(sys);

 tspan = (0.0,10.0);
 p =[a1 => 1.0,
     a2 => 2.0 ];

 u0 =[ D(x1) => 0.0, 
       x1 => 0.0, 
       D(x2) => 0.0, 
       x2 => 0.0, 
        ];

 prob = ODEProblem(sys,u0,tspan,p,jac=true)
 sol = solve(prob);

```

I can plot the solution x1(t) even without knowing the position of this variable in `pos(t)`, since there is a Plot recipe

```julia
 plot(sol,vars=(x1));

```

My question is. Suppose I want to make some mathematical operations on `x1(t)`. For example, evaluate the integral of `(x1(t)/(a+b))^2` over the time span.

How can I “recover” x1(t) from sol and use it along with the symbolic operations? For example,

```julia
expr = substitute( (x1/(a+b))^2, p) 

```

will substitute the values of a and b, as expected. But if I try to use it with QuadGK, for example, it will give me an error, since x1(t) is not linked to sol(t).

I guess I am missing something important about it and I really want to understand how to properly use the solution provided by DifferentialEquations here.

Thank you.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 20, 2021, 7:38pm UTC](https://discourse.julialang.org/t/how-to-properly-use-the-solution-of-a-simulation-in-modelingtoolkit/59686/2 "2021-04-20T19:38:41Z")

</div>

`sol[x1]`?

---

<div class="post-metadata">

### Author: ![CodeLenz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/codelenz/32/3419_2.png) [@CodeLenz](https://discourse.julialang.org/u/CodeLenz)
#### Post date: [April 20, 2021, 8:08pm UTC](https://discourse.julialang.org/t/how-to-properly-use-the-solution-of-a-simulation-in-modelingtoolkit/59686/3 "2021-04-20T20:08:26Z")

</div>

Thank you @ChrisRackauckas

Is it possible to interpolate, like in sol(t)? I would like to call something like `sol(t)[x1]` or `sol[x1](t)..`

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 20, 2021, 8:15pm UTC](https://discourse.julialang.org/t/how-to-properly-use-the-solution-of-a-simulation-in-modelingtoolkit/59686/4 "2021-04-20T20:15:05Z")

</div>

Not yet. It will be `sol(t;idxs=x1)` when it’s ready.

---

<div class="post-metadata">

### Author: ![CodeLenz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/codelenz/32/3419_2.png) [@CodeLenz](https://discourse.julialang.org/u/CodeLenz)
#### Post date: [April 20, 2021, 8:16pm UTC](https://discourse.julialang.org/t/how-to-properly-use-the-solution-of-a-simulation-in-modelingtoolkit/59686/5 "2021-04-20T20:16:02Z")

</div>

OK.

Thank you very much!
