Skip to main content
Engineering LibreTexts

19.4: Implementing Euler’s Method

  • Page ID
    86328
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    As an example we’ll use Euler’s method to solve the equation from page , \[\frac{dy}{dt}(t) = a y(t) \notag \] with the initial condition \(y(0) = 5\) billion cells and the growth parameter \(a = 0.2\) per hour.

    As a first step, create a file named euler.m with a top-level function and a helper function:

    function res = euler()
        T(1) = 0;
        Y(1) = 5;
        r = rate_func(T(1), Y(1))
    end
    
    function res = rate_func(t, y)
       a = 0.2;
       dydt = a * y;
       res = dydt;
    end

    In euler we initialize the initial conditions and then call rate_func, so called because it computes the rate of growth in the population.

    After testing these functions, we can add code to euler to compute these difference equations: \[\begin{aligned} T_{i+1} &=& T_i + \Delta t \\ Y_{i+1} &=& Y_i + r \Delta t\end{aligned}\] where \(r\) is the rate of growth computed by rate_func. Listing 9.1 has the code we need:

    Listing 9.1: A function implementing Euler's method

    function res = euler()
        T(1) = 0;
        Y(1) = 5;
        dt = 0.1;
    
        for i=1:40
            r = rate_func(T(i), Y(i));
            T(i+1) = T(i) + dt;
            Y(i+1) = Y(i) + r * dt;
        end
        plot(T, Y)
    end

    Before the loop, we create two vectors, T and Y, and set the first element of each with the initial conditions; dt, which is the size of the time steps, is 0.1 hours.

    Inside the loop, we compute the growth rate based on the current time, T(i), and population, Y(i). You might notice that the rate depends only on population, but we pass time as an input variable anyway, for reasons you’ll see soon.

    After computing the growth rate, we add an element both T and Y. Then, when the loop exits, we plot Y as a function of T.

    If you run the code, you should get a plot of population over time, as shown in Figure 9.1.

    9.1.jpg
    Figure 9.1: Solution to a simple differential equation by Euler’s method

    As you can see, the population doubles in a little less than 4 hours.


    This page titled 19.4: Implementing Euler’s Method is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.