# FAQ: solve diffrerential equation?

**URL:** <https://discourse.julialang.org/t/faq-solve-diffrerential-equation/49593>\
**Category:** General Usage\
**Tags:** question, diffeq\
**Created:** [November 4, 2020, 5:25pm UTC](https://discourse.julialang.org/t/faq-solve-diffrerential-equation/49593 "2020-11-04T17:25:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZhouZhuofei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhouzhuofei/32/18399_2.png) [@ZhouZhuofei](https://discourse.julialang.org/u/ZhouZhuofei)\
**Post date:** [November 4, 2020, 5:25pm UTC](https://discourse.julialang.org/t/faq-solve-diffrerential-equation/49593/1 "2020-11-04T17:25:45Z")

</div>

I have a function here:

\frac{dx}{dt} = kx(N-x) - lx(1-\alpha\frac{x}{N}) + \sigma k x \xi(t)

\xi(t) is Random perturbation term, \sigma is the variance of it,  
k,l,N, \alpha is a const.

```julia
using Plots
using DifferentialEquations
k, l, N, α = 0.02, 0.01, 1.0, 0.2
f(u,p,t)=k*u*(N-u) - l*u * (1 - α * u/N)

u₀ = 0.001
tspan = (0.0,100.0)
prob = ODEProblem(f,u₀,tspan)
res = solve(prob,Tsit5(), reltol=1e-8, abstol=1e-8)

plot(res)

```

how I add the \xi(t) in this function and solve?

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [November 4, 2020, 7:31pm UTC](https://discourse.julialang.org/t/faq-solve-diffrerential-equation/49593/2 "2020-11-04T19:31:03Z")

</div>

That’s actually a stochastic differential equation and not an ordinary differential equation. Take a look at [Stochastic Differential Equations · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/tutorials/sde_example/) for an example of how to solve them with DifferentialEquations.jl.

---

<div class="post-metadata">

**Author:** ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)\
**Post date:** [November 4, 2020, 7:34pm UTC](https://discourse.julialang.org/t/faq-solve-diffrerential-equation/49593/3 "2020-11-04T19:34:43Z")

</div>

As @dawbarton said, its an SDE–basically, you define a second function for the stochastic term and construct an `SDEProblem` instead of an `ODEProblem`. If I’m understanding your equation correctly it should look like this:

```julia
f(u, p, t) = k*u*(N-u) - l*u * (1 - α * u/N)
g(u, p, t) = k * σ * u

prob = SDEProblem(f, g, u₀, tspan)
res = solve(prob)

```

As a side note, your equations will solve faster if you [avoid global variables](https://docs.julialang.org/en/v1/manual/performance-tips/) by passing in your parameters as a named tuple:

```julia
params = (k=0.02, l=0.01, N=1.0, α=0.2, σ=1.0)
f(u, p, t) = p.k*u*(p.N-u) - p.l*u * (1 - p.α * u/p.N)
g(u, p, t) = p.k * p.σ * u

prob = SDEProblem(f, g, u₀, tspan, params)
@time res = solve(prob)

```

---

<div class="post-metadata">

**Author:** ![ZhouZhuofei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhouzhuofei/32/18399_2.png) [@ZhouZhuofei](https://discourse.julialang.org/u/ZhouZhuofei)\
**Post date:** [November 5, 2020, 5:31am UTC](https://discourse.julialang.org/t/faq-solve-diffrerential-equation/49593/4 "2020-11-05T05:31:35Z")

</div>

em, so `g(u, p, t) = k*σ*u` is `σkx`, dW = \frac{d(\xi(t))}{dt}, is that right?

if I want change the variance of (\xi(t)), how to do that?

---

<div class="post-metadata">

**Author:** ![zierenberg](https://avatars.discourse-cdn.com/v4/letter/z/278dde/32.png) [@zierenberg](https://discourse.julialang.org/u/zierenberg)\
**Post date:** [November 5, 2020, 7:44am UTC](https://discourse.julialang.org/t/faq-solve-diffrerential-equation/49593/5 "2020-11-05T07:44:58Z")

</div>

In DifferentialEquations.jl, the full equation is `du = f(u,p,t)dt + g(u,p,t)dW`, such that `g(u,p,t)` is the prefactor of the Wiener process `dW` as you correctly say. This prefactor directly sets the variance of the Wiener process, in your case this is \sigma k x according to your first equation. So if you say that for your problem \sigma is the variance of \xi than you should simply change \sigma to change the variance.

However, formally the Wiener process is not differentiable such that dW=\frac{d\xi}{dt} is not 100% correct but heuristically okay, see also the [wikipedia](https://en.wikipedia.org/wiki/Ornstein%E2%80%93Uhlenbeck_process) site for a nice explanation.

---

<div class="post-metadata">

**Author:** ![ZhouZhuofei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhouzhuofei/32/18399_2.png) [@ZhouZhuofei](https://discourse.julialang.org/u/ZhouZhuofei)\
**Post date:** [November 5, 2020, 7:54am UTC](https://discourse.julialang.org/t/faq-solve-diffrerential-equation/49593/6 "2020-11-05T07:54:51Z")

</div>

ok, thanks 😃
