use quarto, not Pluto to render pages

This commit is contained in:
jverzani committed 2022-07-24 16:38:24 -04:00
1 parent 93c993206a
commit 7b37ca828c
879 files changed
+793311 -2678

No files matched your search

@@ -0,0 +1,15 @@
{
"hash": "b57ebc77dce16d5151d357e6f524efb4",
"result": {
"markdown": "# Numeric derivatives\n\n\n\nThis section uses these add-on packages:\n\n``` {.julia .cell-code}\nusing CalculusWithJulia\nusing Plots\nusing ForwardDiff\nusing SymPy\nusing Roots\n```\n\n\n\n\n---\n\n\n`SymPy` returns symbolic derivatives. Up to choices of simplification, these answers match those that would be derived by hand. This is useful when comparing with known answers and for seeing the structure of the answer. However, there are times we just want to work with the answer numerically. For that we have other options within `Julia`. We discuss approximate derivatives and automatic derivatives. The latter will find wide usage in these notes.\n\n\n### Approximate derivatives\n\n\nBy approximating the limit of the secant line with a value for a small, but positive, $h$, we get an approximation to the derivative. That is\n\n\n\n$$\nf'(x) \\approx \\frac{f(x+h) - f(x)}{h}.\n$$\n\n\nThis is the forward-difference approximation. The central difference approximation looks both ways:\n\n\n\n$$\nf'(x) \\approx \\frac{f(x+h) - f(x-h)}{2h}.\n$$\n\n\nThough in general they are different, they are both approximations. The central difference is usually more accurate for the same size $h$. However, both are susceptible to round-off errors. The numerator is a subtraction of like-size numbers - a perfect opportunity to lose precision.\n\n\nAs such there is a balancing act:\n\n\n * if $h$ is too small the round-off errors are problematic,\n * if $h$ is too big, the approximation to the limit is not good.\n\n\nFor the forward difference $h$ values around $10^{-8}$ are typically good, for the central difference, values around $10^{-6}$ are typically good.\n\n\n##### Example\n\n\nLet's verify that the forward difference isn't too far off.\n\n::: {.cell execution_count=4}\n``` {.julia .cell-code}\nf(x) = exp(-x^2/2)\nc = 1\nh = 1e-8\nfapprox = (f(c+h) - f(c)) / h\n```\n\n::: {.cell-output .cell-output-display execution_count=5}\n```\n-0.6065306479285937\n```\n:::\n:::\n\n\nWe can compare to the actual with:\n\n::: {.cell execution_count=5}\n``` {.julia .cell-code}\n@syms x\ndf = diff(f(x), x)\nfactual = N(df(c))\nabs(factual - fapprox)\n```\n\n::: {.cell-output .cell-output-display execution_count=6}\n```\n1.178403974334123392219821170344191813548718695568289215873505651941374433299215e-08\n```\n:::\n:::\n\n\nThe error is about $1$ part in $100$ million.\n\n\nThe central difference is better here:\n\n::: {.cell hold='true' execution_count=6}\n``` {.julia .cell-code}\nh = 1e-6\ncdapprox = (f(c+h) - f(c-h)) / (2h)\nabs(factual - cdapprox)\n```\n\n::: {.cell-output .cell-output-display execution_count=7}\n```\n1.567568231457459370771602167922282298718695568289215873505651941374433299214873e-11\n```\n:::\n:::\n\n\n---\n\nThe [FiniteDifferences](https://github.com/JuliaDiff/FiniteDifferences.jl) and [FiniteDiff](https://github.com/JuliaDiff/FiniteDiff.jl) packages provide performant interfaces for differentiation based on finite differences.\n\n\n### Automatic derivatives\n\n\nThere are some other ways to compute derivatives numerically that give much more accuracy at the expense of slightly increased computing time. Automatic differentiation is the general name for a few different approaches. These approaches promise less complexity - in some cases - than symbolic derivatives and more accuracy than approximate derivatives; the accuracy is on the order of machine precision.\n\n\nThe `ForwardDiff` package provides one of [several](https://juliadiff.org/) ways for `Julia` to compute automatic derivatives. `ForwardDiff` is well suited for functions encountered in these notes, which depend on at most a few variables and output no more than a few values at once.\n\n\nThe `ForwardDiff` package was loaded in this section; in general its features are available when the `CalculusWithJulia` package is loaded, as that package provides a more convenient interface. The `derivative` function is not exported by `FiniteDiff`, so its usage requires qualification. To illustrate, to find the derivative of $f(x)$ at a *point* we have this syntax:\n\n::: {.cell execution_count=7}\n``` {.julia .cell-code}\nForwardDiff.derivative(f, c) # derivative is qualified by a module name\n```\n\n::: {.cell-output .cell-output-display execution_count=8}\n```\n-0.6065306597126334\n```\n:::\n:::\n\n\nThe `CalculusWithJulia` package defines an operator `D` which goes from finding a derivative at a point with `ForwardDiff.derivative` to defining a function which evaluates the derivative at each point. It is defined along the lines of `D(f) = x -> ForwardDiff.derivative(f,x)` in parallel to how the derivative operation for a function is defined mathematically from the definition for its value at a point.\n\n\nHere we see the error in estimating $f'(1)$:\n\n::: {.cell execution_count=8}\n``` {.julia .cell-code}\nfauto = D(f)(c) # D(f) is a function, D(f)(c) is the function called on c\nabs(factual - fauto)\n```\n\n::: {.cell-output .celLine truncated
"supporting": [
"numeric_derivatives_files/figure-html"
],
"filters": [],
"includes": {
"include-in-header": [
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js\" integrity=\"sha512-c3Nl8+7g4LMSTdrm621y7kf9v3SDPnhxLNhcjFJbKECVnmZHTdo+IRO05sNLTH/D3vA6u1X32ehoLC7WFVdheg==\" crossorigin=\"anonymous\"></script>\n<script src=\"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js\" integrity=\"sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==\" crossorigin=\"anonymous\"></script>\n<script type=\"application/javascript\">define('jquery', [],function() {return window.jQuery;})</script>\n"
]
}
}
}