Search code examples
rdifferential-equationsstochastic

How do I solve a SDE with two cases in R?


I want to solve the following stochastic differential equation with R:

\frac{dx}{dt}=f(x)+sigma*dW

f(x)= a+bx+cx^2 (for x \leq 1) f(x)= a+bx (for x > 1)

and

sigma=d^2

where (a, b, c, and d are constants).

I tried using:

f = expression(a+bx+cx^2)
s = expression(d^2)

solution <- sde.sim(X0=0.6, t0=0, N=2000, delta=0.01, drift = f, sigma = s )

But how do I include the second case (when x>1)?

Sorry for the poor inclusion of the mathematical expression. I do not how to write latex here.


Solution

  • Maybe something like this where (x <= 1) would evaluate to 0 or 1 depending on the case.

    f = expression(1+ 2 * x + (x <= 1) * 3*x^2)
    s = expression(2^2)
    
    solution <- sde.sim(X0=0.6, t0=0, N=2000, delta=0.01, drift = f, sigma = s)