Notes on Ch. 2 of “Applied Stochastic Differential Equations”

Author

Sean L. Wu (slwood89@gmail.com)

Published

June 1, 2022

Text

An Euler method:

function euler(f, tspan, x0)
    # number of steps
    steps = length(tspan)

    # allocate space
    x = zeros(length(x0), steps)

    # initial state
    x[:,1] .= x0    

    # iterate
    for k in 2:steps

        # time discretization
        dt = tspan[k] - tspan[k-1]

        # step
        x[:,k] .= x[:,k-1] + f(x[:,k-1], tspan[k-1]) * dt

    end

    return x
end
euler (generic function with 1 method)

Exercises

2.9

Consider the IVP \(\dot{x} = -x\) with initial condition \(x(0)=1\).

a.

Solve the problem analytically. What is the exact value of \(x(1)\)?

This is easy to do with seperation of variables.

\[ \frac{dx}{dt} = -x \]

\[ \frac{dx}{x} = -dt \]

\[ \int_{0}^{t} \frac{1}{x} dx = -\int_{0}^{t} dt \]

\[ \log x(1) - \log x(0) = -t \]

\[ \log x(1) = \log x(0) - t \]

\[ x(1) = e^{\log x(0) - t} \]

\[ x(1) = e^{\log x(0)} e^{- t} \]

\[ x(1) = x(0)e^{-t} \]

b.

Implement the Euler method for this ODE. Using a step size of 1, es- timate \(x(1)\) numerically. Repeat this for step sizes \(10^n\), where \(n = 1, 2, 3, 4\)

Use the Euler method we coded earlier.

x0::Float64 = 1.0

f = (x,t) -> -x

x_exact = (x0,t) -> exp(-t)*x0
x_exact(x0,1)

euler(f, 0:1e0:1, x0)[:,end]
euler(f, 0:1e-1:1, x0)[:,end]
euler(f, 0:1e-2:1, x0)[:,end]
euler(f, 0:1e-3:1, x0)[:,end]
euler(f, 0:1e-4:1, x0)[:,end]
1-element Vector{Float64}:
 0.36786104643292855