# Help solving coupled differential equations involving complex numbers

**URL:** https://discourse.julialang.org/t/help-solving-coupled-differential-equations-involving-complex-numbers/89893
**Category:** Modelling & Simulations
**Tags:** differentialequation
**Created:** [November 7, 2022, 4:24pm UTC](https://discourse.julialang.org/t/help-solving-coupled-differential-equations-involving-complex-numbers/89893 "2022-11-07T16:24:43Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)
#### Post date: [November 7, 2022, 4:24pm UTC](https://discourse.julialang.org/t/help-solving-coupled-differential-equations-involving-complex-numbers/89893/1 "2022-11-07T16:24:43Z")

</div>

Original title: “Some help solving set of differential equations”

Hi. I am stuck in doing some homework, and I was hoping I could get some help.  
The task is to analytically or numerically solve some differential equations. I will provide the full task for completeness:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/f/2fa913d8e34b2e525ba56cdca4b5e9c2ee126005.png)

These are the DE’s to solve, in the following task:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/b/3b3f76b034396f066ebf9965a2945e650a375a0f.png)  
“Real valued” Rabi frequencies means that \Omega is simply some real number, which reduces the complexity quite a bit. Below is my attempt at defining this, using ModelingToolkit.jl:

```julia
using ModelingToolkit
using DifferentialEquations

@variables t c̃₁(t) c̃₂(t) # independent and dependent variables
@parameters Ω Δ # parameters 

∂ₜ = Differential(t) # define an operator for the differentiation w.r.t. time

```

Inspired by the docs, I tried to define a vector of equations:

```julia
@named eqs = ODESystem([∂ₜ(c̃₁) ~ Ω*im*c̃₂*cis(t*Δ),
                        ∂ₜ(c̃₂) ~ Ω*im*c̃₁*cis(-t*Δ)
])

```

Resulting in

```julia
ERROR: type Array has no field lhs
Stacktrace:

```

So instead, I tried what I found inntuitive - defining two equations, and giving _those_ as a vector:

```julia
julia> @named eq1 = ODESystem(∂ₜ(c̃₁) ~ Ω*im*c̃₂*cis(t*Δ))
Model eq1 with 2 equations
States (2):
  c̃₁(t)
  c̃₂(t)
Parameters (2):
  Ω
  Δ

julia> @named eq2 = ODESystem(∂ₜ(c̃₂) ~ Ω*im*c̃₁*cis(-t*Δ))
Model eq2 with 2 equations
States (2):
  c̃₂(t)
  c̃₁(t)
Parameters (2):
  Ω
  Δ

```

So far so good I think. I do not get why both equations say “with 2 equations”.  
I then attempt:

```julia
julia> using DifferentialEquations: solve

julia> initial_conds = [c̃₁ => 1.0, c̃₂ => 0.0];

julia> tspan = (0.0, 10.0);

julia> params = [Ω => 1.0, Δ => 1.0];

julia> prob = ODEProblem([eq1, eq2], initial_conds, tspan, params)
ERROR: No methods were found for the model function passed to the equation solver.
The function `f` needs to have dispatches, for example, for an ODEProblem
`f` must define either `f(u,p,t)` or `f(du,u,p,t)`. For more information
on how the model function `f` should be defined, consult the docstring for
the appropriate `AbstractSciMLFunction`.

Offending function: f
Stacktrace:
 [1] isinplace(f::Vector{ODESystem}, inplace_param_number::Int64, fname::String, iip_preferred::Bool; has_two_dispatches::Bool, isoptimization::Bool)
   @ SciMLBase ~/.julia/packages/SciMLBase/RAGXU/src/utils.jl:236
 [2] isinplace (repeats 2 times)
   @ ~/.julia/packages/SciMLBase/RAGXU/src/utils.jl:229 [inlined]
 [3] ODEProblem(f::Vector{ODESystem}, u0::Vector{Pair{Num, Float64}}, tspan::Tuple{Float64, Float64}, p::Vector{Pair{Num, Float64}}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ SciMLBase ~/.julia/packages/SciMLBase/RAGXU/src/problems/ode_problems.jl:168
 [4] ODEProblem(f::Vector{ODESystem}, u0::Vector{Pair{Num, Float64}}, tspan::Tuple{Float64, Float64}, p::Vector{Pair{Num, Float64}})
   @ SciMLBase ~/.julia/packages/SciMLBase/RAGXU/src/problems/ode_problems.jl:167
 [5] top-level scope
   @ REPL[33]:1

```

Now I realize that I could be going about it the wrong way. Is it even an ODE system, or something else? I do however not know how to proceed, and would much appreciate some pointers.

