# Combining ODE and SDE

**URL:** <https://discourse.julialang.org/t/combining-ode-and-sde/78674>\
**Category:** General Usage\
**Tags:** sde, differentialequation\
**Created:** [March 29, 2022, 11:42am UTC](https://discourse.julialang.org/t/combining-ode-and-sde/78674 "2022-03-29T11:42:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pitaevskii](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitaevskii/32/35020_2.png) [@Pitaevskii](https://discourse.julialang.org/u/Pitaevskii)\
**Post date:** [March 29, 2022, 11:42am UTC](https://discourse.julialang.org/t/combining-ode-and-sde/78674/1 "2022-03-29T11:42:52Z")

</div>

Hi everyone,

I’m working on a problem where I have a large set of differential equations. Noise is added to the system in two ways, by sampling the initial condition for many simulations and by explicitly adding noise in only one of the differential equations. It’s the second part that has me wondering if my implementation is correct.  
The deterministic part is

```julia
param=[0.0, Jm, Uv, γv, zeros(Float64,M), dWv]
function du_det!(du,u,p,t)
        Jm,Uv,γv,D=p
        mul!(D,Jm,u)
        @inbounds @. du = im*D - (im*Uv*abs2(u)+γv)*u
    end

```

Where `Jm`, `γv` and `Uv` are predefined arrays and `M` the size of the array `u`. For the stochastic part I take

```julia
function du_stoch!(du,u,p,t)
        du[:].=p[6][:]
    end

```

I want only the equation for `u[N]` to be stochastic so I usually have `dWv = zeros(Float64,M); dWv[N] = 1.0`. However when running this and pulling the noise

```julia
prob=SDEProblem(du_det!, du_stoch!, ψ0[1], (0.0,1.0), param)
sol=solve(prob,SOSRA(),dt=0.001,adaptive=true,save_noise=true,saveat=0.01)

```

I find that `sol.W` is nonzero for the other equations. Does anyone know how I can correctly implement my problem?  
Thanks in advance and apologies if the answer is trivial, my experience with Julia is limited.

---

<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:** [March 29, 2022, 2:04pm UTC](https://discourse.julialang.org/t/combining-ode-and-sde/78674/2 "2022-03-29T14:04:23Z")

</div>

> [@Pitaevskii](#):
>
> I find that `sol.W` is nonzero for the other equations. Does anyone know how I can correctly implement my problem?

Just make `du` in the `du_stoch!` be zero for the equations where you don’t want the Wiener process.

---

<div class="post-metadata">

**Author:** ![Pitaevskii](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitaevskii/32/35020_2.png) [@Pitaevskii](https://discourse.julialang.org/u/Pitaevskii)\
**Post date:** [March 29, 2022, 2:50pm UTC](https://discourse.julialang.org/t/combining-ode-and-sde/78674/3 "2022-03-29T14:50:29Z")

</div>

This is what I already do by taking `dWv = zeros(Float64,M); dWv[N]=1.0`. I went to check the noise thinking I would get zero for the equations where I put `du` equal to zero in `du_stoch`, but that isn’t the case.

Am I correct to assume that `sol.W` just gives the Wiener process for each equation, but that it does not necessarily mean that this noise also effects the corresponding equation (depending on whether I chose `du` zero or not)?

---

<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:** [March 29, 2022, 2:59pm UTC](https://discourse.julialang.org/t/combining-ode-and-sde/78674/4 "2022-03-29T14:59:27Z")

</div>

The noise is du .\* sol.W. The W is just a Brownian process.

---

<div class="post-metadata">

**Author:** ![Pitaevskii](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pitaevskii/32/35020_2.png) [@Pitaevskii](https://discourse.julialang.org/u/Pitaevskii)\
**Post date:** [March 29, 2022, 3:06pm UTC](https://discourse.julialang.org/t/combining-ode-and-sde/78674/5 "2022-03-29T15:06:30Z")

</div>

Ok, that clears some things up. Thanks for your response!
