# Can we solve complex valued two point boundary value problems?

**URL:** <https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232>\
**Category:** Numerics\
**Created:** [January 12, 2021, 4:25pm UTC](https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232 "2021-01-12T16:25:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![406700](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/406700/32/3705_2.png) [@406700](https://discourse.julialang.org/u/406700)\
**Post date:** [January 12, 2021, 4:25pm UTC](https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232/1 "2021-01-12T16:25:48Z")

</div>

Hi,

I am new to using Julia for differential equations. I am having a problem trying to solve a complex valued two point boundary value problem using the MIRK4 solver. I have tried to define complex valued initial conditions, and this throws an inexact error.

Does this algorithm work with complex functions? Are there alternatives?

Let me know if I should provide more information, and I can give details or post code.

Thanks! Michael

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [January 12, 2021, 5:30pm UTC](https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232/2 "2021-01-12T17:30:06Z")

</div>

It should work, do you have a minimum working example?

---

<div class="post-metadata">

**Author:** ![406700](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/406700/32/3705_2.png) [@406700](https://discourse.julialang.org/u/406700)\
**Post date:** [January 12, 2021, 5:53pm UTC](https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232/3 "2021-01-12T17:53:06Z")

</div>

````nohighlight
L = 1.0
tspan = (0.0,pi/2)
function simplependulum!(du,u,p,t)
    θ = u[1]
    dθ = u[2]
    du[1] = dθ
    du[2] = -(g/L)*sin(θ)
end

function bc2!(residual, u, p, t)
    residual[1] = u[1][1] + pi/2 
    residual[2] = u[end][1] - pi/2 
bvp2 = TwoPointBVProblem(simplependulum!, bc2!, [pi/2+1*im,pi/2+1*im], tspan)
sol2 = solve(bvp2, MIRK4(), dt=0.05)```
````

---

<div class="post-metadata">

**Author:** ![406700](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/406700/32/3705_2.png) [@406700](https://discourse.julialang.org/u/406700)\
**Post date:** [January 12, 2021, 5:56pm UTC](https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232/4 "2021-01-12T17:56:59Z")

</div>

This is the example given in the docs, where I have replaced the initial guess with an imaginary value. This is a nonsensical input for this example, but it shows the error. similarly, making one of the equations complex valued throws an inexact error.

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [January 12, 2021, 11:34pm UTC](https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232/5 "2021-01-12T23:34:53Z")

</div>

Definitively a bug in the BVPSystem constructor from BoundaryValueDiffEq.jl

It assumes same type for x and y  
[https://github.com/SciML/BoundaryValueDiffEq.jl/blob/fcb3f40e7cddf8aa1a22528f434ebe336c3ff554/src/collocation.jl](https://github.com/SciML/BoundaryValueDiffEq.jl/blob/fcb3f40e7cddf8aa1a22528f434ebe336c3ff554/src/collocation.jl)

Actually easy to fix:  
[Proposed fix](https://github.com/MatFi/BoundaryValueDiffEq.jl/tree/fix_y_type)

---

<div class="post-metadata">

**Author:** ![406700](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/406700/32/3705_2.png) [@406700](https://discourse.julialang.org/u/406700)\
**Post date:** [January 13, 2021, 9:10am UTC](https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232/6 "2021-01-13T09:10:30Z")

</div>

Thanks for the quick fix!

---

<div class="post-metadata">

**Author:** ![406700](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/406700/32/3705_2.png) [@406700](https://discourse.julialang.org/u/406700)\
**Post date:** [January 13, 2021, 9:48am UTC](https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232/7 "2021-01-13T09:48:57Z")

</div>

I will leave this open for the moment until all the changes are implemented.

---

<div class="post-metadata">

**Author:** ![406700](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/406700/32/3705_2.png) [@406700](https://discourse.julialang.org/u/406700)\
**Post date:** [January 13, 2021, 11:37am UTC](https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232/8 "2021-01-13T11:37:09Z")

</div>

It seems like a still get an error when using the fix\_y\_type branch. It is now “InexactError: Int64(NaN)”. Could you post a working example?

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [January 13, 2021, 1:56pm UTC](https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232/9 "2021-01-13T13:56:01Z")

</div>

Try:

```julia
using Pkg
Pkg.add(url="https://github.com/MatFi/BoundaryValueDiffEq.jl", rev="fix_y_type")
using BoundaryValueDiffEq
using DiffEqBase, OrdinaryDiffEq
L = 1.0
g = 9.81
tspan = (0.0,pi/2)
function simplependulum!(du,u,p,t)
    θ = u[1]
    dθ = u[2]
    du[1] = dθ
    du[2] = -(g/L)*sin(θ)
end

function bc2!(residual, u, p, t)
    residual[1] = u[1][1] + pi/2 
    residual[2] = u[end][1] - pi/2 
end
bvp2 = TwoPointBVProblem(simplependulum!, bc2!, [pi/2+1*im,pi/2+1*im], tspan)
sol2 = solve(bvp2, MIRK4(), dt=0.05)

```

The branch was broken for a few minutes, but now it is working again…

---

<div class="post-metadata">

**Author:** ![406700](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/406700/32/3705_2.png) [@406700](https://discourse.julialang.org/u/406700)\
**Post date:** [January 13, 2021, 3:49pm UTC](https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232/10 "2021-01-13T15:49:06Z")

</div>

Thanks! This example works. I’m still getting the error in my code, but I’ll try to see if it’s something on my end, as I can’t reproduce it.

---

<div class="post-metadata">

**Author:** ![406700](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/406700/32/3705_2.png) [@406700](https://discourse.julialang.org/u/406700)\
**Post date:** [January 13, 2021, 4:02pm UTC](https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232/11 "2021-01-13T16:02:28Z")

</div>

Solved. I got the error with u0 as a complex Int instead of complex float.

---

<div class="post-metadata">

**Author:** ![erny123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erny123/32/52255_2.png) [@erny123](https://discourse.julialang.org/u/erny123)\
**Post date:** [August 13, 2025, 4:23pm UTC](https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232/12 "2025-08-13T16:23:49Z")

</div>

This no longer works and gives the following error:

```julia-auto
TypeError: in Dual, in P, expected P<:Real, got Type{ComplexF64}

Stacktrace:
  [1] jacobian_eltype(x::Vector{ComplexF64}, ::SparseConnectivityTracer.TracerLocalSparsityDetector{SparseConnectivityTracer.GradientTracer{Int64, BitSet}, SparseConnectivityTracer.HessianTracer{Int64, BitSet, Dict{Int64, BitSet}, SparseConnectivityTracer.NotShared}})
    @ SparseConnectivityTracer ~/.julia/packages/SparseConnectivityTracer/ejsx1/src/adtypes_interface.jl:209
  [2] jacobian_buffer(x::Vector{ComplexF64}, detector::SparseConnectivityTracer.TracerLocalSparsityDetector{SparseConnectivityTracer.GradientTracer{Int64, BitSet}, SparseConnectivityTracer.HessianTracer{Int64, BitSet, Dict{Int64, BitSet}, SparseConnectivityTracer.NotShared}})
    @ SparseConnectivityTracer ~/.julia/packages/SparseConnectivityTracer/ejsx1/src/adtypes_interface.jl:231
  [3] 
....

```

Can we figure out what the issue is?

---

<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:** [August 13, 2025, 5:13pm UTC](https://discourse.julialang.org/t/can-we-solve-complex-valued-two-point-boundary-value-problems/53232/13 "2025-08-13T17:13:25Z")

</div>

@ErikQQY