Do anyone have the know-how and time to help me out?

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [November 7, 2022, 9:11pm UTC](https://discourse.julialang.org/t/help-solving-coupled-differential-equations-involving-complex-numbers/89893/2 "2022-11-07T21:11:15Z")

</div>

ModelingToolkit variables are all real, as written you get

```julia
julia> eqs = [∂ₜ(c̃₁) ~ Ω*im*c̃₂*cis(t*Δ),
              ∂ₜ(c̃₂) ~ Ω*im*c̃₁*cis(-t*Δ)
       ]
2-element Vector{Vector{Equation}}:
 [Differential(t)(c̃₁(t)) ~ -Ω*c̃₂(t)*sin(t*Δ), 0 ~ Ω*c̃₂(t)*cos(t*Δ)]
 [Differential(t)(c̃₂(t)) ~ -Ω*c̃₁(t)*sin(-t*Δ), 0 ~ Ω*c̃₁(t)*cos(-t*Δ)]

```

Which probably isn’t what you mean. Try defining c̃₁ and c̃₂ with explicit real and imaginary parts.

```julia
@variables c̃r₁ c̃i₁ c̃₁ c̃r₂ c̃i₂ c̃₂
c̃₁ = c̃r₁ + im*c̃i₁
c̃₂ = c̃r₂ + im*c̃i₂

```

---

<div class="post-metadata">

### Author: ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)
#### Post date: [November 8, 2022, 7:38am UTC](https://discourse.julialang.org/t/help-solving-coupled-differential-equations-involving-complex-numbers/89893/3 "2022-11-08T07:38:15Z")

</div>

> [@contradict](#):
>
> ```julia
> 
> ```

Thank you for your suggestion.  
With the following definitions:

```julia
@variables t c̃r₁ c̃i₁ c̃₁ c̃r₂ c̃i₂ c̃₂ # independent and dependent variables
@parameters Ω Δ # parameters 

c̃₁ = c̃r₁ + im*c̃i₁
c̃₂ = c̃r₂ + im*c̃i₂

∂ₜ = Differential(t) # define an operator for the differentiation w.r.t. time

```

I get the following error:

```julia
julia> @named eqs = ODESystem([∂ₜ(c̃₁) ~ Ω*im*c̃₂*cis(t*Δ),
                               ∂ₜ(c̃₂) ~ Ω*im*c̃₁*cis(-t*Δ)
       ])
ERROR: type ComplexTerm has no field metadata
Stacktrace:
  [1] getproperty(x::Symbolics.ComplexTerm{Real}, f::Symbol)
 ...

```

I get the same error when defining a single equation:

```julia
julia> @named eq1 = ODESystem(∂ₜ(c̃₁) ~ Ω*im*c̃₂*cis(t*Δ))
ERROR: type ComplexTerm has no field metadata

```

Should I define seperate equations for the real and imaginary parts, giving me 4 equations? If yes, I am unsure how to even go about that. Should I use the `real` and `imag` functions in defining the equations?

---

<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: [November 8, 2022, 11:25am UTC](https://discourse.julialang.org/t/help-solving-coupled-differential-equations-involving-complex-numbers/89893/4 "2022-11-08T11:25:45Z")

</div>

Open an issue. This is a very specific question to ModelingToolkit with complex numbers and it would be helpful if you title it as such.

---

<div class="post-metadata">

### Author: ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)
#### Post date: [November 8, 2022, 8:44pm UTC](https://discourse.julialang.org/t/help-solving-coupled-differential-equations-involving-complex-numbers/89893/5 "2022-11-08T20:44:04Z")

</div>

Done. Are you saying this could be solved without ModelingToolkit? Any quick pointers on how?

---

<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: [November 9, 2022, 10:27am UTC](https://discourse.julialang.org/t/help-solving-coupled-differential-equations-involving-complex-numbers/89893/6 "2022-11-09T10:27:55Z")

</div>

Just use complex numbers in DiffEq directly.

---

<div class="post-metadata">

### Author: ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)
#### Post date: [November 9, 2022, 11:13am UTC](https://discourse.julialang.org/t/help-solving-coupled-differential-equations-involving-complex-numbers/89893/7 "2022-11-09T11:13:36Z")

</div>

Thanks guys. This ended up doing the trick:

```julia
using DifferentialEquations
using ParameterizedFunctions

my_ode = @ode_def begin
    dc̃₁ = Ω*im*c̃₂*exp( im*t*Δ)
    dc̃₂ = Ω*im*c̃₁*exp(-im*t*Δ)
end Ω Δ

u₀ = Complex{Float64}[1, 0]
tspan = (0.0, 10.0)
p = [1.0, 1.0]

prob = ODEProblem(my_ode, u₀, tspan, p)
sol = solve(prob)

```
