SDE with diagonal noise

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:

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:

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?

Try

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

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.

Thanks for your answer. I was doing something similar to here. I thought I could add the diagonal noise this way as with lorenz system. Did I misunderstood the example?

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