# ODE solver giving wrong results

**URL:** https://discourse.julialang.org/t/ode-solver-giving-wrong-results/63891
**Category:** General Usage
**Tags:** diffeq
**Created:** [July 1, 2021, 2:04pm UTC](https://discourse.julialang.org/t/ode-solver-giving-wrong-results/63891 "2021-07-01T14:04:06Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![rodpiferreira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rodpiferreira/32/25747_2.png) [@rodpiferreira](https://discourse.julialang.org/u/rodpiferreira)
#### Post date: [July 1, 2021, 2:04pm UTC](https://discourse.julialang.org/t/ode-solver-giving-wrong-results/63891/1 "2021-07-01T14:04:06Z")

</div>

Hey everyone!  
I want to solve f(u,p,t) = An\*u + b, where:

An = [0.0+0.0im 1.0+0.0im  
5.0+0.0im -1.0+0.0im],  
b = [1.0+0.0im, 0.0+0.0im],  
x = [0.0+0.0im, 1.0+0.0im] (initial condition)

Which means that I have a system like this:  
x’ = y+1, x(0) = 0  
y’ = 5x-y, y(0) = 1

And I get the following ODE: x"(t)+x’(t)-5x(t)=1. The analytic solution gives me x(1) = 3.1259897764930082  
However, when I solve it via Julia’s ODE solver, I get x(1) = 4.21229.

Am I missing something? Here’s my code:

```julia
An = [0.0+0.0im 1.0+0.0im
	5.0+0.0im -1.0+0.0im]
b = [1.0+0.0im, 0.0+0.0im]
x = [0.0+0.0im, 1.0+0.0im]
tspan = (0.0,1.0)
f(u,p,t) = An*u + b;
prob = ODEProblem(f, x, tspan)
sol = solve(prob, Vern7(), dt = 0.1, adaptive = false, reltol=1e-8)

```

---

<div class="post-metadata">

### Author: ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)
#### Post date: [July 1, 2021, 2:38pm UTC](https://discourse.julialang.org/t/ode-solver-giving-wrong-results/63891/2 "2021-07-01T14:38:56Z")

</div>

Seems strange, when I simply copy and paste your code, i get

```julia
julia> sol = solve(prob, Vern7(), dt = 0.1, adaptive = false, reltol=1e-8);

julia> sol(1)
2-element Vector{ComplexF64}:
 3.125989776501887 + 0.000000000000000im
 5.058513100378909 + 0.000000000000000im

julia> 

```

which corresponds well enough to your analytical solution

---

<div class="post-metadata">

### Author: ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)
#### Post date: [July 1, 2021, 2:58pm UTC](https://discourse.julialang.org/t/ode-solver-giving-wrong-results/63891/3 "2021-07-01T14:58:57Z")

</div>

Another thing even though unrelated to your original problem: Setting `adaptive = false`, should basically invalidate your `reltol = 1e-8` option, since the ODE solver is only allowed to go in steps of 0.1 without adjusting the stepsize due to error. It should always be safer to use the default, which is `adaptive = true`.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [July 1, 2021, 3:07pm UTC](https://discourse.julialang.org/t/ode-solver-giving-wrong-results/63891/4 "2021-07-01T15:07:53Z")

</div>

The analytic solution to your equation is

x(t) = \frac{1-\sqrt{21}}{10}\exp{\left(-\frac{1+\sqrt{21}}{2}t\right)} + \frac{1+\sqrt{21}}{10}\exp{\left( \frac{\sqrt{21}-1}{2}t\right)} - \frac{1}{5}

which gives x(0) = 0 and x(1) \approx 3.1259....

Two comments:

- Why do you use complex numbers in `An`, `b`, and `x` (initial value)?
- The solution represents an unstable system, where numerical errors easily can “explode”.

---

<div class="post-metadata">

### Author: ![rodpiferreira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rodpiferreira/32/25747_2.png) [@rodpiferreira](https://discourse.julialang.org/u/rodpiferreira)
#### Post date: [July 3, 2021, 3:51am UTC](https://discourse.julialang.org/t/ode-solver-giving-wrong-results/63891/5 "2021-07-03T03:51:31Z")

</div>

Thank you for replying.  
Yes, I tried again later, and I got the same result this time. I guess that was a bug or something.

---

<div class="post-metadata">

### Author: ![rodpiferreira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rodpiferreira/32/25747_2.png) [@rodpiferreira](https://discourse.julialang.org/u/rodpiferreira)
#### Post date: [July 3, 2021, 3:56am UTC](https://discourse.julialang.org/t/ode-solver-giving-wrong-results/63891/6 "2021-07-03T03:56:29Z")

</div>

Thanks!  
I’m using complex numbers only because the other parts of my code require complex inputs.  
Also, yes - I’m mainly focusing on stable systems to get lower errors (which is not this case though).
