# MethodError when solving SDE in DifferentialEquations.jl

**URL:** <https://discourse.julialang.org/t/methoderror-when-solving-sde-in-differentialequations-jl/49053>\
**Category:** New to Julia\
**Tags:** diffeq, sde\
**Created:** [October 26, 2020, 3:50pm UTC](https://discourse.julialang.org/t/methoderror-when-solving-sde-in-differentialequations-jl/49053 "2020-10-26T15:50:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sam_power/32/18972_2.png) [@Sam\_Power](https://discourse.julialang.org/u/Sam_Power)\
**Post date:** [October 26, 2020, 3:50pm UTC](https://discourse.julialang.org/t/methoderror-when-solving-sde-in-differentialequations-jl/49053/1 "2020-10-26T15:50:04Z")

</div>

Hi,

Apologies for not having a more informative title. I’m having a little trouble solving a 2-dimensional SDE in DifferentialEquations.jl. My current code is

```julia
using DifferentialEquations, ForwardDiff

# Solve Chemical Langevin Equation SDE
# dX = α(X) dt + √β(x) dW

S = [1 -1 0; 0 1 -1]

function h(x, c)
    return h = [c[1] * x[1]; c[2] * x[1] * x[2]; c[3] * x[2]]
end

function α(x, c)
    return dx = S * h(x, c)
end

function β(x, c)
    return σx = S * Diagonal(h(x, c)) * transpose(S)
end

function sqrtβ(x, c)
    return sqrt(β(x, c))
end

x0 = [70 ; 80]

tspan = (0.0, 50.0)

c = [1.0, 0.005, 0.6]

lotvol_CLE_SDE = SDEProblem(α, sqrtβ, x0, tspan, c)
CLE_sol = solve(lotvol_CLE_SDE)

```

which, upon running the final line, returns the error

```julia
> MethodError: no method matching α(::Array{Float64,1}, ::Array{Float64, 1}, ::Float64)

```

and I can’t totally make sense of this. Is the issue to do with e.g.

- how I’ve written the drift function
- how I’ve passed the drift function to the SDE problem
- how I’ve passed the parameters `c` to the problem

or something else entirely?

Separately, the SDE in question has a multiplicative, non-diagonal noise structure. I’m not sure whether this is causing the problem (the error I’m receiving doesn’t seem to suggest so), but I wanted to get a sanity check on whether I’ve written it sensibly. The examples [here](https://diffeq.sciml.ai/stable/tutorials/sde_example/#Example-4:-Systems-of-SDEs-with-Non-Diagonal-Noise) were useful, but are phrased in slightly different terms to what I’m used to.

---

<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 26, 2020, 4:45pm UTC](https://discourse.julialang.org/t/methoderror-when-solving-sde-in-differentialequations-jl/49053/2 "2020-10-26T16:45:57Z")

</div>

> [@Sam\_Power](#):
>
> ```julia
> function α(x, c)
> return dx = S * h(x, c)
> end
> 
> ```

there is no two argument syntax. See the examples in [https://diffeq.sciml.ai/stable/tutorials/sde\_example/](https://diffeq.sciml.ai/stable/tutorials/sde_example/)

---

<div class="post-metadata">

**Author:** ![Sam\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sam_power/32/18972_2.png) [@Sam\_Power](https://discourse.julialang.org/u/Sam_Power)\
**Post date:** [October 26, 2020, 5:17pm UTC](https://discourse.julialang.org/t/methoderror-when-solving-sde-in-differentialequations-jl/49053/3 "2020-10-26T17:17:55Z")

</div>

Ah, of course! Thank you. I have changed the functions to

```julia
function α(dx, x, c, t)
    return dx = S * h(x, c)
end

function β(x, c)
    return σx = S * Diagonal(h(x, c)) * transpose(S)
end

function sqrtβ(dx, x, c, t)
    return sqrt(β(x, c))
end

```

and the code runs now. I seem to be getting a slightly weird solution for the SDE itself (constant?), but I’ll try to work that out in my own time. Thanks!

---

<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 26, 2020, 5:31pm UTC](https://discourse.julialang.org/t/methoderror-when-solving-sde-in-differentialequations-jl/49053/4 "2020-10-26T17:31:05Z")

</div>

`dx .= S * h(x, c)` you’re creating new arrays, but you need to mutate like that.

---

<div class="post-metadata">

**Author:** ![Sam\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sam_power/32/18972_2.png) [@Sam\_Power](https://discourse.julialang.org/u/Sam_Power)\
**Post date:** [October 26, 2020, 5:35pm UTC](https://discourse.julialang.org/t/methoderror-when-solving-sde-in-differentialequations-jl/49053/5 "2020-10-26T17:35:14Z")

</div>

Thanks, I’m getting much more sensible outputs now.
