# Plot only real part of solution

**URL:** <https://discourse.julialang.org/t/plot-only-real-part-of-solution/80167>\
**Category:** Visualization\
**Tags:** modelingtoolkit\
**Created:** [April 27, 2022, 7:25pm UTC](https://discourse.julialang.org/t/plot-only-real-part-of-solution/80167 "2022-04-27T19:25:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ograsdijk](https://avatars.discourse-cdn.com/v4/letter/o/d26b3c/32.png) [@ograsdijk](https://discourse.julialang.org/u/ograsdijk)\
**Post date:** [April 27, 2022, 7:25pm UTC](https://discourse.julialang.org/t/plot-only-real-part-of-solution/80167/1 "2022-04-27T19:25:03Z")

</div>

I have some ODE systems generated with ModelingToolkit that have complex solutions but I only want to plot the real part of the solution. I know I can pull out the solution for a specific component manually and use `real` to get the real part of the solution, but I’d like to use the `plot(sol, vars = [])` interface since that is much more convenient.

MWE

```nohighlight
using ModelingToolkit
using DifferentialEquations
using Plots

# define variables and parameters of system
@variables t ρ[1:2, 1:2](t) δ(t) Ω(t)
@parameters Δ Ω0 ω ϕ δ0 icomplex
D = Differential(t)

# Hamiltonian
H = [
    0 Ω
    Ω Δ+δ
]

# bloch equations
eq = -icomplex * Symbolics.scalarize(H*ρ - ρ*H)
eqns = [D(ρ[idx, idy]) ~ eq[idx,idy] for idx in 1:2 for idy in 1:2]
# time varying detuning
append!(eqns, [δ ~ δ0*cos(ω*t+ϕ)])
append!(eqns, [Ω ~ Ω0*sin(ω*t+ϕ)])

# create symbolic system
@named bloch = ODESystem(eqns)
# structural_simplify simplifies a system of equations if possible by removing
# reduntancies etc.
bloch_simplified = structural_simplify(bloch)

# initial conditions
ρᵢ = zeros(ComplexF64,2,2)
ρᵢ[1,1] = 1
u0 = [ρ[idx,idy] => ρᵢ[idx,idy] for idx in 1:2 for idy in 1:2]

# initial parameters
p = [Ω0 => 1., Δ => 20., δ0 => 1., ω => 20., ϕ =>0, icomplex => 1im]

# setup and solve of ODE, t from 0 => π
prob = ODEProblem(bloch_simplified, u0, (0., π), p, jac = true)
sol = solve(prob, Tsit5())

plot(sol, vars = [ρ[1,1], ρ[2,2]])

```

In this case I could use `plot(sol, vars = [abs(ρ[1,1]), abs(ρ[2,2])])` because these components of the solution never go below zero, but other components like `δ` and `Ω` do. I tried using `plot(sol, vars = [real(ρ[1,1]), real(ρ[2,2])])` but that doesn’t seem to work.

---

<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 27, 2022, 8:36pm UTC](https://discourse.julialang.org/t/plot-only-real-part-of-solution/80167/2 "2022-04-27T20:36:26Z")

</div>

Open an issue. This seems like a difficult case, but we can get it.

---

<div class="post-metadata">

**Author:** ![ograsdijk](https://avatars.discourse-cdn.com/v4/letter/o/d26b3c/32.png) [@ograsdijk](https://discourse.julialang.org/u/ograsdijk)\
**Post date:** [April 27, 2022, 8:38pm UTC](https://discourse.julialang.org/t/plot-only-real-part-of-solution/80167/3 "2022-04-27T20:38:48Z")

</div>

An issue in ModelingToolkit or DifferentialEquations? I’m guessing ModelingToolkit but just want to make sure.

---

<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 27, 2022, 8:58pm UTC](https://discourse.julialang.org/t/plot-only-real-part-of-solution/80167/4 "2022-04-27T20:58:08Z")

</div>

Put it to MTK, we might move it around. I’m not quite sure how to solve this right now, so I’m not quite sure where the issue lives either. But that’s my problem, not yours 😅
