# SDE with diagonal noise

**URL:** <https://discourse.julialang.org/t/sde-with-diagonal-noise/50420>\
**Category:** General Usage\
**Tags:** sde\
**Created:** [November 19, 2020, 10:15am UTC](https://discourse.julialang.org/t/sde-with-diagonal-noise/50420 "2020-11-19T10:15:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [November 19, 2020, 10:15am UTC](https://discourse.julialang.org/t/sde-with-diagonal-noise/50420/1 "2020-11-19T10:15:24Z")

</div>

I am trying to solve the following SDE:

du=f(u,p,t)dt+\sigma dW,

where f(u,p,t) can be written as following:

```julia
function f(du,u,p,t)

#calculate the derivative of the vector at time j
p=0.0

for i=1:Nx
    if i==2
        du[i]=-(u[i+1]^2-0)/(4*Δx)-p*(u[i+1]-0)/(2*Δx)-(u[i+1]-2*u[i]+0)/(Δx^2)-(u[i]-0+6*u[i]-4*u[i+1]+u[i+2])/(Δx^4)
    end

    if i==3   
        du[i]=-(u[i+1]^2-u[i-1]^2)/(4*Δx)-p*(u[i+1]-u[i-1])/(2*Δx)-(u[i+1]-2*u[i]+u[i-1])/(Δx^2) -(0-4*u[i-1]+6*u[i]-4*u[i+1]+u[i+2])/(Δx^4)
    end

    if i>=4 && i<=Nx-3 # Inner scheme
        du[i]=-(u[i+1]^2-u[i-1]^2)/(4*Δx)-p*(u[i+1]-u[i-1])/(2*Δx)-(u[i+1]-2*u[i]+u[i-1])/(Δx^2) -(u[i-2]-4*u[i-1]+6*u[i]-4*u[i+1]+u[i+2])/(Δx^4)
    end

    if i==Nx-2 # u[i+2]=0
        du[i]=-(u[i+1]^2-u[i-1]^2)/(4*Δx)-p*(u[i+1]-u[i-1])/(2*Δx)-(u[i+1]-2*u[i]+u[i-1])/(Δx^2) -(u[i-2]-4*u[i-1]+6*u[i]-4*u[i+1]+0)/(Δx^4)
    end

    if i==Nx-1

        du[i]=-(0-u[i-1]^2)/(4*Δx)-p*(0-u[i-1])/(2*Δx)-(0-2*u[i]+u[i-1])/(Δx^2)-(u[i-2]-4*u[i-1]+6*u[i]-0+u[i])/(Δx^4)
    end

end

    return du
end

```

and \sigma=3.0. In order to do that, I wrote the following code:

```julia
p = [c] # parameter
Random.seed!(SEED)
u0 =vec(-0.5 * ones(N, 1) + (0.5 + 0.5) * rand(N, 1))
u0[1] = 0.0
u0[end] = 0.0
tspan = (0.0, Tspan)

###############################################################################

function g(du1,u,p,t)
    du1=ones(1,N).*3.0
    return du1
end

###############################################################################

prob_sde= SDEProblem(f,g, u0, tspan, p)
sol = solve(prob_sde,saveat=Δt)
U = convert(Array, sol)

```

Now I expect that every time I solve this problem without changing anything, I should get a different solution U due to the noise. However, I always get the same solution. Why is that?

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [November 19, 2020, 11:19am UTC](https://discourse.julialang.org/t/sde-with-diagonal-noise/50420/2 "2020-11-19T11:19:48Z")

</div>

Try

> [@AhmedAlreweny](#):
>
> `du1 .= 3.0`

(you are not modifying `du1`, so the solver doesn’t see your choice of dispersion coefficient)

---

<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 19, 2020, 1:12pm UTC](https://discourse.julialang.org/t/sde-with-diagonal-noise/50420/3 "2020-11-19T13:12:31Z")

</div>

Are you sure you want diagonal not scalar noise? By your definition it looks scalar. But for diagonal @mschauer’s response is the correct thing to do.

---

<div class="post-metadata">

**Author:** ![AhmedAlreweny](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@AhmedAlreweny](https://discourse.julialang.org/u/AhmedAlreweny)\
**Post date:** [November 19, 2020, 1:23pm UTC](https://discourse.julialang.org/t/sde-with-diagonal-noise/50420/4 "2020-11-19T13:23:27Z")

</div>

Thanks for your answer. I was doing something similar to [here](https://diffeq.sciml.ai/stable/tutorials/sde_example/#Example-2:-Systems-of-SDEs-with-Diagonal-Noise). I thought I could add the diagonal noise this way as with lorenz system. Did I misunderstood the example?

---

<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 19, 2020, 1:31pm UTC](https://discourse.julialang.org/t/sde-with-diagonal-noise/50420/5 "2020-11-19T13:31:53Z")

</div>

Yes, if you want diagonal noise it’s just `du . = 0.3` for the general sized form, because you need to respect mutation.
