# How to know variable order in the result after solving an ODE

**URL:** https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762
**Category:** New to Julia
**Tags:** ode, modelingtoolkit
**Created:** [April 21, 2023, 9:20pm UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762 "2023-04-21T21:20:02Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Ethan\_Tran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethan_tran/32/49198_2.png) [@Ethan\_Tran](https://discourse.julialang.org/u/Ethan_Tran)
#### Post date: [April 21, 2023, 9:20pm UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762/1 "2023-04-21T21:20:03Z")

</div>

Hi everyone, I tried to solve the ODE system with three sets of variables (V, theta, and delta) each set has 3 variables. After solving I got the array as a result. How could I know which value belongs to which variables?

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [April 21, 2023, 9:30pm UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762/2 "2023-04-21T21:30:42Z")

</div>

Hi @Ethan_Tran!  
Could you post a reproducible example so that we can help you understand it?

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

### Author: ![Ethan\_Tran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethan_tran/32/49198_2.png) [@Ethan\_Tran](https://discourse.julialang.org/u/Ethan_Tran)
#### Post date: [April 21, 2023, 9:44pm UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762/3 "2023-04-21T21:44:36Z")

</div>

```julia
@variables t (delta(t))[1:3] (V(t))[1:3] (theta(t))[1:3] (Id(t))[1:3] (Iq(t))[1:3]
D = Differential(t)
eqs = [
    0 ~ Ed[1] - V[1]*sin(delta[1] - theta[1]) + Iq[1]*0.0969,
    0 ~ Eq[1] - V[1]*cos(delta[1] - theta[1]) - Id[1]*0.0608,
    0 ~ Ed[2] - V[2]*sin(delta[2] - theta[2]) + Iq[2]*0.1969,
    0 ~ Eq[2] - V[2]*cos(delta[2] - theta[2]) - Id[2]*0.1198,
    0 ~ Ed[3] - V[3]*sin(delta[3] - theta[3]) + Iq[3]*0.25,
    0 ~ Eq[3] - V[3]*cos(delta[3] - theta[3]) - Id[3]*0.1813,
]
@named sys = ODESystem(eqs)
three_machine_ode = structural_simplify(sys)
tspan = (0.0,10.0)
h = 0.01
u0 = [
    delta[1] => 3.58*pi/180,
    delta[2] => 61.1*pi/180,
    delta[3] => 54.2*pi/180,
    V[1] => 1.0,
    V[2] => 1.0,
    V[3] => 1.0,
    theta[1] => 0.0,
    theta[2] => 0.0,
    theta[3] => 0.0,
    Id[1] => 0.302,
    Id[2] => 1.29,
    Id[3] => 0.562,
    Iq[1] => 0.671,
    Iq[2] => 0.931,
    Iq[3] => 0.619
]
prob = ODEProblem(sys,u0,tspan)
sol = solve(prob,dt = h, adaptive = false)

```

This is just a part of the equations in my model. After solving, the result is the array of values. I would like to know how I figure out which vector of values belongs to delta1, delta2, delta3, theta1, theta2,theta3 … Thank you for your time.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [April 21, 2023, 9:54pm UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762/4 "2023-04-21T21:54:50Z")

</div>

Can you try something like `dump(sol)` to see if your solution has named attributes? Or alternately, explore it in the VSCode Julia panel? I’m not an expert of ODEs + Symbolics but that’s what I would try to recover the individual variables

---

<div class="post-metadata">

### Author: ![Ethan\_Tran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethan_tran/32/49198_2.png) [@Ethan\_Tran](https://discourse.julialang.org/u/Ethan_Tran)
#### Post date: [April 21, 2023, 10:02pm UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762/5 "2023-04-21T22:02:58Z")

</div>

Thank you for your information, I am a newbie in Julia, so I have not tried `dump(sol)` before. I will try it.

---

<div class="post-metadata">

### Author: ![Ethan\_Tran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethan_tran/32/49198_2.png) [@Ethan\_Tran](https://discourse.julialang.org/u/Ethan_Tran)
#### Post date: [April 21, 2023, 10:24pm UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762/6 "2023-04-21T22:24:38Z")

</div>

I have another question how could I save data into a CSV file with the given header?

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [April 21, 2023, 10:27pm UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762/7 "2023-04-21T22:27:16Z")

</div>

Can you please tell us if “dump(sol)” works for you.

---

<div class="post-metadata">

### Author: ![Ethan\_Tran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethan_tran/32/49198_2.png) [@Ethan\_Tran](https://discourse.julialang.org/u/Ethan_Tran)
#### Post date: [April 21, 2023, 10:31pm UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762/8 "2023-04-21T22:31:43Z")

</div>

I have tried but I think it does not work for me.

---

<div class="post-metadata">

### Author: ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)
#### Post date: [April 22, 2023, 12:21am UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762/9 "2023-04-22T00:21:50Z")

</div>

> [@Ethan\_Tran](#):
>
> `@variables t (delta(t))[1:3] (V(t))[1:3] (theta(t))[1:3] (Id(t))[1:3] (Iq(t))[1:3]`

I don’t know the answer but I can suggest a way to find out. Look at the solution at time zero, which should equal the initial conditions, e.g. delta1=3.58\*pi/180. The initial delta states should this be identifiable, and that may be enough to deduce the rest. You could also simulate with distinct initial values for V[1:3] and theta[1:3], such as 1, 2, 3, etc. it doesn’t matter if they are nonsensical, it’s just to figure out the ordering.

I would guess the order is serial delta[1], delta[2], etc. because it’s hard to imagine it could be done as delta[1], V[1], theta[1], because there’s no expectation every variable is parallel 1:3. So serial makes the most sense.

Of course, the real answer is probably in the documentation, but the simple test is helpful for making a mental model for what’s going on.

---

<div class="post-metadata">

### Author: ![Ethan\_Tran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethan_tran/32/49198_2.png) [@Ethan\_Tran](https://discourse.julialang.org/u/Ethan_Tran)
#### Post date: [April 22, 2023, 12:48am UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762/10 "2023-04-22T00:48:38Z")

</div>

Thank you for your help. Actually, the model has a lot of sets of variables with the initial condition being zero. The code I posted is just a part of the model.

---

<div class="post-metadata">

### Author: ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)
#### Post date: [April 22, 2023, 1:23am UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762/11 "2023-04-22T01:23:47Z")

</div>

This shows how to access solution data using the symbolic variables instead of integer indexes. It is written for symbolic variables in Catalyst, but the guidance should be the same for solving via `ODESystem`s:

[FAQs · Catalyst.jl](https://docs.sciml.ai/Catalyst/stable/faqs/#How-to-index-solution-objects-using-symbolic-variables-and-observables)?

---

<div class="post-metadata">

### Author: ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)
#### Post date: [April 22, 2023, 2:32am UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762/12 "2023-04-22T02:32:19Z")

</div>

Yes, the idea is not to assign 0 to all the initial values, but to intentionally make them distinct so you can figure out the ordering. Just set them to 1, 2, 3, in the serial ordering and then do a simulation and look at sol(0) to see the initial values in order of state. If the actual ordering is parallel then you might see 1, 4, 7, etc. or something else. You also don’t need to do this for every single state, because you can probably figure out the pattern from the first three.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 22, 2023, 6:09am UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762/13 "2023-04-22T06:09:23Z")

</div>

See `states(sys)` for the variable order in the solution. You can also index the solution using symbols: `sol[var_name]`

---

<div class="post-metadata">

### Author: ![Ethan\_Tran](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ethan_tran/32/49198_2.png) [@Ethan\_Tran](https://discourse.julialang.org/u/Ethan_Tran)
#### Post date: [May 5, 2023, 10:09pm UTC](https://discourse.julialang.org/t/how-to-know-variable-order-in-the-result-after-solving-an-ode/97762/14 "2023-05-05T22:09:06Z")

</div>

Thank you for your information
