{ "hash": "05e6cb38cb6caf6b026021fdc4afc264", "result": { "markdown": "# Euler's method\n\n\n\nThis section uses these add-on packages:\n\n``` {.julia .cell-code}\nusing CalculusWithJulia\nusing Plots\nusing SymPy\nusing Roots\n```\n\n\n\n\n---\n\n\nThe following section takes up the task of numerically approximating solutions to differential equations. `Julia` has a huge set of state-of-the-art tools for this task starting with the [DifferentialEquations](https://github.com/SciML/DifferentialEquations.jl) package. We don't use that package in this section, focusing on simpler methods and implementations for pedagogical purposes, but any further exploration should utilize the tools provided therein. A brief introduction to the package follows in an upcoming [section](./differential_equations.html).\n\n\n---\n\nConsider the differential equation:\n\n\n\n$$\ny'(x) = y(x) \\cdot x, \\quad y(1)=1,\n$$\n\n\nwhich can be solved with `SymPy`:\n\n::: {.cell execution_count=4}\n``` {.julia .cell-code}\n@syms x, y, u()\nD = Differential(x)\nx0, y0 = 1, 1\nF(y,x) = y*x\n\ndsolve(D(u)(x) - F(u(x), x))\n```\n\n::: {.cell-output .cell-output-display execution_count=5}\n```{=html}\n \n\\[\nu{\\left(x \\right)} = C_{1} e^{\\frac{x^{2}}{2}}\n\\]\n\n```\n:::\n:::\n\n\nWith the given initial condition, the solution becomes:\n\n::: {.cell execution_count=5}\n``` {.julia .cell-code}\nout = dsolve(D(u)(x) - F(u(x),x), u(x), ics=Dict(u(x0) => y0))\n```\n\n::: {.cell-output .cell-output-display execution_count=6}\n```{=html}\n \n\\[\nu{\\left(x \\right)} = \\frac{e^{\\frac{x^{2}}{2}}}{e^{\\frac{1}{2}}}\n\\]\n\n```\n:::\n:::\n\n\nPlotting this solution over the slope field\n\n::: {.cell execution_count=6}\n``` {.julia .cell-code}\np = plot(legend=false)\nvectorfieldplot!((x,y) -> [1, F(x,y)], xlims=(0, 2.5), ylims=(0, 10))\nplot!(rhs(out), linewidth=5)\n```\n\n::: {.cell-output .cell-output-display execution_count=7}\n{}\n:::\n:::\n\n\nwe see that the vectors that are drawn seem to be tangent to the graph of the solution. This is no coincidence, the tangent lines to integral curves are in the direction of the slope field.\n\n\nWhat if the graph of the solution were not there, could we use this fact to *approximately* reconstruct the solution?\n\n\nThat is, if we stitched together pieces of the slope field, would we get a curve that was close to the actual answer?\n\n::: {.cell cache='true' hold='true' execution_count=7}\n\n::: {.cell-output .cell-output-display execution_count=8}\n```{=html}\n
Illustration of a function stitching together slope field lines to approximate the answer to an initial-value problem. The other function drawn is the actual solution.
\nThe race is on. An illustration of beads falling along a path, as can be seen, some paths are faster than others. The fastest path would follow a cycloid. See Bensky and Moelter for details on simulating a bead on a wire.
\n