# Single initial value problem for ODE: numerical integration forward and backward

**URL:** https://discourse.julialang.org/t/single-initial-value-problem-for-ode-numerical-integration-forward-and-backward/70547
**Category:** New to Julia
**Tags:** diffeq, ode
**Created:** [October 28, 2021, 12:32pm UTC](https://discourse.julialang.org/t/single-initial-value-problem-for-ode-numerical-integration-forward-and-backward/70547 "2021-10-28T12:32:01Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)
#### Post date: [October 28, 2021, 12:32pm UTC](https://discourse.julialang.org/t/single-initial-value-problem-for-ode-numerical-integration-forward-and-backward/70547/1 "2021-10-28T12:32:01Z")

</div>

Hi there,

I have an ordinary differential equation, such as (mine is much more complicated, besides being a 2-d system):

```julia
function dxdt!(du, u, p, t)
    x = u[1]
    du[1] = dx = 2*x^2
end

```

whose general analytical solution is, of course; x = 1/(c-2t), c = constant.  
The initial condition (corresponding to c=1), for instance,

```julia
u0 = [1.0]

```

is given at time t = 0.0, but I would like to find the corresponding numerical solution for a whole interval containing the initial time, i.e, both forward and backward, such as, e.g., in the interval (-5.0, 0.45). I am able to set two ODEProblem’s, with the same single initial value, with distinct timespans: (0.0, 0.498) and (0.0, -5.0), such as:

```julia
tspan1 = (0.0,0.498)
prob1 = ODEProblem(dxdt!, u0, tspan1)
sol1 = solve(prob1)
tspan2 = (0.0,0.498)
prob2 = ODEProblem(dxdt!, u0, tspan2)
sol2 = solve(prob1)

```

but then it is a bit awkward to deal with the union of these solutions in a convenient way. Is there a way to find the numerical solution, in a single shot, which comes from the past, passes through the initial condition point, and goes to the future?

Thanks in advance.

---

<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: [October 29, 2021, 2:23pm UTC](https://discourse.julialang.org/t/single-initial-value-problem-for-ode-numerical-integration-forward-and-backward/70547/2 "2021-10-29T14:23:33Z")

</div>

> [@mocalvao](#):
>
> but then it is a bit awkward to deal with the union of these solutions in a convenient way. Is there a way to find the numerical solution, in a single shot, which comes from the past, passes through the initial condition point, and goes to the future?

@Vaibhavdixit02 had code for stitching two solutions together you might want to start from.

---

<div class="post-metadata">

### Author: ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)
#### Post date: [October 29, 2021, 4:41pm UTC](https://discourse.julialang.org/t/single-initial-value-problem-for-ode-numerical-integration-forward-and-backward/70547/3 "2021-10-29T16:41:51Z")

</div>

Thanks Chris, I will try to look at it. At any rate, I think this might be a nice improvement to the package: generating a solution, from a given initial value problem, in both time directions, with all the current features (interpolation, plots, …) of the single direction problem; certainly, then, the user would have to provide the concrete time span or interval `(a, b)` for the integration (within which the initial time `t0` should be included: `a < t0 < b`).

---

<div class="post-metadata">

### Author: ![Vaibhavdixit02](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vaibhavdixit02/32/2916_2.png) [@Vaibhavdixit02](https://discourse.julialang.org/u/Vaibhavdixit02)
#### Post date: [October 30, 2021, 7:25am UTC](https://discourse.julialang.org/t/single-initial-value-problem-for-ode-numerical-integration-forward-and-backward/70547/4 "2021-10-30T07:25:47Z")

</div>

[https://github.com/SciML/DiffEqParamEstim.jl/blob/d812632db012913f7f026002db6680f7b314f05e/src/multiple\_shooting\_objective.jl#L58-L64](https://github.com/SciML/DiffEqParamEstim.jl/blob/d812632db012913f7f026002db6680f7b314f05e/src/multiple_shooting_objective.jl#L58-L64) this is the code that Chris mentioned, hope it helps!

---

<div class="post-metadata">

### Author: ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)
#### Post date: [October 30, 2021, 10:43am UTC](https://discourse.julialang.org/t/single-initial-value-problem-for-ode-numerical-integration-forward-and-backward/70547/5 "2021-10-30T10:43:17Z")

</div>

Thanks @Vaibhavdixit02
