# SDE simulation error: ERROR: BoundsError: attempt to access 3-element Vector{Float64} at index \[1, 2\]

**URL:** <https://discourse.julialang.org/t/sde-simulation-error-error-boundserror-attempt-to-access-3-element-vector-float64-at-index-1-2/81045>\
**Category:** Modelling & Simulations\
**Created:** [May 13, 2022, 11:00pm UTC](https://discourse.julialang.org/t/sde-simulation-error-error-boundserror-attempt-to-access-3-element-vector-float64-at-index-1-2/81045 "2022-05-13T23:00:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [May 13, 2022, 11:00pm UTC](https://discourse.julialang.org/t/sde-simulation-error-error-boundserror-attempt-to-access-3-element-vector-float64-at-index-1-2/81045/1 "2022-05-13T23:00:43Z")

</div>

I am getting the following error trying to run a simulation  
ERROR: BoundsError: attempt to access 3-element Vector{Float64} at index [1, 2]

My code is as follows  
using DifferentialEquations  
using Plots; plotly()

```nohighlight
function β(t)
    0.2*sin(t/200)+0.5
end
function Γ(t)
    0.1*cos(t/200)+0.85
end
function Σ1(t)
    2+0.1*cos(t)
end
function Σ2(t)
    2+0.1*sin(t)
end
function φ12(t)
    0.1+0.001*cos(t)
end
function φ3(t)
    0.2+0.002*cos(t)
end
function χ(t)
    1.2+0.05*sin(t)
end

function f!(du,u,p,t)
    du[1] = -β(t)*u[1]*u[2]
    du[2] = β(t)*u[1]*u[2]-Γ(t)*u[2]
    du[3] = Γ(t)*u[2]
end

function g!(du,u,p,t)
    du[1,1] = -u[1]*u[2]
    du[1,2] = 0.0
    du[2,1] = u[1]*u[2]
    du[2,2] = u[2]*u[3] 
    du[3,1] = 0.0
    du[3,2] = -u[2]*u[3]
end

u0 = [0.75, 0.15, 0.1]
dt= 1/2^15
W = WienerProcess(0.0,0.0)

prob = SDEProblem(f!, g!, u0,(0.0,1.0), noise = W)
sol = solve(prob, EM(), dt=dt)

plot(sol)

```

This was previously working fine. If I change g! to

```julia
du[1] = ...
du[2] = ..
du[3] = .. 

```

It will run but that isn’t the correct form, I want non-diagonal noise.

EDIT1: If I add noise\_rate\_prototype with a value it still doesn’t work.  
EDIT2: I copied the example here [Stochastic Differential Equations · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/tutorials/sde_example/#Example-4:-Systems-of-SDEs-with-Non-Diagonal-Noise) And, I then altered it to look identical to what I want and it compiled no issue so I am very lost.

---

<div class="post-metadata">

**Author:** ![suavesito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suavesito/32/34386_2.png) [@suavesito](https://discourse.julialang.org/u/suavesito)\
**Post date:** [May 14, 2022, 1:40am UTC](https://discourse.julialang.org/t/sde-simulation-error-error-boundserror-attempt-to-access-3-element-vector-float64-at-index-1-2/81045/2 "2022-05-14T01:40:53Z")

</div>

Unrelated to the question, but maybe have a look at [this article about quoting code](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530).

---

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [May 14, 2022, 1:43am UTC](https://discourse.julialang.org/t/sde-simulation-error-error-boundserror-attempt-to-access-3-element-vector-float64-at-index-1-2/81045/3 "2022-05-14T01:43:08Z")

</div>

Thanks for pointing that out, I have edited.

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [May 14, 2022, 2:18am UTC](https://discourse.julialang.org/t/sde-simulation-error-error-boundserror-attempt-to-access-3-element-vector-float64-at-index-1-2/81045/4 "2022-05-14T02:18:48Z")

</div>

> [@Max\_Beez](#):
>
> ```julia
> prob = SDEProblem(f!, g!, u0,(0.0,1.0), noise = W)
> sol = solve(prob, EM(), dt=dt)
> 
> ```

You don’t need to explicitly construct the noise process as the default is to use Wiener processes. You can just do:

```julia
prob = SDEProblem(f!, g!, u0, (0.0,1.0); noise_rate_prototype=zeros(3,2))
sol = solve(prob, EM(), dt=dt)
plot(sol)

```

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [May 14, 2022, 2:21am UTC](https://discourse.julialang.org/t/sde-simulation-error-error-boundserror-attempt-to-access-3-element-vector-float64-at-index-1-2/81045/5 "2022-05-14T02:21:13Z")

</div>

And if you really want to explicitly construct the process even though it isn’t necessary I think this works:

```julia
# note the initial value is a vector of length two since you need a
# two-dimensional Brownian motion
W = WienerProcess(0.0, [0.0,0.0])   
prob = SDEProblem(f!, g!, u0, (0.0,1.0); noise = W, noise_rate_prototype=zeros(3,2))

```

---

<div class="post-metadata">

**Author:** ![Max\_Beez](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Beez](https://discourse.julialang.org/u/Max_Beez)\
**Post date:** [May 14, 2022, 2:22am UTC](https://discourse.julialang.org/t/sde-simulation-error-error-boundserror-attempt-to-access-3-element-vector-float64-at-index-1-2/81045/6 "2022-05-14T02:22:50Z")

</div>

Thank you! And I just checked, by placing

```julia
W = WienerProcess(0.0, [0.0,0.0]) 

```

it does work.

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [May 14, 2022, 2:30am UTC](https://discourse.julialang.org/t/sde-simulation-error-error-boundserror-attempt-to-access-3-element-vector-float64-at-index-1-2/81045/7 "2022-05-14T02:30:11Z")

</div>

Great, glad that works!
