CalculusWithJuliaNotes.jl/quarto/0e7f54ed/differentiable_vector_calculus/scalar_functions_applications.html
2022-08-11 13:15:19 -04:00

3675 lines
307 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.0.32">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Calculus with Julia - 56&nbsp; Applications with scalar functions</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script src="../site_libs/quarto-nav/quarto-nav.js"></script>
<script src="../site_libs/quarto-nav/headroom.min.js"></script>
<script src="../site_libs/clipboard/clipboard.min.js"></script>
<script src="../site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="../site_libs/quarto-search/fuse.min.js"></script>
<script src="../site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="../">
<link href="../differentiable_vector_calculus/vector_fields.html" rel="next">
<link href="../differentiable_vector_calculus/scalar_functions.html" rel="prev">
<script src="../site_libs/quarto-html/quarto.js"></script>
<script src="../site_libs/quarto-html/popper.min.js"></script>
<script src="../site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="../site_libs/quarto-html/anchor.min.js"></script>
<link href="../site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="../site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="../site_libs/bootstrap/bootstrap.min.js"></script>
<link href="../site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="../site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script id="quarto-search-options" type="application/json">{
"location": "navbar",
"copy-button": false,
"collapse-after": 3,
"panel-placement": "end",
"type": "overlay",
"limit": 20,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
"search-copy-link-title": "Copy link to search",
"search-hide-matches-text": "Hide additional matches",
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit"
}
}</script>
<script async="" src="https://hypothes.is/embed.js"></script>
<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>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
<script type="application/javascript">define('jquery', [],function() {return window.jQuery;})</script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
</head>
<body class="nav-sidebar floating nav-fixed">
<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top">
<nav class="navbar navbar-expand-lg navbar-dark ">
<div class="navbar-container container-fluid">
<a class="navbar-brand" href="../index.html">
<img src="../logo.png" alt="">
<span class="navbar-title">Calculus with Julia</span>
</a>
<div id="quarto-search" class="" title="Search"></div>
</div> <!-- /container-fluid -->
</nav>
<nav class="quarto-secondary-nav" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<div class="container-fluid d-flex justify-content-between">
<h1 class="quarto-secondary-nav-title"><span class="chapter-number">56</span>&nbsp; <span class="chapter-title">Applications with scalar functions</span></h1>
<button type="button" class="quarto-btn-toggle btn" aria-label="Show secondary navigation">
<i class="bi bi-chevron-right"></i>
</button>
</div>
</nav>
</header>
<!-- content -->
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
<!-- sidebar -->
<nav id="quarto-sidebar" class="sidebar collapse sidebar-navigation floating overflow-auto">
<div class="mt-2 flex-shrink-0 align-items-center">
<div class="sidebar-search">
<div id="quarto-search" class="" title="Search"></div>
</div>
</div>
<div class="sidebar-menu-container">
<ul class="list-unstyled mt-1">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../index.html" class="sidebar-item-text sidebar-link">Preface</a>
</div>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" aria-expanded="false">Precalculus Concepts</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-1" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/calculator.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">1</span>&nbsp; <span class="chapter-title">From calculator to computer</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/variables.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">2</span>&nbsp; <span class="chapter-title">Variables</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/numbers_types.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">Number systems</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/logical_expressions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">4</span>&nbsp; <span class="chapter-title">Inequalities, Logical expressions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/vectors.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">5</span>&nbsp; <span class="chapter-title">Vectors</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/ranges.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">6</span>&nbsp; <span class="chapter-title">Ranges and Sets</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">7</span>&nbsp; <span class="chapter-title">Functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">8</span>&nbsp; <span class="chapter-title">The Graph of a Function</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/transformations.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">9</span>&nbsp; <span class="chapter-title">Function manipulations</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/inversefunctions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">10</span>&nbsp; <span class="chapter-title">The Inverse of a Function</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/polynomial.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">11</span>&nbsp; <span class="chapter-title">Polynomials</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/polynomial_roots.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">12</span>&nbsp; <span class="chapter-title">Roots of a polynomial</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/polynomials_package.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">13</span>&nbsp; <span class="chapter-title">The Polynomials package</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/rational_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">14</span>&nbsp; <span class="chapter-title">Rational functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/exp_log_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">15</span>&nbsp; <span class="chapter-title">Exponential and logarithmic functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/trig_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">16</span>&nbsp; <span class="chapter-title">Trigonometric functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/julia_overview.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">17</span>&nbsp; <span class="chapter-title">Overview of Julia commands</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-2" aria-expanded="false">Limits</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-2" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-2" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../limits/limits.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">18</span>&nbsp; <span class="chapter-title">Limits</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../limits/limits_extensions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">19</span>&nbsp; <span class="chapter-title">Limits, issues, extensions of the concept</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../limits/continuity.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">20</span>&nbsp; <span class="chapter-title">Continuity</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../limits/intermediate_value_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">21</span>&nbsp; <span class="chapter-title">Implications of continuity</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-3" aria-expanded="false">Derivatives</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-3" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-3" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">22</span>&nbsp; <span class="chapter-title">Derivatives</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/numeric_derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">23</span>&nbsp; <span class="chapter-title">Numeric derivatives</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/symbolic_derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">24</span>&nbsp; <span class="chapter-title">Symbolic derivatives</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/mean_value_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">25</span>&nbsp; <span class="chapter-title">The mean value theorem for differentiable functions.</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/optimization.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">26</span>&nbsp; <span class="chapter-title">Optimization</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/first_second_derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">27</span>&nbsp; <span class="chapter-title">The first and second derivatives</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/curve_sketching.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">28</span>&nbsp; <span class="chapter-title">Curve Sketching</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/linearization.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">29</span>&nbsp; <span class="chapter-title">Linearization</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/newtons_method.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">30</span>&nbsp; <span class="chapter-title">Newtons method</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/more_zeros.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">31</span>&nbsp; <span class="chapter-title">Derivative-free alternatives to Newtons method</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/lhospitals_rule.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">32</span>&nbsp; <span class="chapter-title">LHospitals Rule</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/implicit_differentiation.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">33</span>&nbsp; <span class="chapter-title">Implicit Differentiation</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/related_rates.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">34</span>&nbsp; <span class="chapter-title">Related rates</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/taylor_series_polynomials.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">35</span>&nbsp; <span class="chapter-title">Taylor Polynomials and other Approximating Polynomials</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-4" aria-expanded="false">Integrals</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-4" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-4" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/area.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">36</span>&nbsp; <span class="chapter-title">Area under a curve</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/ftc.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">37</span>&nbsp; <span class="chapter-title">Fundamental Theorem or Calculus</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/substitution.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">38</span>&nbsp; <span class="chapter-title">Substitution</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/integration_by_parts.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">39</span>&nbsp; <span class="chapter-title">Integration By Parts</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/partial_fractions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">40</span>&nbsp; <span class="chapter-title">Partial Fractions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/improper_integrals.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">41</span>&nbsp; <span class="chapter-title">Improper Integrals</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/mean_value_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">42</span>&nbsp; <span class="chapter-title">Mean value theorem for integrals</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/area_between_curves.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">43</span>&nbsp; <span class="chapter-title">Area between two curves</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/center_of_mass.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">44</span>&nbsp; <span class="chapter-title">Center of Mass</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/volumes_slice.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">45</span>&nbsp; <span class="chapter-title">Volumes by slicing</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/arc_length.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">46</span>&nbsp; <span class="chapter-title">Arc length</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/surface_area.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">47</span>&nbsp; <span class="chapter-title">Surface Area</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-5" aria-expanded="false">ODEs</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-5" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-5" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../ODEs/odes.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">48</span>&nbsp; <span class="chapter-title">ODEs</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../ODEs/euler.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">49</span>&nbsp; <span class="chapter-title">Eulers method</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../ODEs/solve.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">50</span>&nbsp; <span class="chapter-title">The problem-algorithm-solve interface</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../ODEs/differential_equations.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">51</span>&nbsp; <span class="chapter-title">The <code>DifferentialEquations</code> suite</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-6" aria-expanded="true">Differential vector calculus</a>
<a class="sidebar-item-toggle text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-6" aria-expanded="true">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-6" class="collapse list-unstyled sidebar-section depth1 show">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/polar_coordinates.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">52</span>&nbsp; <span class="chapter-title">Polar Coordinates and Curves</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/vectors.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">53</span>&nbsp; <span class="chapter-title">Vectors and matrices</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/vector_valued_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">54</span>&nbsp; <span class="chapter-title">Vector-valued functions, <span class="math inline">\(f:R \rightarrow R^n\)</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/scalar_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">55</span>&nbsp; <span class="chapter-title">Scalar functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/scalar_functions_applications.html" class="sidebar-item-text sidebar-link active"><span class="chapter-number">56</span>&nbsp; <span class="chapter-title">Applications with scalar functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/vector_fields.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">57</span>&nbsp; <span class="chapter-title">Functions <span class="math inline">\(R^n \rightarrow R^m\)</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/plots_plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">58</span>&nbsp; <span class="chapter-title">2D and 3D plots in Julia with Plots</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-7" aria-expanded="false">Integral vector calculus</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-7" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-7" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/double_triple_integrals.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">59</span>&nbsp; <span class="chapter-title">Multi-dimensional integrals</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/line_integrals.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">60</span>&nbsp; <span class="chapter-title">Line and Surface Integrals</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/div_grad_curl.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">61</span>&nbsp; <span class="chapter-title">The Gradient, Divergence, and Curl</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/stokes_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">62</span>&nbsp; <span class="chapter-title">Greens Theorem, Stokes Theorem, and the Divergence Theorem</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/review.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">63</span>&nbsp; <span class="chapter-title">Quick Review of Vector Calculus</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-8" aria-expanded="false">Alternatives</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-8" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-8" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../alternatives/plotly_plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">64</span>&nbsp; <span class="chapter-title">JavaScript based plotting libraries</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../alternatives/makie_plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">65</span>&nbsp; <span class="chapter-title">Calculus plots with Makie</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-9" aria-expanded="false">Appendices</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-9" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-9" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/getting_started_with_julia.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">66</span>&nbsp; <span class="chapter-title">Getting started with Julia</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/julia_interfaces.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">67</span>&nbsp; <span class="chapter-title">Julia interfaces</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/calculus_with_julia.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">68</span>&nbsp; <span class="chapter-title">The <code>CalculusWithJulia</code> package</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/unicode.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">69</span>&nbsp; <span class="chapter-title">Usages of Unicode symbols</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/quick_notes.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">70</span>&nbsp; <span class="chapter-title">Quick introduction to Calculus with Julia</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../references.html" class="sidebar-item-text sidebar-link">References</a>
</div>
</li>
</ul>
</div>
</nav>
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#tangent-planes-linearization" id="toc-tangent-planes-linearization" class="nav-link active" data-scroll-target="#tangent-planes-linearization"> <span class="header-section-number">56.1</span> Tangent planes, linearization</a>
<ul class="collapse">
<li><a href="#equation-of-the-tangent-plane" id="toc-equation-of-the-tangent-plane" class="nav-link" data-scroll-target="#equation-of-the-tangent-plane"> <span class="header-section-number">56.1.1</span> Equation of the tangent plane</a></li>
<li><a href="#tangent-plane-for-level-curves" id="toc-tangent-plane-for-level-curves" class="nav-link" data-scroll-target="#tangent-plane-for-level-curves"> <span class="header-section-number">56.1.2</span> Tangent plane for level curves</a></li>
</ul></li>
<li><a href="#linearization" id="toc-linearization" class="nav-link" data-scroll-target="#linearization"> <span class="header-section-number">56.2</span> Linearization</a>
<ul class="collapse">
<li><a href="#newtons-method-to-solve-fxy-0-and-gxy0." id="toc-newtons-method-to-solve-fxy-0-and-gxy0." class="nav-link" data-scroll-target="#newtons-method-to-solve-fxy-0-and-gxy0."> <span class="header-section-number">56.2.1</span> Newtons method to solve <span class="math inline">\(f(x,y) = 0\)</span> and <span class="math inline">\(g(x,y)=0\)</span>.</a></li>
</ul></li>
<li><a href="#implicit-differentiation" id="toc-implicit-differentiation" class="nav-link" data-scroll-target="#implicit-differentiation"> <span class="header-section-number">56.3</span> Implicit differentiation</a></li>
<li><a href="#optimization" id="toc-optimization" class="nav-link" data-scroll-target="#optimization"> <span class="header-section-number">56.4</span> Optimization</a>
<ul class="collapse">
<li><a href="#gradient-descent" id="toc-gradient-descent" class="nav-link" data-scroll-target="#gradient-descent"> <span class="header-section-number">56.4.1</span> Gradient descent</a></li>
<li><a href="#newtons-method-for-minimization" id="toc-newtons-method-for-minimization" class="nav-link" data-scroll-target="#newtons-method-for-minimization"> <span class="header-section-number">56.4.2</span> Newtons method for minimization</a></li>
</ul></li>
<li><a href="#constrained-optimization-lagrange-multipliers" id="toc-constrained-optimization-lagrange-multipliers" class="nav-link" data-scroll-target="#constrained-optimization-lagrange-multipliers"> <span class="header-section-number">56.5</span> Constrained optimization, Lagrange multipliers</a></li>
<li><a href="#taylors-theorem" id="toc-taylors-theorem" class="nav-link" data-scroll-target="#taylors-theorem"> <span class="header-section-number">56.6</span> Taylors theorem</a></li>
<li><a href="#questions" id="toc-questions" class="nav-link" data-scroll-target="#questions"> <span class="header-section-number">56.7</span> Questions</a></li>
</ul>
<div class="toc-actions"><div><i class="bi bi-github"></i></div><div class="action-links"><p><a href="https://github.com/jverzani/CalculusWithJuliaNotes.jl/edit/main/quarto/differentiable_vector_calculus/scalar_functions_applications.qmd" class="toc-action">Edit this page</a></p><p><a href="https://github.com/jverzani/CalculusWithJuliaNotes.jl/issues/new" class="toc-action">Report an issue</a></p></div></div></nav>
</div>
<!-- main -->
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title d-none d-lg-block"><span class="chapter-number">56</span>&nbsp; <span class="chapter-title">Applications with scalar functions</span></h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<p>This section uses these add-on packages:</p>
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">using</span> <span class="bu">CalculusWithJulia</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="im">using</span> <span class="bu">Plots</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="im">using</span> <span class="bu">SymPy</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="im">using</span> <span class="bu">Roots</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>And the following from the <code>Contour</code> package:</p>
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> <span class="bu">Contour</span>: contours, levels, level, lines, coordinates</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>This section presents different applications of scalar functions.</p>
<section id="tangent-planes-linearization" class="level2" data-number="56.1">
<h2 data-number="56.1" class="anchored" data-anchor-id="tangent-planes-linearization"><span class="header-section-number">56.1</span> Tangent planes, linearization</h2>
<p>Consider the case <span class="math inline">\(f:R^2 \rightarrow R\)</span>. We visualize <span class="math inline">\(z=f(x,y)\)</span> through a surface. At a point <span class="math inline">\((a, b)\)</span>, this surface, if <span class="math inline">\(f\)</span> is sufficiently smooth, can be approximated by a flat area, or a plane. For example, the Northern hemisphere of the earth, might be modeled simplistically by <span class="math inline">\(z = \sqrt{R^2 - (x^2 + y^2)}\)</span> for some <span class="math inline">\(R\)</span> and with the origin at the earths core. The ancient view of a “flat earth,” can be more generously seen as identifying this tangent plane with the sphere. More apt for current times, is the use of GPS coordinates to describe location. The difference between any two coordinates is technically a distance on a curved, nearly spherical, surface. But if the two points are reasonably closes (miles, not tens of miles) and accuracy isnt of utmost importance (i.e., not used for self-driving cars), then the distance can be found from the Euclidean distance formula, <span class="math inline">\(\sqrt{(\Delta\text{latitude})^2 + \Delta\text{longitude})^2}\)</span>. That is, as if the points were on a plane, not a curved surface.</p>
<p>For the univariate case, the tangent line has many different uses. Here we see the tangent plane also does.</p>
<section id="equation-of-the-tangent-plane" class="level3" data-number="56.1.1">
<h3 data-number="56.1.1" class="anchored" data-anchor-id="equation-of-the-tangent-plane"><span class="header-section-number">56.1.1</span> Equation of the tangent plane</h3>
<p>The partial derivatives have the geometric view of being the derivative of the univariate functions <span class="math inline">\(f(\vec\gamma_x(t))\)</span> and <span class="math inline">\(f(\vec\gamma_y(t))\)</span>, where <span class="math inline">\(\vec\gamma_x\)</span> moves just parallel to the <span class="math inline">\(x\)</span> axis (e.g.&nbsp;<span class="math inline">\(\langle t + a, b\rangle\)</span>). and <span class="math inline">\(\vec\gamma_y\)</span> moves just parallel to the <span class="math inline">\(y\)</span> axis. The partial derivatives then are slopes of tangent lines to each curve. The tangent plane, should it exist, should match both slopes at a given point. With this observation, we can identify it.</p>
<p>Consider <span class="math inline">\(f(\vec\gamma_x)\)</span> at a point <span class="math inline">\((a,b)\)</span>. The path has a tangent vector, which has “slope” <span class="math inline">\(\frac{\partial f}{\partial x}\)</span>. and in the direction of the <span class="math inline">\(x\)</span> axis, but not the <span class="math inline">\(y\)</span> axis, as does this vector: <span class="math inline">\(\langle 1, 0, \frac{\partial f}{\partial x} \rangle\)</span>. Similarly, this vector <span class="math inline">\(\langle 0, 1, \frac{\partial f}{\partial y} \rangle\)</span> describes the tangent line to <span class="math inline">\(f(\vec\gamma_y)\)</span> a the point.</p>
<p>These two vectors will lie in the plane. The normal vector is found by their cross product:</p>
<div class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> f_x f_y</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>n <span class="op">=</span> [<span class="fl">1</span>, <span class="fl">0</span>, f_x] <span class="op">×</span> [<span class="fl">0</span>, <span class="fl">1</span>, f_y]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="6">
<pre><code>3-element Vector{Sym}:
-fₓ
-f_y
1</code></pre>
</div>
</div>
<p>Let <span class="math inline">\(\vec{x} = \langle a, b, f(a,b)\)</span>. The tangent plane at <span class="math inline">\(\vec{x}\)</span> then is described by all vectors <span class="math inline">\(\vec{v}\)</span> with <span class="math inline">\(\vec{n}\cdot(\vec{v} - \vec{x}) = 0\)</span>. Using <span class="math inline">\(\vec{v} = \langle x,y,z\rangle\)</span>, we have:</p>
<p><span class="math display">\[
[-\frac{\partial f}{\partial x}, -\frac{\partial f}{\partial y}, 1] \cdot [x-a, y-b, z - f(a,b)] = 0,
\]</span></p>
<p>or,</p>
<p><span class="math display">\[
z = f(a,b) + \frac{\partial f}{\partial x} (x-a) + \frac{\partial f}{\partial y} (y-b),
\]</span></p>
<p>which is more compactly expressed as</p>
<p><span class="math display">\[
z = f(a,b) + \nabla(f) \cdot \langle x-a, y-b \rangle.
\]</span></p>
<p>This form would then generalize to scalar functions from <span class="math inline">\(R^n \rightarrow R\)</span>. This is consistent with the definition of <span class="math inline">\(f\)</span> being differentiable, where <span class="math inline">\(\nabla{f}\)</span> plays the role of the slope in the formulas.</p>
<p>The following figure illustrates the above for the function <span class="math inline">\(f(x,y) = 6 - x^2 - y^2\)</span>:</p>
<div class="cell" data-hold="true" data-execution_count="6">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y) <span class="op">=</span> <span class="fl">6</span> <span class="op">-</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">-</span>y<span class="op">^</span><span class="fl">2</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x)<span class="op">=</span> <span class="fu">f</span>(x<span class="op">...</span>)</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>a,b <span class="op">=</span> <span class="fl">1</span>, <span class="op">-</span><span class="fl">1</span><span class="op">/</span><span class="fl">2</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a><span class="co"># draw surface</span></span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a>xr <span class="op">=</span> <span class="fl">7</span><span class="op">/</span><span class="fl">4</span></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span>xr, xr, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a><span class="fu">surface</span>(xs, ys, f, legend<span class="op">=</span><span class="cn">false</span>)</span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a><span class="co"># visualize tangent plane as 3d polygon</span></span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a>pt <span class="op">=</span> [a,b]</span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a><span class="fu">tplane</span>(x) <span class="op">=</span> <span class="fu">f</span>(pt) <span class="op">+</span> <span class="fu">gradient</span>(f)(pt) <span class="op"></span> (x <span class="op">-</span> [a,b])</span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a>pts <span class="op">=</span> [[a<span class="op">-</span><span class="fl">1</span>,b<span class="op">-</span><span class="fl">1</span>], [a<span class="op">+</span><span class="fl">1</span>, b<span class="op">-</span><span class="fl">1</span>], [a<span class="op">+</span><span class="fl">1</span>, b<span class="op">+</span><span class="fl">1</span>], [a<span class="op">-</span><span class="fl">1</span>, b<span class="op">+</span><span class="fl">1</span>], [a<span class="op">-</span><span class="fl">1</span>, b<span class="op">-</span><span class="fl">1</span>]]</span>
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a><span class="fu">plot!</span>(<span class="fu">unzip</span>([[pt<span class="op">...</span>, <span class="fu">tplane</span>(pt)] for pt <span class="kw">in</span> pts])<span class="op">...</span>)</span>
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-19"><a href="#cb5-19" aria-hidden="true" tabindex="-1"></a><span class="co"># plot paths in x and y direction through (a,b)</span></span>
<span id="cb5-20"><a href="#cb5-20" aria-hidden="true" tabindex="-1"></a><span class="fu">γ_x</span>(t) <span class="op">=</span> pt <span class="op">+</span> t<span class="op">*</span>[<span class="fl">1</span>,<span class="fl">0</span>]</span>
<span id="cb5-21"><a href="#cb5-21" aria-hidden="true" tabindex="-1"></a><span class="fu">γ_y</span>(t) <span class="op">=</span> pt <span class="op">+</span> t<span class="op">*</span>[<span class="fl">0</span>,<span class="fl">1</span>]</span>
<span id="cb5-22"><a href="#cb5-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-23"><a href="#cb5-23" aria-hidden="true" tabindex="-1"></a><span class="fu">plot_parametric!</span>((<span class="op">-</span>xr<span class="op">-</span>a)<span class="op">..</span>(xr<span class="op">-</span>a), t <span class="op">-&gt;</span> [<span class="fu">γ_x</span>(t)<span class="op">...</span>, (f<span class="op"></span>γ_x)(t)], linewidth<span class="op">=</span><span class="fl">3</span>)</span>
<span id="cb5-24"><a href="#cb5-24" aria-hidden="true" tabindex="-1"></a><span class="fu">plot_parametric!</span>((<span class="op">-</span>xr<span class="op">-</span>b)<span class="op">..</span>(xr<span class="op">-</span>b), t <span class="op">-&gt;</span> [<span class="fu">γ_y</span>(t)<span class="op">...</span>, (f<span class="op"></span>γ_y)(t)], linewidth<span class="op">=</span><span class="fl">3</span>)</span>
<span id="cb5-25"><a href="#cb5-25" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-26"><a href="#cb5-26" aria-hidden="true" tabindex="-1"></a><span class="co"># draw directional derivatives in 3d and normal</span></span>
<span id="cb5-27"><a href="#cb5-27" aria-hidden="true" tabindex="-1"></a>pt <span class="op">=</span> [a, b, <span class="fu">f</span>(a,b)]</span>
<span id="cb5-28"><a href="#cb5-28" aria-hidden="true" tabindex="-1"></a>fx, fy <span class="op">=</span> <span class="fu">gradient</span>(f)(a,b)</span>
<span id="cb5-29"><a href="#cb5-29" aria-hidden="true" tabindex="-1"></a><span class="fu">arrow!</span>(pt, [<span class="fl">1</span>, <span class="fl">0</span>, fx], linewidth<span class="op">=</span><span class="fl">3</span>)</span>
<span id="cb5-30"><a href="#cb5-30" aria-hidden="true" tabindex="-1"></a><span class="fu">arrow!</span>(pt, [<span class="fl">0</span>, <span class="fl">1</span>, fy], linewidth<span class="op">=</span><span class="fl">3</span>)</span>
<span id="cb5-31"><a href="#cb5-31" aria-hidden="true" tabindex="-1"></a><span class="fu">arrow!</span>(pt, [<span class="op">-</span>fx, <span class="op">-</span>fy, <span class="fl">1</span>], linewidth<span class="op">=</span><span class="fl">3</span>) <span class="co"># normal</span></span>
<span id="cb5-32"><a href="#cb5-32" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-33"><a href="#cb5-33" aria-hidden="true" tabindex="-1"></a><span class="co"># draw point in base, x-y, plane</span></span>
<span id="cb5-34"><a href="#cb5-34" aria-hidden="true" tabindex="-1"></a>pt <span class="op">=</span> [a, b, <span class="fl">0</span>]</span>
<span id="cb5-35"><a href="#cb5-35" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter!</span>(<span class="fu">unzip</span>([pt])<span class="op">...</span>)</span>
<span id="cb5-36"><a href="#cb5-36" aria-hidden="true" tabindex="-1"></a><span class="fu">arrow!</span>(pt, [<span class="fl">1</span>,<span class="fl">0</span>,<span class="fl">0</span>], linestyle<span class="op">=:</span>dash)</span>
<span id="cb5-37"><a href="#cb5-37" aria-hidden="true" tabindex="-1"></a><span class="fu">arrow!</span>(pt, [<span class="fl">0</span>,<span class="fl">1</span>,<span class="fl">0</span>], linestyle<span class="op">=:</span>dash)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="7">
<p><img src="scalar_functions_applications_files/figure-html/cell-7-output-1.svg" class="img-fluid"></p>
</div>
</div>
<section id="alternate-forms" class="level4">
<h4 class="anchored" data-anchor-id="alternate-forms">Alternate forms</h4>
<p>The equation for the tangent plane is often expressed in a more explicit form. For <span class="math inline">\(n=2\)</span>, if we set <span class="math inline">\(dx = x-a\)</span> and <span class="math inline">\(dy=y-a\)</span>, then the equation for the plane becomes:</p>
<p><span class="math display">\[
f(a,b) + \frac{\partial f}{\partial x} dx + \frac{\partial f}{\partial y} dy,
\]</span></p>
<p>which is a common form for the equation, though possibly confusing, as <span class="math inline">\(\partial x\)</span> and <span class="math inline">\(dx\)</span> need to be distinguished. For <span class="math inline">\(n &gt; 2\)</span>, additional terms follow this pattern. This explicit form is helpful when doing calculations by hand, but much less so when working on the computer, say with <code>Julia</code>, as the representations using vectors (or matrices) can be readily implemented and their representation much closer to the formulas. For example, consider these two possible functions to find the tangent plane (returned as a function) at a point in <span class="math inline">\(2\)</span> dimensions</p>
<div class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">tangent_plane_1st_crack</span>(f, pt)</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> fx, fy <span class="op">=</span> ForwardDiff.<span class="fu">gradient</span>(f, pt)</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> x <span class="op">-&gt;</span> <span class="fu">f</span>(x<span class="op">...</span>) <span class="op">+</span> fx <span class="op">*</span> (x[<span class="fl">1</span>]<span class="op">-</span>pt[<span class="fl">1</span>]) <span class="op">+</span> fy <span class="op">*</span> (x[<span class="fl">2</span>]<span class="op">-</span>pt[<span class="fl">2</span>])</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="8">
<pre><code>tangent_plane_1st_crack (generic function with 1 method)</code></pre>
</div>
</div>
<p>It isnt so bad, but as written, we specialized to the number of dimensions, used indexing, and with additional dimensions, it clearly would get tedious to generalize. Using vectors, we might have:</p>
<div class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">tangent_plane</span>(f, pt)</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a> ∇f <span class="op">=</span> ForwardDiff.<span class="fu">gradient</span>(f, pt) <span class="co"># using a variable ∇f</span></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> x <span class="op">-&gt;</span> <span class="fu">f</span>(pt) <span class="op">+</span> ∇f <span class="op"></span> (x <span class="op">-</span> pt)</span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="9">
<pre><code>tangent_plane (generic function with 1 method)</code></pre>
</div>
</div>
<p>This is much more like the compact formula and able to handle higher dimensions without rewriting.</p>
</section>
</section>
<section id="tangent-plane-for-level-curves" class="level3" data-number="56.1.2">
<h3 data-number="56.1.2" class="anchored" data-anchor-id="tangent-plane-for-level-curves"><span class="header-section-number">56.1.2</span> Tangent plane for level curves</h3>
<p>Consider the surface described by <span class="math inline">\(f(x,y,z) = c\)</span>, a constant. This is more general than surfaces described by <span class="math inline">\(z = f(x,y)\)</span>. The concept of a tangent plane should still be applicable though. Suppose, <span class="math inline">\(\vec{\gamma}(t)\)</span> is a curve in the <span class="math inline">\(x-y-z\)</span> plane, then we have <span class="math inline">\((f\circ\vec\gamma)(t)\)</span> is a curve on the surface and its derivative is given by the chain rule through: <span class="math inline">\(\nabla{f}(\vec\gamma(t))\cdot \vec\gamma'(t)\)</span>. But this composition is constantly the same value, so the derivative is <span class="math inline">\(0\)</span>. This says that <span class="math inline">\(\nabla{f}(\vec\gamma(t))\)</span> is <em>orthogonal</em> to <span class="math inline">\(\vec\gamma'(t)\)</span> for any curve. As these tangential vectors to <span class="math inline">\(\vec\gamma\)</span> lie in the tangent plane, the tangent plane can be characterized by having <span class="math inline">\(\nabla{f}\)</span> as the normal.</p>
<p>This computation was previously done in two dimensions, and showed the gradient is orthogonal to the contour lines (and points in the direction of greatest ascent). It can be generalized to higher dimensions.</p>
<p>The surface <span class="math inline">\(F(x,y,z) = z - f(x,y) = 0\)</span> has gradient given by <span class="math inline">\(\langle -\partial{f}/\partial{x}, -\partial{f}/\partial{y}, 1\rangle\)</span>, and as seen above, this vector is normal to the tangent plane, so this generalization agrees on the easier case.</p>
<p>For clarity:</p>
<ul>
<li>The scalar function <span class="math inline">\(z = f(x,y)\)</span> describes a surface, <span class="math inline">\((x,y,f(x,y))\)</span>; the gradient, <span class="math inline">\(\nabla{f}\)</span>, is <span class="math inline">\(2\)</span> dimensional and points in the direction of greatest ascent for the surface.</li>
<li>The scalar function <span class="math inline">\(f(x,y,z)\)</span> <em>also</em> describes a surface, through level curves <span class="math inline">\(f(x,y,z) = c\)</span>, for some <em>constant</em> <span class="math inline">\(c\)</span>. The gradient <span class="math inline">\(\nabla{f}\)</span> is <span class="math inline">\(3\)</span> dimensional and <em>orthogonal</em> to the surface.</li>
</ul>
<section id="example" class="level5">
<h5 class="anchored" data-anchor-id="example">Example</h5>
<p>Let <span class="math inline">\(z = f(x,y) = \sin(x)\cos(x-y)\)</span>. Find an equation for the tangent plane at <span class="math inline">\((\pi/4, \pi/3)\)</span>.</p>
<p>We have many possible forms to express this in, but we will use the functional description:</p>
<div class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x, y</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="10">
<pre><code>(x, y)</code></pre>
</div>
</div>
<div class="cell" data-hold="true" data-execution_count="10">
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y) <span class="op">=</span> <span class="fu">sin</span>(x) <span class="op">*</span> <span class="fu">cos</span>(x<span class="op">-</span>y)</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> <span class="fu">f</span>(x<span class="op">...</span>)</span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a>vars <span class="op">=</span> [x, y]</span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a>gradf <span class="op">=</span> <span class="fu">diff</span>.(<span class="fu">f</span>(x,y), vars) <span class="co"># or use gradient(f, vars) or ∇((f,vars))</span></span>
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a>pt <span class="op">=</span> [PI<span class="op">/</span><span class="fl">4</span>, PI<span class="op">/</span><span class="fl">3</span>]</span>
<span id="cb12-8"><a href="#cb12-8" aria-hidden="true" tabindex="-1"></a>gradfa <span class="op">=</span> <span class="fu">subs</span>.(gradf, x<span class="op">=&gt;</span>pt[<span class="fl">1</span>], y<span class="op">=&gt;</span>pt[<span class="fl">2</span>])</span>
<span id="cb12-9"><a href="#cb12-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-10"><a href="#cb12-10" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(pt) <span class="op">+</span> gradfa <span class="op"></span> (vars <span class="op">-</span> pt)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="11">
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
\[
\left(x - \frac{\pi}{4}\right) \left(- \frac{\sqrt{2} \left(- \frac{\sqrt{6}}{4} + \frac{\sqrt{2}}{4}\right)}{2} + \frac{\sqrt{2} \left(\frac{\sqrt{2}}{4} + \frac{\sqrt{6}}{4}\right)}{2}\right) + \frac{\sqrt{2} \left(- \frac{\sqrt{6}}{4} + \frac{\sqrt{2}}{4}\right) \left(y - \frac{\pi}{3}\right)}{2} + \frac{\sqrt{2} \left(\frac{\sqrt{2}}{4} + \frac{\sqrt{6}}{4}\right)}{2}
\]
</span>
</div>
</div>
</section>
<section id="example-1" class="level5">
<h5 class="anchored" data-anchor-id="example-1">Example</h5>
<p>A cylinder <span class="math inline">\(f(x,y,z) = (x-a)^2 + y^2 = (2a)^2\)</span> is intersected with a sphere <span class="math inline">\(g(x,y,z) = x^2 + y^2 + z^2 = a^2\)</span>. Let <span class="math inline">\(V\)</span> be the line of intersection. (Vivianis curve). Let <span class="math inline">\(P\)</span> be a point on the curve. Describe the tangent to the curve.</p>
<p>We have the line of intersection will have tangent line lying in the tangent plane to both surfaces. These two surfaces have normal vectors given by the gradient, or <span class="math inline">\(\vec{n}_1 = \langle 2(x-a), 2y, 0 \rangle\)</span> and <span class="math inline">\(\vec{n}_2 = \langle 2x, 2y, 2z \rangle\)</span>. The cross product of these two vectors will lie in both tangent planes, so we have:</p>
<p><span class="math display">\[
P + t (\vec{n}_1 \times \vec{n}_2),
\]</span></p>
<p>will describe the tangent.</p>
<p>The curve may be described parametrically by <span class="math inline">\(\vec\gamma(t) = a \langle 1 + \cos(t), \sin(t), 2\sin(t/2) \rangle\)</span>. Lets see that the above is correct by verifying that the cross product of the tangent vector computed two ways is <span class="math inline">\(0\)</span>:</p>
<div class="cell" data-hold="true" data-execution_count="11">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>a <span class="op">=</span> <span class="fl">1</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a><span class="fu">gamma</span>(t) <span class="op">=</span> a <span class="op">*</span> [<span class="fl">1</span> <span class="op">+</span> <span class="fu">cos</span>(t), <span class="fu">sin</span>(t), <span class="fl">2</span><span class="fu">sin</span>(t<span class="op">/</span><span class="fl">2</span>) ]</span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a>P <span class="op">=</span> <span class="fu">gamma</span>(<span class="fl">1</span><span class="op">/</span><span class="fl">2</span>)</span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a><span class="fu">n1</span>(x,y,z)<span class="op">=</span> [<span class="fl">2</span><span class="fu">*</span>(x<span class="op">-</span>a), <span class="fl">2</span>y, <span class="fl">0</span>]</span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a><span class="fu">n2</span>(x,y,z) <span class="op">=</span> [<span class="fl">2</span>x,<span class="fl">2</span>y,<span class="fl">2</span>z]</span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a><span class="fu">n1</span>(x) <span class="op">=</span> <span class="fu">n1</span>(x<span class="op">...</span>)</span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a><span class="fu">n2</span>(x) <span class="op">=</span> <span class="fu">n2</span>(x<span class="op">...</span>)</span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-9"><a href="#cb13-9" aria-hidden="true" tabindex="-1"></a>t <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">2</span></span>
<span id="cb13-10"><a href="#cb13-10" aria-hidden="true" tabindex="-1"></a>(<span class="fu">n1</span>(<span class="fu">gamma</span>(t)) <span class="op">×</span> <span class="fu">n2</span>(<span class="fu">gamma</span>(t))) <span class="op">×</span> gamma<span class="op">'</span>(t)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="12">
<pre><code>3-element Vector{Float64}:
0.0
0.0
0.0</code></pre>
</div>
</div>
</section>
<section id="plotting-level-curves-of-fxyz-c" class="level4">
<h4 class="anchored" data-anchor-id="plotting-level-curves-of-fxyz-c">Plotting level curves of <span class="math inline">\(F(x,y,z) = c\)</span></h4>
<p>The <code>wireframe</code> plot can be used to visualize a surface of the type <code>z=f(x,y)</code>, as previously illustrated. However we have no way of plotting <span class="math inline">\(3\)</span>-dimensional implicit surfaces (of the type <span class="math inline">\(F(x,y,z)=c\)</span>) as we do for <span class="math inline">\(2\)</span>-dimensional implicit surfaces with <code>Plots</code>. (The <code>MDBM</code> or <code>IntervalConstraintProgramming</code> packages can be used along with <code>Makie</code> plotting package to produce one.)</p>
<p>The <code>CalculusWithJulia</code> package provides a stop-gap function, <code>plot_implicit_surface</code> for this task. The basic idea is to slice an axis, by default the <span class="math inline">\(z\)</span> axis up and for each level plot the contours of <span class="math inline">\((x,y) \rightarrow f(x,y,z)-c\)</span>, which becomes a <span class="math inline">\(2\)</span>-dimensional problem. The function allows any of 3 different axes to be chosen to slice over, the default being just the <span class="math inline">\(z\)</span> axis.</p>
<p>We demonstrate with an example from a February 14, 2019 article in the <a href="https://www.nytimes.com/2019/02/14/science/math-algorithm-valentine.html">New York Times</a>. It shows an equation for a “heart,” as the graphic will illustrate:</p>
<div class="cell" data-hold="true" data-execution_count="12">
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>a, b <span class="op">=</span> <span class="fl">1</span>, <span class="fl">3</span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y,z) <span class="op">=</span> (x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> ((<span class="fl">1</span><span class="op">+</span>b) <span class="op">*</span> y)<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> z<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> <span class="fl">1</span>)<span class="op">^</span><span class="fl">3</span> <span class="op">-</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">*</span> z<span class="op">^</span><span class="fl">3</span> <span class="op">-</span> a <span class="op">*</span> y<span class="op">^</span><span class="fl">2</span> <span class="op">*</span> z<span class="op">^</span><span class="fl">3</span></span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a>CalculusWithJulia.<span class="fu">plot_implicit_surface</span>(f, xlim<span class="op">=-</span><span class="fl">2</span><span class="op">..</span><span class="fl">2</span>, ylim<span class="op">=-</span><span class="fl">1</span><span class="op">..</span><span class="fl">1</span>, zlim<span class="op">=-</span><span class="fl">1</span><span class="op">..</span><span class="fl">2</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="13">
<p><img src="scalar_functions_applications_files/figure-html/cell-13-output-1.svg" class="img-fluid"></p>
</div>
</div>
</section>
</section>
</section>
<section id="linearization" class="level2" data-number="56.2">
<h2 data-number="56.2" class="anchored" data-anchor-id="linearization"><span class="header-section-number">56.2</span> Linearization</h2>
<p>The tangent plane is the best “linear approximation” to a function at a point. “Linear” refers to mathematical properties of the tangent plane, but at a practical level it means easy to compute, as it will involve only multiplication and addition. “Approximation” is useful in that if a bit of error is an acceptable tradeoff for computational ease, the tangent plane may be used in place of the function. In the univariate case, this is known as linearization, and the tradeoff is widely used in the derivation of theoretical relationships, as well as in practice to get reasonable numeric values.</p>
<p>Formally, this is saying:</p>
<p><span class="math display">\[
f(\vec{x}) \approx f(\vec{a}) + ∇f(\vec{a}) ⋅ (\vec{x} - \vec{a}).
\]</span></p>
<p>The explicit meaning of <span class="math inline">\(\approx\)</span> will be made clear when the generalization of Taylors theorem is to be stated.</p>
<section id="example-linear-approximation" class="level5">
<h5 class="anchored" data-anchor-id="example-linear-approximation">Example: Linear approximation</h5>
<p>The volume of a cylinder is <span class="math inline">\(V=\pi r^2 h\)</span>. It is thought a cylinder has <span class="math inline">\(r=1\)</span> and <span class="math inline">\(h=2\)</span>. If instead, the amounts are <span class="math inline">\(r=1.01, h=2.01\)</span>, what is the difference in volume?</p>
<p>That is, if <span class="math inline">\(V(r,h) = \pi r^2 h\)</span>, what is <span class="math inline">\(V(1.01, 2.01) - V(1,2)\)</span>?</p>
<p>We can use linear approximation to see that this difference is <em>approximately</em> <span class="math inline">\(\nabla{V} \cdot \langle 0.01, 0.01 \rangle\)</span>. This is:</p>
<div class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="fu">V</span>(r, h) <span class="op">=</span> <span class="cn">pi</span> <span class="op">*</span> r<span class="op">^</span><span class="fl">2</span> <span class="op">*</span> h</span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a><span class="fu">V</span>(v) <span class="op">=</span> <span class="fu">V</span>(v<span class="op">...</span>)</span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a>a₁ <span class="op">=</span> [<span class="fl">1</span>,<span class="fl">2</span>]</span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a>dx₁ <span class="op">=</span> [<span class="fl">0.01</span>, <span class="fl">0.01</span>]</span>
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a>ForwardDiff.<span class="fu">gradient</span>(V, a₁) <span class="op"></span> dx₁ <span class="co"># or use ∇(V)(a)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="14">
<pre><code>0.15707963267948966</code></pre>
</div>
</div>
<p>The exact difference can be computed:</p>
<div class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">V</span>(a₁ <span class="op">+</span> dx₁) <span class="op">-</span> <span class="fu">V</span>(a₁)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="15">
<pre><code>0.15833941133357854</code></pre>
</div>
</div>
</section>
<section id="example-2" class="level5">
<h5 class="anchored" data-anchor-id="example-2">Example</h5>
<p>Let <span class="math inline">\(f(x,y) = \sin(\pi x y^2)\)</span>. Estimate <span class="math inline">\(f(1.1, 0.9)\)</span>.</p>
<p>Using linear approximation with <span class="math inline">\(dx=0.1\)</span> and <span class="math inline">\(dy=-0.1\)</span>, this is</p>
<p><span class="math display">\[
f(1,1) + \nabla{f}(1,1) \cdot \langle 0.1, -0.1\rangle,
\]</span></p>
<p>where <span class="math inline">\(f(1,1) = \sin(\pi) = 0\)</span> and <span class="math inline">\(\nabla{f} = \langle y^2\cos(\pi x y^2), \cos(\pi x y^2) 2y\rangle = \cos(\pi x y^2)\langle x,2y\rangle\)</span>. So, the answer is:</p>
<p><span class="math display">\[
0 + \cos(\pi) \langle 1,2\rangle\cdot \langle 0.1, -0.1 \rangle =
(-1)(0.1 - 2(0.1)) = 0.1.
\]</span></p>
</section>
<section id="example-3" class="level5">
<h5 class="anchored" data-anchor-id="example-3">Example</h5>
<p>A <a href="http://www.math.harvard.edu/~knill/teaching/summer2011/handouts/32-linearization.pdf">piriform</a> is described by the quartic surface <span class="math inline">\(f(x,y,z) = x^4 -x^3 + y^2+z^2 = 0\)</span>. Find the tangent line at the point <span class="math inline">\(\langle 2,2,2 \rangle\)</span>.</p>
<p>Here, <span class="math inline">\(\nabla{f}\)</span> describes a <em>normal</em> to the tangent plane. The description of a plane may be described by <span class="math inline">\(\hat{N}\cdot(\vec{x} - \vec{x}_0) = 0\)</span>, where <span class="math inline">\(\vec{x}_0\)</span> is identified with a point on the plane (the point <span class="math inline">\((2,2,2)\)</span> here). With this, we have <span class="math inline">\(\hat{N}\cdot\vec{x} = ax + by + cz = \hat{N}\cdot\langle 2,2,2\rangle = 2(a+b+c)\)</span>. For ths problem, <span class="math inline">\(\nabla{f}(2,2,2) = \langle a, b, c\rangle\)</span> is given by:</p>
<div class="cell" data-hold="true" data-execution_count="15">
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y,z) <span class="op">=</span> x<span class="op">^</span><span class="fl">4</span> <span class="op">-</span>x<span class="op">^</span><span class="fl">3</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> z<span class="op">^</span><span class="fl">2</span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(v) <span class="op">=</span> <span class="fu">f</span>(v<span class="op">...</span>)</span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a>a, b,c <span class="op">=</span> <span class="fu"></span>(f)(<span class="fl">2</span>,<span class="fl">2</span>,<span class="fl">2</span>)</span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a><span class="st">"</span><span class="sc">$</span>a<span class="st"> x + </span><span class="sc">$</span>b<span class="st"> y + </span><span class="sc">$</span>c<span class="st"> z = </span><span class="sc">$</span>([a,b,c] <span class="op"></span> [<span class="fl">2</span>,<span class="fl">2</span>,<span class="fl">2</span>])<span class="st">"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="16">
<pre><code>"20 x + 4 y + 4 z = 56"</code></pre>
</div>
</div>
</section>
<section id="newtons-method-to-solve-fxy-0-and-gxy0." class="level3" data-number="56.2.1">
<h3 data-number="56.2.1" class="anchored" data-anchor-id="newtons-method-to-solve-fxy-0-and-gxy0."><span class="header-section-number">56.2.1</span> Newtons method to solve <span class="math inline">\(f(x,y) = 0\)</span> and <span class="math inline">\(g(x,y)=0\)</span>.</h3>
<p>The level curve <span class="math inline">\(f(x,y)=0\)</span> and the level curve <span class="math inline">\(g(x,y)=0\)</span> may intersect. Solving algebraically for the intersection may be difficult in most cases, though the linear case is not. (The linear case being the intersection of two lines).</p>
<p>To elaborate, consider two linear equations written in a general form:</p>
<p><span class="math display">\[
\begin{align}
ax + by &amp;= u\\
cx + dy &amp;= v
\end{align}
\]</span></p>
<p>A method to solve this by hand would be to solve for <span class="math inline">\(y\)</span> from one equation, replace this expression into the second equation and then solve for <span class="math inline">\(x\)</span>. From there, <span class="math inline">\(y\)</span> can be found. A more advanced method expresses the problem in a matrix formulation of the form <span class="math inline">\(Mx=b\)</span> and solves that equation. This form of solving is implemented in <code>Julia</code>, through the “backslash” operator. Here is the general solution:</p>
<div class="cell" data-hold="true" data-execution_count="16">
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> a b c d u v</span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a>M <span class="op">=</span> [a b; c d]</span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a>B <span class="op">=</span> [u, v]</span>
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a>M <span class="op">\</span> B <span class="op">.|&gt;</span> simplify</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="17">
<pre><code>2-element Vector{Sym}:
(-b*v + d*u)/(a*d - b*c)
(a*v - c*u)/(a*d - b*c)</code></pre>
</div>
</div>
<p>The term <span class="math inline">\(\det(M) = ad-bc\)</span> term is important, as evidenced by its appearance in the denominator of each term. When this is zero there is not a unique solution, as in the typical case.</p>
<p>Using Newtons method to solve for intersection points, uses linearization of the surfaces to replace the problem to the intersection of level curves for tangent planes. This is the linear case that can be readily solved. As with Newtons method for the univariate case, the new answer is generally a better <em>approximation</em> to the answer, and the process is iterated to get a <em>good enough</em> approximation, as defined through some tolerance.</p>
<p>Consider the functions <span class="math inline">\(f(x,y) =2 - x^2 - y^2\)</span> and <span class="math inline">\(g(x,y) = 3 - 2x^2 - (1/3)y^2\)</span>. These graphs show their surfaces with the level sets for <span class="math inline">\(c=0\)</span> drawn and just the levels sets, showing they intersect in <span class="math inline">\(4\)</span> places.</p>
<div class="cell" data-hold="true" data-execution_count="17">
<div class="cell-output cell-output-display" data-execution_count="18">
<p><img src="scalar_functions_applications_files/figure-html/cell-18-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>We look to find the intersection point near <span class="math inline">\((1,1)\)</span> using Newtons method</p>
<p>We have by linearization:</p>
<p><span class="math display">\[
\begin{align}
f(x,y) &amp;\approx f(x_n, y_n) + \frac{\partial f}{\partial x}\Delta x + \frac{\partial f}{\partial y}\Delta y \\
g(x,y) &amp;\approx g(x_n, y_n) + \frac{\partial g}{\partial x}\Delta x + \frac{\partial g}{\partial y}\Delta y,
\end{align}
\]</span></p>
<p>where <span class="math inline">\(\Delta x = x- x_n\)</span> and <span class="math inline">\(\Delta y = y-y_n\)</span>. Setting <span class="math inline">\(f(x,y)=0\)</span> and <span class="math inline">\(g(x,y)=0\)</span>, leaves these two linear equations in <span class="math inline">\(\Delta x\)</span> and <span class="math inline">\(\Delta y\)</span>:</p>
<p><span class="math display">\[
\begin{align}
\frac{\partial f}{\partial x} \Delta x + \frac{\partial f}{\partial y} \Delta y &amp;= -f(x_n, y_n)\\
\frac{\partial g}{\partial x} \Delta x + \frac{\partial g}{\partial y} \Delta y &amp;= -g(x_n, y_n).
\end{align}
\]</span></p>
<p>One step of Newtons method defines <span class="math inline">\((x_{n+1}, y_{n+1})\)</span> to be the values <span class="math inline">\((x,y)\)</span> that make the linearized functions about <span class="math inline">\((x_n, y_n)\)</span> both equal to <span class="math inline">\(\vec{0}\)</span>.</p>
<p>As just described, we can use <code>Julia</code>s <code>\</code> operation to solve the above system of equations, if we express them in matrix form. With this, one step of Newtons method can be coded as follows:</p>
<div class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">newton_step</span>(f, g, xn)</span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a> M <span class="op">=</span> [ForwardDiff.<span class="fu">gradient</span>(f, xn)<span class="ch">'; ForwardDiff.gradient(g, xn)'</span>]</span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a> b <span class="op">=</span> <span class="op">-</span>[<span class="fu">f</span>(xn), <span class="fu">g</span>(xn)]</span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a> Delta <span class="op">=</span> M <span class="op">\</span> b</span>
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a> xn <span class="op">+</span> Delta</span>
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="19">
<pre><code>newton_step (generic function with 1 method)</code></pre>
</div>
</div>
<p>We investigate what happens starting at <span class="math inline">\((1,1)\)</span> after one step:</p>
<div class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="fu">𝒇</span>(x,y) <span class="op">=</span> <span class="fl">2</span> <span class="op">-</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> y<span class="op">^</span><span class="fl">2</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="fu">𝒈</span>(x,y) <span class="op">=</span> <span class="fl">3</span> <span class="op">-</span> <span class="fl">2</span>x<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> (<span class="fl">1</span><span class="op">/</span><span class="fl">3</span>)y<span class="op">^</span><span class="fl">2</span></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a><span class="fu">𝒇</span>(v) <span class="op">=</span> <span class="fu">𝒇</span>(v<span class="op">...</span>); <span class="fu">𝒈</span>(v) <span class="op">=</span> <span class="fu">𝒈</span>(v<span class="op">...</span>)</span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a>𝒙₀ <span class="op">=</span> [<span class="fl">1</span>,<span class="fl">1</span>]</span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a>𝒙₁ <span class="op">=</span> <span class="fu">newton_step</span>(𝒇, 𝒈, 𝒙₀)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="20">
<pre><code>2-element Vector{Float64}:
1.2
0.8</code></pre>
</div>
</div>
<p>The new function values are</p>
<div class="cell" data-execution_count="20">
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="fu">𝒇</span>(𝒙₁), <span class="fu">𝒈</span>(𝒙₁)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="21">
<pre><code>(-0.08000000000000007, -0.09333333333333327)</code></pre>
</div>
</div>
<p>We can get better approximations by iterating. Here we hard code <span class="math inline">\(4\)</span> more steps:</p>
<div class="cell" data-execution_count="21">
<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>𝒙₂ <span class="op">=</span> <span class="fu">newton_step</span>(𝒇, 𝒈, 𝒙₁)</span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a>𝒙₃ <span class="op">=</span> <span class="fu">newton_step</span>(𝒇, 𝒈, 𝒙₂)</span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a>𝒙₄ <span class="op">=</span> <span class="fu">newton_step</span>(𝒇, 𝒈, 𝒙₃)</span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a>𝒙₅ <span class="op">=</span> <span class="fu">newton_step</span>(𝒇, 𝒈, 𝒙₄)</span>
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a>𝒙₅, <span class="fu">𝒇</span>(𝒙₅), <span class="fu">𝒈</span>(𝒙₅)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="22">
<pre><code>([1.1832159566199232, 0.7745966692414834], 0.0, 1.6653345369377348e-16)</code></pre>
</div>
</div>
<p>We see that at the new point, <code>x5</code>, both functions are basically the same value, <span class="math inline">\(0\)</span>, so we have approximated the intersection point.</p>
<p>For nearby initial guesses and reasonable functions, Newtons method is <em>quadratic</em>, so should take few steps for convergence, as above.</p>
<p>Here is a simplistic method to iterate <span class="math inline">\(n\)</span> steps:</p>
<div class="cell" data-execution_count="22">
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">nm</span>(f, g, x, n<span class="op">=</span><span class="fl">5</span>)</span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> i <span class="kw">in</span> <span class="fl">1</span><span class="op">:</span>n</span>
<span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a> x <span class="op">=</span> <span class="fu">newton_step</span>(f, g, x)</span>
<span id="cb32-4"><a href="#cb32-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb32-5"><a href="#cb32-5" aria-hidden="true" tabindex="-1"></a> x</span>
<span id="cb32-6"><a href="#cb32-6" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="23">
<pre><code>nm (generic function with 2 methods)</code></pre>
</div>
</div>
<section id="example-4" class="level5">
<h5 class="anchored" data-anchor-id="example-4">Example</h5>
<p>Consider the <a href="https://blogs.scientificamerican.com/roots-of-unity/a-few-of-my-favorite-spaces-the-bicylinder/">bicylinder</a> the intersection of two perpendicular cylinders of the same radius. If the radius is <span class="math inline">\(1\)</span>, we might express these by the functions:</p>
<p><span class="math display">\[
f(x,y) = \sqrt{1 - y^2}, \quad g(x,y) = \sqrt{1 - x^2}.
\]</span></p>
<p>We see that <span class="math inline">\((1,1)\)</span>, <span class="math inline">\((-1,1)\)</span>, <span class="math inline">\((1,-1)\)</span> and <span class="math inline">\((-1,-1)\)</span> are solutions to <span class="math inline">\(f(x,y)=0\)</span>, <span class="math inline">\(g(x,y)=0\)</span> <em>and</em> <span class="math inline">\((0,0)\)</span> is a solution to <span class="math inline">\(f(x,y)=1\)</span> and <span class="math inline">\(g(x,y)=1\)</span>. What about a level like <span class="math inline">\(1/2\)</span>, say?</p>
<p>Rather than work with <span class="math inline">\(f(x,y) = c\)</span> we solve <span class="math inline">\(f(x,y)^2 = c^2\)</span>, as that will be avoid issues with the square root not being defined. Here is one way to solve:</p>
<div class="cell" data-hold="true" data-execution_count="23">
<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>c <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">2</span></span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y) <span class="op">=</span> <span class="fl">1</span> <span class="op">-</span> y<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> c<span class="op">^</span><span class="fl">2</span></span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a><span class="fu">g</span>(x,y) <span class="op">=</span> (<span class="fl">1</span> <span class="op">-</span> x<span class="op">^</span><span class="fl">2</span>) <span class="op">-</span> c<span class="op">^</span><span class="fl">2</span></span>
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(v) <span class="op">=</span> <span class="fu">f</span>(v<span class="op">...</span>); <span class="fu">g</span>(v) <span class="op">=</span> <span class="fu">g</span>(v<span class="op">...</span>)</span>
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a><span class="fu">nm</span>(f, g, [<span class="fl">1</span><span class="op">/</span><span class="fl">2</span>, <span class="fl">1</span><span class="op">/</span><span class="fl">3</span>])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="24">
<pre><code>2-element Vector{Float64}:
0.8660254037844386
0.8660254037935468</code></pre>
</div>
</div>
<p>That <span class="math inline">\(x=y\)</span> is not so surprising, and in fact, this problem can more easily be solved analytically through <span class="math inline">\(x^2 = y^2 = 1 - c^2\)</span>.</p>
</section>
</section>
</section>
<section id="implicit-differentiation" class="level2" data-number="56.3">
<h2 data-number="56.3" class="anchored" data-anchor-id="implicit-differentiation"><span class="header-section-number">56.3</span> Implicit differentiation</h2>
<p>Implicit differentiation of an equation of two variables (say <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span>) is performed by <em>assuming</em> <span class="math inline">\(y\)</span> is a function of <span class="math inline">\(x\)</span> and when differentiating an expression with <span class="math inline">\(y\)</span>, use the chain rule. For example, the slope of the tangent line, <span class="math inline">\(dy/dx\)</span>, for the general ellipse <span class="math inline">\(x^2/a + y^2/b = 1\)</span> can be found through this calculation:</p>
<p><span class="math display">\[
\frac{d}{dx}(\frac{x^2}{a} + \frac{y^2}{b}) =
\frac{d}{dx}(1),
\]</span></p>
<p>or, using <span class="math inline">\(d/dx(y^2) = 2y dy/dx\)</span>:</p>
<p><span class="math display">\[
\frac{2x}{a} + \frac{2y \frac{dy}{dx}}{b} = 0.
\]</span></p>
<p>From this, solving for <span class="math inline">\(dy/dx\)</span> is routine, as the equation is linear in that unknown: <span class="math inline">\(dy/dx = -(b/a)(x/y)\)</span></p>
<p>With more variables, the same technique may be used. Say we have variables <span class="math inline">\(x\)</span>, <span class="math inline">\(y\)</span>, and <span class="math inline">\(z\)</span> in a relation like <span class="math inline">\(F(x,y,z) = 0\)</span>. If we assume <span class="math inline">\(z=z(x,y)\)</span> for some differentiable function (we mention later what conditions will ensure this assumption is valid for some open set), then we can proceed as before, using the chain rule as necessary.</p>
<p>For example, consider the ellipsoid: <span class="math inline">\(x^2/a + y^2/b + z^2/c = 1\)</span>. What is <span class="math inline">\(\partial z/\partial x\)</span> and <span class="math inline">\(\partial{z}/\partial{y}\)</span>, as needed to describe the tangent plane as above?</p>
<p>To find <span class="math inline">\(\partial/\partial{x}\)</span> we have:</p>
<p><span class="math display">\[
\frac{\partial}{\partial{x}}(x^2/a + y^2/b + z^2/c) =
\frac{\partial}{\partial{x}}1,
\]</span></p>
<p>or</p>
<p><span class="math display">\[
\frac{2x}{a} + \frac{0}{b} + \frac{2z\frac{\partial{z}}{\partial{x}}}{c} = 0.
\]</span></p>
<p>Again the desired unknown is within a linear equation so can readily be solved:</p>
<p><span class="math display">\[
\frac{\partial{z}}{\partial{x}} = -\frac{c}{a} \frac{x}{z}.
\]</span></p>
<p>A similar approach can be used for <span class="math inline">\(\partial{z}/\partial{y}\)</span>.</p>
<section id="example-5" class="level5">
<h5 class="anchored" data-anchor-id="example-5">Example</h5>
<p>Let <span class="math inline">\(f(x,y,z) = x^4 -x^3 + y^2 + z^2 = 0\)</span> be a surface with point <span class="math inline">\((2,2,2)\)</span>. Find <span class="math inline">\(\partial{z}/\partial{x}\)</span> and <span class="math inline">\(\partial{z}/\partial{y}\)</span>.</p>
<p>To find <span class="math inline">\(\partial{z}/\partial{x}\)</span> and <span class="math inline">\(\partial{z}/\partial{y}\)</span> we have:</p>
<div class="cell" data-hold="true" data-execution_count="24">
<div class="sourceCode cell-code" id="cb36"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x, y, <span class="fu">Z</span>()</span>
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a>∂x <span class="op">=</span> <span class="fu">solve</span>(<span class="fu">diff</span>(x<span class="op">^</span><span class="fl">4</span> <span class="op">-</span>x<span class="op">^</span><span class="fl">3</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> <span class="fu">Z</span>(x,y)<span class="op">^</span><span class="fl">2</span>, x), <span class="fu">diff</span>(<span class="fu">Z</span>(x,y),x))</span>
<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a>∂y <span class="op">=</span> <span class="fu">solve</span>(<span class="fu">diff</span>(x<span class="op">^</span><span class="fl">4</span> <span class="op">-</span>x<span class="op">^</span><span class="fl">3</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> <span class="fu">Z</span>(x,y)<span class="op">^</span><span class="fl">2</span>, x), <span class="fu">diff</span>(<span class="fu">Z</span>(x,y),y))</span>
<span id="cb36-4"><a href="#cb36-4" aria-hidden="true" tabindex="-1"></a>∂x, ∂y</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="25">
<pre><code>(Sym[x^2*(3 - 4*x)/(2*Z(x, y))], Any[])</code></pre>
</div>
</div>
</section>
</section>
<section id="optimization" class="level2" data-number="56.4">
<h2 data-number="56.4" class="anchored" data-anchor-id="optimization"><span class="header-section-number">56.4</span> Optimization</h2>
<p>For a continuous univariate function <span class="math inline">\(f:R \rightarrow R\)</span> over an interval <span class="math inline">\(I\)</span> the question of finding a maximum or minimum value is aided by two theorems:</p>
<ul>
<li>The Extreme Value Theorem, which states that if <span class="math inline">\(I\)</span> is closed (e.g, <span class="math inline">\(I=[a,b]\)</span>) then <span class="math inline">\(f\)</span> has a maximum (minimum) value <span class="math inline">\(M\)</span> and there is at least one value <span class="math inline">\(c\)</span> with <span class="math inline">\(a \leq c \leq b\)</span> with <span class="math inline">\(M = f(x)\)</span>.</li>
<li><a href="https://tinyurl.com/nfgz8fz">Fermat</a>s theorem on critical points, which states that if <span class="math inline">\(f:(a,b) \rightarrow R\)</span> and <span class="math inline">\(x_0\)</span> is such that <span class="math inline">\(a &lt; x_0 &lt; b\)</span> and <span class="math inline">\(f(x_0)\)</span> is a <em>local</em> extremum. If <span class="math inline">\(f\)</span> is differentiable at <span class="math inline">\(x_0\)</span>, then <span class="math inline">\(f'(x_0) = 0\)</span>. That is, local extrema of <span class="math inline">\(f\)</span> happen at points where the derivative does not exist or is <span class="math inline">\(0\)</span> (critical points).</li>
</ul>
<p>These two theorems provide an algorithm to find the extreme values of a continuous function over a closed interval: find the critical points, check these and the end points for the maximum and minimum value.</p>
<p>These checks can be reduced by two theorems that can classify critical points as local extrema, the first and second derivative tests.</p>
<p>These theorems have generalizations to scalar functions, allowing a similar study of extrema.</p>
<p>First, we define a <em>local</em> maximum for <span class="math inline">\(f:R^n \rightarrow R\)</span> over a region <span class="math inline">\(U\)</span>: a point <span class="math inline">\(\vec{a}\)</span> in <span class="math inline">\(U\)</span> is a <em>local</em> maximum if <span class="math inline">\(f(\vec{a}) \geq f(\vec{u})\)</span> for all <span class="math inline">\(u\)</span> in some ball about <span class="math inline">\(\vec{a}\)</span>. A <em>local</em> minimum would have <span class="math inline">\(\leq\)</span> instead.</p>
<p>An <em>absolute</em> maximum over <span class="math inline">\(U\)</span>, should it exist, would be <span class="math inline">\(f(\vec{a})\)</span> if there exists a value <span class="math inline">\(\vec{a}\)</span> in <span class="math inline">\(U\)</span> with the property <span class="math inline">\(f(\vec{a}) \geq f(\vec{u})\)</span> for all <span class="math inline">\(\vec{u}\)</span> in <span class="math inline">\(U\)</span>.</p>
<p>The difference is the same as the one-dimensional case: local is a statement about nearby points only, absolute a statement about all the points in the specified set.</p>
<blockquote class="blockquote">
<p>The <a href="https://tinyurl.com/yyhgxu8y">Extreme Value Theorem</a> Let <span class="math inline">\(f:R^n \rightarrow R\)</span> be continuous and defined on <em>closed</em> set <span class="math inline">\(V\)</span>. Then <span class="math inline">\(f\)</span> has a minimum value <span class="math inline">\(m\)</span> and maximum value <span class="math inline">\(M\)</span> over <span class="math inline">\(V\)</span> and there exists at least two points <span class="math inline">\(\vec{a}\)</span> and <span class="math inline">\(\vec{b}\)</span> with <span class="math inline">\(m = f(\vec{a})\)</span> and <span class="math inline">\(M = f(\vec{b})\)</span>.</p>
</blockquote>
<blockquote class="blockquote">
<p><a href="https://tinyurl.com/nfgz8fz">Fermat</a>s theorem on critical points. Let <span class="math inline">\(f:R^n \rightarrow R\)</span> be a continuous function defined on an <em>open</em> set <span class="math inline">\(U\)</span>. If <span class="math inline">\(x \in U\)</span> is a point where <span class="math inline">\(f\)</span> has a local extrema <em>and</em> <span class="math inline">\(f\)</span> is differentiable, then the gradient of <span class="math inline">\(f\)</span> at <span class="math inline">\(x\)</span> is <span class="math inline">\(\vec{0}\)</span>.</p>
</blockquote>
<p>Call a point in the domain of <span class="math inline">\(f\)</span> where the function is differentiable and the gradient is zero a <em>stationary point</em> and a point in the domain where the function is either not differentiable or is a stationary point a <em>critical point</em>. The local extrema can only happen at critical points by Fermat.</p>
<p>Consider the function <span class="math inline">\(f(x,y) = e^{-(x^2 + y^2)/5} \cos(x^2 + y^2)\)</span>.</p>
<div class="cell" data-hold="true" data-execution_count="25">
<div class="sourceCode cell-code" id="cb38"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y)<span class="op">=</span> <span class="fu">exp</span>(<span class="fu">-</span>(x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span>)<span class="op">/</span><span class="fl">5</span>) <span class="op">*</span> <span class="fu">cos</span>(x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span>)</span>
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">4</span>, <span class="fl">4</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a><span class="fu">surface</span>(xs, ys, f, legend<span class="op">=</span><span class="cn">false</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="26">
<p><img src="scalar_functions_applications_files/figure-html/cell-26-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>This function is differentiable and the gradient is given by:</p>
<p><span class="math display">\[
\nabla{f} = -2/5e^{-(x^2 + y^2)/5} (5\sin(x^2 + y^2) + \cos(x^2 + y^2)) \langle x, y \rangle.
\]</span></p>
<p>This is zero at the origin, or when <span class="math inline">\(5\sin(x^2 + y^2) = -\cos(x^2 + y^2)\)</span>. The latter is <span class="math inline">\(0\)</span> on circles of radius <span class="math inline">\(r\)</span> where <span class="math inline">\(5\sin(r) = \cos(r)\)</span> or <span class="math inline">\(r = \tan^{-1}(-1/5) + k\pi\)</span> for <span class="math inline">\(k = 1, 2, \dots\)</span>. This matches the graph, where the extrema are on circles by symmetry. Imagine now, picking a value where the function takes a maximum and adding the tangent plane. As the gradient is <span class="math inline">\(\vec{0}\)</span>, this will be flat. The point at the origin will have the surface fall off from the tangent plane in each direction, whereas the other points, will have a circle where the tangent plane rests on the surface, but otherwise will fall off from the tangent plane. Characterizing this “falling off” will help to identify local maxima that are distinct.</p>
<hr>
<p>Now consider the differentiable function <span class="math inline">\(f(x,y) = xy\)</span>, graphed below with the projections of the <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> axes:</p>
<div class="cell" data-hold="true" data-execution_count="26">
<div class="sourceCode cell-code" id="cb39"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y) <span class="op">=</span> x<span class="op">*</span>y</span>
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">3</span>, <span class="fl">3</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></a><span class="fu">surface</span>(xs, ys, f, legend<span class="op">=</span><span class="cn">false</span>)</span>
<span id="cb39-4"><a href="#cb39-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb39-5"><a href="#cb39-5" aria-hidden="true" tabindex="-1"></a><span class="fu">plot_parametric!</span>(<span class="op">-</span><span class="fl">4</span><span class="op">..</span><span class="fl">4</span>, t <span class="op">-&gt;</span> [t, <span class="fl">0</span>, <span class="fu">f</span>(t, <span class="fl">0</span>)], linewidth<span class="op">=</span><span class="fl">5</span>)</span>
<span id="cb39-6"><a href="#cb39-6" aria-hidden="true" tabindex="-1"></a><span class="fu">plot_parametric!</span>(<span class="op">-</span><span class="fl">4</span><span class="op">..</span><span class="fl">4</span>, t <span class="op">-&gt;</span> [<span class="fl">0</span>, t, <span class="fu">f</span>(<span class="fl">0</span>, t)], linewidth<span class="op">=</span><span class="fl">5</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="27">
<p><img src="scalar_functions_applications_files/figure-html/cell-27-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>The extrema happen at the edges of the region. The gradient is <span class="math inline">\(\nabla{f} = \langle y, x \rangle\)</span>. This is <span class="math inline">\(\vec{0}\)</span> only at the origin. At the origin, were we to imagine a tangent plane, the surface falls off in one direction but falls <em>above</em> in the other direction. Such a point is referred to as a <em>saddle point</em>. A saddle point for a continuous <span class="math inline">\(f:R^n \rightarrow R\)</span> would be a critical point, <span class="math inline">\(\vec{a}\)</span> where for any ball with non-zero radius about <span class="math inline">\(\vec{a}\)</span>, there are values where the function is greater than <span class="math inline">\(f(\vec{a})\)</span> and values where the function is less.</p>
<p>To identify these through formulas, and not graphically, we could try and use the first derivative test along all paths through <span class="math inline">\(\vec{a}\)</span>, but this approach is better at showing something isnt the case, like two paths to show non-continuity.</p>
<p>The generalization of the <em>second</em> derivative test is more concrete though. Recall, the second derivative test is about the concavity of the function at the critical point. When the concavity can be determined as non-zero, the test is conclusive; when the concavity is zero, the test is not conclusive. Similarly here:</p>
<blockquote class="blockquote">
<p>The <a href="https://en.wikipedia.org/wiki/Second_partial_derivative_test">second</a> Partial Derivative Test for <span class="math inline">\(f:R^2 \rightarrow R\)</span>.</p>
<p>Assume the first and second partial derivatives of <span class="math inline">\(f\)</span> are defined and continuous; <span class="math inline">\(\vec{a}\)</span> be a critical point of <span class="math inline">\(f\)</span>; <span class="math inline">\(H\)</span> is the hessian matrix, <span class="math inline">\([f_{xx}\quad f_{xy};f_{xy}\quad f_{yy}]\)</span>, and <span class="math inline">\(d = \det(H) = f_{xx} f_{yy} - f_{xy}^2\)</span> is the determinant of the Hessian matrix. Then:</p>
<ul>
<li>The function <span class="math inline">\(f\)</span> has a local minimum at <span class="math inline">\(\vec{a}\)</span> if <span class="math inline">\(f_{xx} &gt; 0\)</span> <em>and</em> <span class="math inline">\(d&gt;0\)</span>,</li>
<li>The function <span class="math inline">\(f\)</span> has a local maximum at <span class="math inline">\(\vec{a}\)</span> if <span class="math inline">\(f_{xx} &lt; 0\)</span> <em>and</em> <span class="math inline">\(d&gt;0\)</span>,</li>
<li>The function <span class="math inline">\(f\)</span> has a saddle point at <span class="math inline">\(\vec{a}\)</span> if <span class="math inline">\(d &lt; 0\)</span>,</li>
<li>Nothing can be said if <span class="math inline">\(d=0\)</span>.</li>
</ul>
</blockquote>
<hr>
<p>The intuition behind a proof follows. The case when <span class="math inline">\(f_{xx} &gt; 0\)</span> and <span class="math inline">\(d &gt; 0\)</span> uses a consequence of these assumptions that for any non-zero vector <span class="math inline">\(\vec{x}\)</span> it <em>must</em> be that <span class="math inline">\(x\cdot(Hx) &gt; 0\)</span> (<a href="https://en.wikipedia.org/wiki/Definiteness_of_a_matrix">positive definite</a>) <em>and</em> the quadratic approximation <span class="math inline">\(f(\vec{a}+d\vec{x}) \approx f(\vec{a}) + \nabla{f}(\vec{a}) \cdot d\vec{x} + d\vec{x} \cdot (Hd\vec{x}) = f(\vec{a}) + d\vec{x} \cdot (Hd\vec{x})\)</span>, so for any <span class="math inline">\(d\vec{x}\)</span> small enough, <span class="math inline">\(f(\vec{a}+d\vec{x}) \geq f(\vec{a})\)</span>. That is <span class="math inline">\(f(\vec{a})\)</span> is a local minimum. Similarly, a proof for the local maximum follows by considering <span class="math inline">\(-f\)</span>. Finally, if <span class="math inline">\(d &lt; 0\)</span>, then there are vectors, <span class="math inline">\(d\vec{x}\)</span>, for which <span class="math inline">\(d\vec{x} \cdot (Hd\vec{x})\)</span> will have different signs, and along these vectors the function will be concave up/concave down.</p>
<p>Apply this to <span class="math inline">\(f(x,y) = xy\)</span> at <span class="math inline">\(\vec{a} = \vec{0}\)</span> we have <span class="math inline">\(f_{xx} = f_{yy} = 0\)</span> and <span class="math inline">\(f_{xy} = 1\)</span>, so the determinant of the Hessian is <span class="math inline">\(-1\)</span>. By the second partial derivative test, this critical point is a saddle point, as seen from a previous graph.</p>
<p>Applying this to <span class="math inline">\(f(x,y) = e^{-(x^2 + y^2)/5} \cos(x^2 + y^2)\)</span>, we will use <code>SymPy</code> to compute the derivatives, as they get a bit involved:</p>
<div class="cell" data-execution_count="27">
<div class="sourceCode cell-code" id="cb40"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a><span class="fu">fₖ</span>(x,y) <span class="op">=</span> <span class="fu">exp</span>(<span class="fu">-</span>(x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span>)<span class="op">/</span><span class="fl">5</span>) <span class="op">*</span> <span class="fu">cos</span>(x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span>)</span>
<span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a>Hₖ <span class="op">=</span> sympy.<span class="fu">hessian</span>(<span class="fu">fₖ</span>(x,y), (x,y))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="28">
<pre><code>2×2 Matrix{Sym}:
8*x^2*exp(-x^2/5 - y^2/5)*sin(x^2 + y^2)/5 - 96*x^2*exp(-x^2/5 - y^2/5)*cos(x^2 + y^2)/25 - 2*exp(-x^2/5 - y^2/5)*sin(x^2 + y^2) - 2*exp(-x^2/5 - y^2/5)*cos(x^2 + y^2)/5 … 8*x*y*exp(-x^2/5 - y^2/5)*sin(x^2 + y^2)/5 - 96*x*y*exp(-x^2/5 - y^2/5)*cos(x^2 + y^2)/25
8*x*y*exp(-x^2/5 - y^2/5)*sin(x^2 + y^2)/5 - 96*x*y*exp(-x^2/5 - y^2/5)*cos(x^2 + y^2)/25 8*y^2*exp(-x^2/5 - y^2/5)*sin(x^2 + y^2)/5 - 96*y^2*exp(-x^2/5 - y^2/5)*cos(x^2 + y^2)/25 - 2*exp(-x^2/5 - y^2/5)*sin(x^2 + y^2) - 2*exp(-x^2/5 - y^2/5)*cos(x^2 + y^2)/5</code></pre>
</div>
</div>
<p>This is messy, but we only consider it at critical points. The point <span class="math inline">\((0,0)\)</span> is graphically a local maximum. We can see from the Hessian, that the second partial derivative test will give the same characterization:</p>
<div class="cell" data-execution_count="28">
<div class="sourceCode cell-code" id="cb42"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a>H₀₀ <span class="op">=</span> <span class="fu">subs</span>.(Hₖ, x<span class="op">=&gt;</span><span class="fl">0</span>, y<span class="op">=&gt;</span><span class="fl">0</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="29">
<pre><code>2×2 Matrix{Sym}:
-2/5 0
0 -2/5</code></pre>
</div>
</div>
<p>Which satisfies:</p>
<div class="cell" data-execution_count="29">
<div class="sourceCode cell-code" id="cb44"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a>H₀₀[<span class="fl">1</span>,<span class="fl">1</span>] <span class="op">&lt;</span> <span class="fl">0</span> <span class="op">&amp;&amp;</span> <span class="fu">det</span>(H₀₀) <span class="op">&gt;</span> <span class="fl">0</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="30">
<pre><code>true</code></pre>
</div>
</div>
<p>Now consider <span class="math inline">\(\vec{a} = \langle \sqrt{2\pi + \tan^{-1}(-1/5)}, 0 \rangle\)</span>, a point on the first visible ring on the graph. The gradient vanishes here:</p>
<div class="cell" data-hold="true" data-execution_count="30">
<div class="sourceCode cell-code" id="cb46"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a>gradfₖ <span class="op">=</span> <span class="fu">diff</span>.(<span class="fu">fₖ</span>(x,y), [x,y])</span>
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a>a <span class="op">=</span> [<span class="fu">sqrt</span>(<span class="fl">2</span>PI <span class="op">+</span> <span class="fu">atan</span>(<span class="fu">-Sym</span>(<span class="fl">1</span>)<span class="op">//</span><span class="fl">5</span>)), <span class="fl">0</span>]</span>
<span id="cb46-3"><a href="#cb46-3" aria-hidden="true" tabindex="-1"></a><span class="fu">subs</span>.(gradfₖ, x <span class="op">=&gt;</span> a[<span class="fl">1</span>], y <span class="op">=&gt;</span> a[<span class="fl">2</span>])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="31">
<pre><code>2-element Vector{Sym}:
0
0</code></pre>
</div>
</div>
<p>But the test is <em>inconclusive</em>, as the determinant of the Hessian is <span class="math inline">\(0\)</span>:</p>
<div class="cell" data-hold="true" data-execution_count="31">
<div class="sourceCode cell-code" id="cb48"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a>a <span class="op">=</span> [<span class="fu">sqrt</span>(PI <span class="op">+</span> <span class="fu">atan</span>(<span class="fu">-Sym</span>(<span class="fl">1</span>)<span class="op">//</span><span class="fl">5</span>)), <span class="fl">0</span>]</span>
<span id="cb48-2"><a href="#cb48-2" aria-hidden="true" tabindex="-1"></a>H_a <span class="op">=</span> <span class="fu">subs</span>.(Hₖ, x <span class="op">=&gt;</span> a[<span class="fl">1</span>], y <span class="op">=&gt;</span> a[<span class="fl">2</span>])</span>
<span id="cb48-3"><a href="#cb48-3" aria-hidden="true" tabindex="-1"></a><span class="fu">det</span>(H_a)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="32">
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
\[
0
\]
</span>
</div>
</div>
<p>(The test is inconclusive, as it needs the function to “fall away” from the tangent plane in all directions, in this case, along a circular curve, the function touches the tangent plane, so it doesnt fall away.)</p>
<section id="example-6" class="level5">
<h5 class="anchored" data-anchor-id="example-6">Example</h5>
<p>Characterize the critical points of <span class="math inline">\(f(x,y) = 4xy - x^4 - y^4\)</span>.</p>
<p>The critical points may be found by solving when the gradient is <span class="math inline">\(\vec{0}\)</span>:</p>
<div class="cell" data-execution_count="32">
<div class="sourceCode cell-code" id="cb49"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="fu">fⱼ</span>(x,y) <span class="op">=</span> <span class="fl">4</span>x<span class="op">*</span>y <span class="op">-</span> x<span class="op">^</span><span class="fl">4</span> <span class="op">-</span> y<span class="op">^</span><span class="fl">4</span></span>
<span id="cb49-2"><a href="#cb49-2" aria-hidden="true" tabindex="-1"></a>gradfⱼ <span class="op">=</span> <span class="fu">diff</span>.(<span class="fu">fⱼ</span>(x,y), [x,y])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="33">
<pre><code>2-element Vector{Sym}:
-4*x^3 + 4*y
4*x - 4*y^3</code></pre>
</div>
</div>
<div class="cell" data-execution_count="33">
<div class="sourceCode cell-code" id="cb51"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a>all_ptsⱼ <span class="op">=</span> <span class="fu">solve</span>(gradfⱼ, [x,y])</span>
<span id="cb51-2"><a href="#cb51-2" aria-hidden="true" tabindex="-1"></a>ptsⱼ <span class="op">=</span> <span class="fu">filter</span>(u <span class="op">-&gt;</span> <span class="fu">all</span>(<span class="fu">isreal</span>.(u)), all_ptsⱼ)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="34">
<pre><code>3-element Vector{Tuple{Sym, Sym}}:
(-1, -1)
(0, 0)
(1, 1)</code></pre>
</div>
</div>
<p>There are <span class="math inline">\(3\)</span> real critical points. To classify them we need the sign of <span class="math inline">\(f_{xx}\)</span> and the determinant of the Hessian. We make a simple function to compute these, then apply it to each point using a comprehension:</p>
<div class="cell" data-execution_count="34">
<div class="sourceCode cell-code" id="cb53"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a>Hⱼ <span class="op">=</span> sympy.<span class="fu">hessian</span>(<span class="fu">fⱼ</span>(x,y), (x,y))</span>
<span id="cb53-2"><a href="#cb53-2" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">classify</span>(H, pt)</span>
<span id="cb53-3"><a href="#cb53-3" aria-hidden="true" tabindex="-1"></a> Ha <span class="op">=</span> <span class="fu">subs</span>.(H, x <span class="op">.=&gt;</span> pt[<span class="fl">1</span>], y <span class="op">.=&gt;</span> pt[<span class="fl">2</span>])</span>
<span id="cb53-4"><a href="#cb53-4" aria-hidden="true" tabindex="-1"></a> (det<span class="op">=</span><span class="fu">det</span>(Ha), f_xx<span class="op">=</span>Ha[<span class="fl">1</span>,<span class="fl">1</span>])</span>
<span id="cb53-5"><a href="#cb53-5" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span>
<span id="cb53-6"><a href="#cb53-6" aria-hidden="true" tabindex="-1"></a>[<span class="fu">classify</span>(Hⱼ, pt) for pt <span class="kw">in</span> ptsⱼ]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="35">
<pre><code>3-element Vector{NamedTuple{(:det, :f_xx), Tuple{Sym, Sym}}}:
(det = 128, f_xx = -12)
(det = -16, f_xx = 0)
(det = 128, f_xx = -12)</code></pre>
</div>
</div>
<p>We see the first and third points have positive determinant and negative <span class="math inline">\(f_{xx}\)</span>, so are relative maxima, and the second point has negative derivative, so is a saddle point. We graphically confirm this:</p>
<div class="cell" data-hold="true" data-execution_count="35">
<div class="sourceCode cell-code" id="cb55"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">3</span><span class="op">/</span><span class="fl">2</span>, <span class="fl">3</span><span class="op">/</span><span class="fl">2</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a>p <span class="op">=</span> <span class="fu">surface</span>(xs, ys, fⱼ, legend<span class="op">=</span><span class="cn">false</span>)</span>
<span id="cb55-3"><a href="#cb55-3" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> pt <span class="op"></span> ptsⱼ</span>
<span id="cb55-4"><a href="#cb55-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scatter!</span>(p, <span class="fu">unzip</span>([<span class="fu">N</span>.([pt<span class="op">...</span>,<span class="fu">fⱼ</span>(pt<span class="op">...</span>)])])<span class="op">...</span>,</span>
<span id="cb55-5"><a href="#cb55-5" aria-hidden="true" tabindex="-1"></a> markercolor<span class="op">=:</span>black, markersize<span class="op">=</span><span class="fl">5</span>) <span class="co"># add each pt on surface</span></span>
<span id="cb55-6"><a href="#cb55-6" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb55-7"><a href="#cb55-7" aria-hidden="true" tabindex="-1"></a>p</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="36">
<p><img src="scalar_functions_applications_files/figure-html/cell-36-output-1.svg" class="img-fluid"></p>
</div>
</div>
</section>
<section id="example-7" class="level5">
<h5 class="anchored" data-anchor-id="example-7">Example</h5>
<p>Consider the function <span class="math inline">\(f(x,y) = x^2 + 3y^2 -x\)</span> over the region <span class="math inline">\(x^2 + y^2 \leq 1\)</span>. This is a continuous function over a closed set, so will have both an absolute maximum and minimum. Find these from an investigation of the critical points and the boundary points.</p>
<p>The gradient is easily found: <span class="math inline">\(\nabla{f} = \langle 2x - 1, 6y \rangle\)</span>, and is <span class="math inline">\(\vec{0}\)</span> only at <span class="math inline">\(\vec{a} = \langle 1/2, 0 \rangle\)</span>. The Hessian is:</p>
<p><span class="math display">\[
H = \left[
\begin{array}{}
2 &amp; 0\\
0 &amp; 6
\end{array}
\right].
\]</span></p>
<p>At <span class="math inline">\(\vec{a}\)</span> this has positive determinant and <span class="math inline">\(f_{xx} &gt; 0\)</span>, so <span class="math inline">\(\vec{a}\)</span> corresponds to a <em>local</em> minimum with values <span class="math inline">\(f(\vec{a}) = (1/2)^2 + 3(0) - 1/2 = -1/4\)</span>. The absolute maximum and minimum may occur here (well, not the maximum) or on the boundary, so that must be considered. In this case we can easily parameterize the boundary and turn this into the univariate case:</p>
<div class="cell" data-execution_count="36">
<div class="sourceCode cell-code" id="cb56"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a><span class="fu">fₗ</span>(x,y) <span class="op">=</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> <span class="fl">2</span>y<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> x</span>
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a><span class="fu">fₗ</span>(v) <span class="op">=</span> <span class="fu">fₗ</span>(v<span class="op">...</span>)</span>
<span id="cb56-3"><a href="#cb56-3" aria-hidden="true" tabindex="-1"></a><span class="fu">gammaₗ</span>(t) <span class="op">=</span> [<span class="fu">cos</span>(t), <span class="fu">sin</span>(t)] <span class="co"># traces out x^2 + y^2 = 1 over [0, 2pi]</span></span>
<span id="cb56-4"><a href="#cb56-4" aria-hidden="true" tabindex="-1"></a>gₗ <span class="op">=</span> fₗ <span class="op"></span> gammaₗ</span>
<span id="cb56-5"><a href="#cb56-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-6"><a href="#cb56-6" aria-hidden="true" tabindex="-1"></a>cpsₗ <span class="op">=</span> <span class="fu">find_zeros</span>(gₗ<span class="op">'</span>, <span class="fl">0</span>, <span class="fl">2</span>pi) <span class="co"># critical points of g</span></span>
<span id="cb56-7"><a href="#cb56-7" aria-hidden="true" tabindex="-1"></a><span class="fu">append!</span>(cpsₗ, [<span class="fl">0</span>, <span class="fl">2</span>pi])</span>
<span id="cb56-8"><a href="#cb56-8" aria-hidden="true" tabindex="-1"></a><span class="fu">unique!</span>(cpsₗ)</span>
<span id="cb56-9"><a href="#cb56-9" aria-hidden="true" tabindex="-1"></a><span class="fu">gₗ</span>.(cpsₗ)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="37">
<pre><code>5-element Vector{Float64}:
0.0
2.25
2.0
2.25
0.0</code></pre>
</div>
</div>
<p>We see that maximum value is <code>2.25</code> and that the interior point, <span class="math inline">\(\vec{a}\)</span>, will be where the minimum value occurs. To see exactly where the maximum occurs, we look at the values of gamma:</p>
<div class="cell" data-execution_count="37">
<div class="sourceCode cell-code" id="cb58"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a>inds <span class="op">=</span> [<span class="fl">2</span>,<span class="fl">4</span>]</span>
<span id="cb58-2"><a href="#cb58-2" aria-hidden="true" tabindex="-1"></a>cpsₗ[inds]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="38">
<pre><code>2-element Vector{Float64}:
2.0943951023931953
4.1887902047863905</code></pre>
</div>
</div>
<p>These are multiples of <span class="math inline">\(\pi\)</span>:</p>
<div class="cell" data-execution_count="38">
<div class="sourceCode cell-code" id="cb60"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a>cpsₗ[inds]<span class="op">/</span><span class="cn">pi</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="39">
<pre><code>2-element Vector{Float64}:
0.6666666666666666
1.3333333333333333</code></pre>
</div>
</div>
<p>So we have the maximum occurs at the angles <span class="math inline">\(2\pi/3\)</span> and <span class="math inline">\(4\pi/3\)</span>. Here we visualize, using a hacky trick of assigning <code>NaN</code> values to the function to avoid plotting outside the circle:</p>
<div class="cell" data-execution_count="39">
<div class="sourceCode cell-code" id="cb62"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a><span class="fu">hₗ</span>(x,y) <span class="op">=</span> <span class="fu">fₗ</span>(x,y) <span class="op">*</span> (x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span> <span class="op">&lt;=</span> <span class="fl">1</span> ? <span class="fl">1</span> <span class="op">:</span> <span class="cn">NaN</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="40">
<pre><code>hₗ (generic function with 1 method)</code></pre>
</div>
</div>
<div class="cell" data-hold="true" data-execution_count="40">
<div class="sourceCode cell-code" id="cb64"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">1</span>,<span class="fl">1</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb64-2"><a href="#cb64-2" aria-hidden="true" tabindex="-1"></a><span class="fu">surface</span>(xs, ys, hₗ)</span>
<span id="cb64-3"><a href="#cb64-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb64-4"><a href="#cb64-4" aria-hidden="true" tabindex="-1"></a>ts <span class="op">=</span> cpsₗ <span class="co"># 2pi/3 and 4pi/3 by above</span></span>
<span id="cb64-5"><a href="#cb64-5" aria-hidden="true" tabindex="-1"></a>xs, ys <span class="op">=</span> <span class="fu">cos</span>.(ts), <span class="fu">sin</span>.(ts)</span>
<span id="cb64-6"><a href="#cb64-6" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter!</span>(xs, ys, fₗ)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="41">
<p><img src="scalar_functions_applications_files/figure-html/cell-41-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>A contour plot also shows that some - and only one - extrema happens on the interior:</p>
<div class="cell" data-hold="true" data-execution_count="41">
<div class="sourceCode cell-code" id="cb65"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb65-1"><a href="#cb65-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">1</span>,<span class="fl">1</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb65-2"><a href="#cb65-2" aria-hidden="true" tabindex="-1"></a><span class="fu">contour</span>(xs, ys, hₗ)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="42">
<p><img src="scalar_functions_applications_files/figure-html/cell-42-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>The extrema are identified by the enclosing regions, in this case the one around the point <span class="math inline">\((1/2, 0)\)</span>.</p>
</section>
<section id="example-steiners-problem" class="level5">
<h5 class="anchored" data-anchor-id="example-steiners-problem">Example: Steiners problem</h5>
<p>This is from <a href="https://ocw.mit.edu/resources/res-18-001-calculus-online-textbook-spring-2005/textbook/MITRES_18_001_strang_13.pdf">Strang</a> p 506.</p>
<p>We have three points in the plane, <span class="math inline">\((x_1, y_1)\)</span>, <span class="math inline">\((x_2, y_2)\)</span>, and <span class="math inline">\((x_3,y_3)\)</span>. A point <span class="math inline">\(p=(p_x, p_y)\)</span> will have <span class="math inline">\(3\)</span> distances <span class="math inline">\(d_1\)</span>, <span class="math inline">\(d_2\)</span>, and <span class="math inline">\(d_3\)</span>. Broadly speaking we want to minimize to find the point <span class="math inline">\(p\)</span> “nearest” the three fixed points within the triangle. Locating a facility so that it can service <span class="math inline">\(3\)</span> separate cities might be one application. The answer depends on the notion of what measure of distance to use.</p>
<p>If the measure is the Euclidean distance, then <span class="math inline">\(d_i^2 = (p_x - x_i)^2 + (p_y - y_i)^2\)</span>. If we sought to minimize <span class="math inline">\(d_1^2 + d_2^2 + d_3^2\)</span>, then we would proceed as follows:</p>
<div class="cell" data-execution_count="42">
<div class="sourceCode cell-code" id="cb66"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb66-1"><a href="#cb66-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x1 y1 x2 y2 x3 y3</span>
<span id="cb66-2"><a href="#cb66-2" aria-hidden="true" tabindex="-1"></a><span class="fu">d2</span>(p,x) <span class="op">=</span> (p[<span class="fl">1</span>] <span class="op">-</span> x[<span class="fl">1</span>])<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> (p[<span class="fl">2</span>]<span class="op">-</span>x[<span class="fl">2</span>])<span class="op">^</span><span class="fl">2</span></span>
<span id="cb66-3"><a href="#cb66-3" aria-hidden="true" tabindex="-1"></a>d2_1, d2_2, d2_3 <span class="op">=</span> <span class="fu">d2</span>((x,y), (x1, y1)), <span class="fu">d2</span>((x,y), (x2, y2)), <span class="fu">d2</span>((x,y), (x3, y3))</span>
<span id="cb66-4"><a href="#cb66-4" aria-hidden="true" tabindex="-1"></a>exₛ <span class="op">=</span> d2_1 <span class="op">+</span> d2_2 <span class="op">+</span> d2_3</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="43">
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
\[
\left(x - x_{1}\right)^{2} + \left(x - x_{2}\right)^{2} + \left(x - x_{3}\right)^{2} + \left(y - y_{1}\right)^{2} + \left(y - y_{2}\right)^{2} + \left(y - y_{3}\right)^{2}
\]
</span>
</div>
</div>
<p>We then find the gradient, and solve for when it is <span class="math inline">\(\vec{0}\)</span>:</p>
<div class="cell" data-execution_count="43">
<div class="sourceCode cell-code" id="cb67"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a>gradfₛ <span class="op">=</span> <span class="fu">diff</span>.(exₛ, [x,y])</span>
<span id="cb67-2"><a href="#cb67-2" aria-hidden="true" tabindex="-1"></a>xstarₛ <span class="op">=</span> <span class="fu">solve</span>(gradfₛ, [x,y])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="44">
<pre><code>Dict{Any, Any} with 2 entries:
x =&gt; x1/3 + x2/3 + x3/3
y =&gt; y1/3 + y2/3 + y3/3</code></pre>
</div>
</div>
<p>There is only one critical point, so must be a minimum.</p>
<p>We confirm this by looking at the Hessian and noting <span class="math inline">\(H_{11} &gt; 0\)</span>:</p>
<div class="cell" data-execution_count="44">
<div class="sourceCode cell-code" id="cb69"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb69-1"><a href="#cb69-1" aria-hidden="true" tabindex="-1"></a>Hₛ <span class="op">=</span> <span class="fu">subs</span>.(<span class="fu">hessian</span>(exₛ, [x,y]), x<span class="op">=&gt;</span>xstarₛ[x], y<span class="op">=&gt;</span>xstarₛ[y])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="45">
<pre><code>2×2 Matrix{Sym}:
6 0
0 6</code></pre>
</div>
</div>
<p>As it occurs at <span class="math inline">\((\bar{x}, \bar{y})\)</span> where <span class="math inline">\(\bar{x} = (x_1 + x_2 + x_3)/3\)</span> and <span class="math inline">\(\bar{y} = (y_1+y_2+y_3)/3\)</span> - the averages of the three values - the critical point is an interior point of the triangle.</p>
<p>As mentioned by Strang, the real problem is to minimize <span class="math inline">\(d_1 + d_2 + d_3\)</span>. A direct approach with <code>SymPy</code> - just replacing <code>d2</code> above with the square root` fails. Consider instead the gradient of <span class="math inline">\(d_1\)</span>, say. To avoid square roots, this is taken implicitly from <span class="math inline">\(d_1^2\)</span>:</p>
<p><span class="math display">\[
\frac{\partial}{\partial{x}}(d_1^2) = 2 d_1 \frac{\partial{d_1}}{\partial{x}}.
\]</span></p>
<p>But computing directly from the expression yields <span class="math inline">\(2(x - x_1)\)</span> Solving, yields:</p>
<p><span class="math display">\[
\frac{\partial{d_1}}{\partial{x}} = \frac{(x-x_1)}{d_1}, \quad
\frac{\partial{d_1}}{\partial{y}} = \frac{(y-y_1)}{d_1}.
\]</span></p>
<p>The gradient is then <span class="math inline">\((\vec{p} - \vec{x}_1)/\|\vec{p} - \vec{x}_1\|\)</span>, a <em>unit</em> vector, call it <span class="math inline">\(\hat{u}_1\)</span>. Similarly for <span class="math inline">\(\hat{u}_2\)</span> and <span class="math inline">\(\hat{u}_3\)</span>.</p>
<p>Let <span class="math inline">\(f = d_1 + d_2 + d_3\)</span>. Then <span class="math inline">\(\nabla{f} = \hat{u}_1 + \hat{u}_2 + \hat{u}_3\)</span>. At the minimum, the gradient is <span class="math inline">\(\vec{0}\)</span>, so the three unit vectors must cancel. This can only happen if the three make a “peace” sign with angles <span class="math inline">\(120^\circ\)</span> between them. To find the minimum then within the triangle, this point and the boundary must be considered, when this point falls outside the triangle.</p>
<p>Here is a triangle, where the minimum would be within the triangle:</p>
<div class="cell" data-execution_count="45">
<div class="sourceCode cell-code" id="cb71"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb71-1"><a href="#cb71-1" aria-hidden="true" tabindex="-1"></a>usₛ <span class="op">=</span> [[<span class="fu">cos</span>(t), <span class="fu">sin</span>(t)] for t <span class="kw">in</span> (<span class="fl">0</span>, <span class="fl">2</span>pi<span class="op">/</span><span class="fl">3</span>, <span class="fl">4</span>pi<span class="op">/</span><span class="fl">3</span>)]</span>
<span id="cb71-2"><a href="#cb71-2" aria-hidden="true" tabindex="-1"></a><span class="fu">polygon</span>(ps) <span class="op">=</span> <span class="fu">unzip</span>(<span class="fu">vcat</span>(ps, ps[<span class="fl">1</span><span class="op">:</span><span class="fl">1</span>])) <span class="co"># easier way to plot a polygon</span></span>
<span id="cb71-3"><a href="#cb71-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb71-4"><a href="#cb71-4" aria-hidden="true" tabindex="-1"></a>pₛ <span class="op">=</span> <span class="fu">scatter</span>([<span class="fl">0</span>],[<span class="fl">0</span>], markersize<span class="op">=</span><span class="fl">2</span>, legend<span class="op">=</span><span class="cn">false</span>, aspect_ratio<span class="op">=:</span>equal)</span>
<span id="cb71-5"><a href="#cb71-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb71-6"><a href="#cb71-6" aria-hidden="true" tabindex="-1"></a>asₛ <span class="op">=</span> (<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">3</span>)</span>
<span id="cb71-7"><a href="#cb71-7" aria-hidden="true" tabindex="-1"></a><span class="fu">plot!</span>(<span class="fu">polygon</span>([a<span class="op">*</span>u for (a,u) <span class="kw">in</span> <span class="fu">zip</span>(asₛ, usₛ)])<span class="op">...</span>)</span>
<span id="cb71-8"><a href="#cb71-8" aria-hidden="true" tabindex="-1"></a>[<span class="fu">arrow!</span>([<span class="fl">0</span>,<span class="fl">0</span>], a<span class="op">*</span>u, alpha<span class="op">=</span><span class="fl">0.5</span>) for (a,u) <span class="kw">in</span> <span class="fu">zip</span>(asₛ, usₛ)]</span>
<span id="cb71-9"><a href="#cb71-9" aria-hidden="true" tabindex="-1"></a>pₛ</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="46">
<p><img src="scalar_functions_applications_files/figure-html/cell-46-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>For this triangle we find the Steiner point outside of the triangle.</p>
<div class="cell" data-execution_count="46">
<div class="sourceCode cell-code" id="cb72"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb72-1"><a href="#cb72-1" aria-hidden="true" tabindex="-1"></a>asₛ₁ <span class="op">=</span> (<span class="fl">1</span>, <span class="op">-</span><span class="fl">1</span>, <span class="fl">3</span>)</span>
<span id="cb72-2"><a href="#cb72-2" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter</span>([<span class="fl">0</span>],[<span class="fl">0</span>], markersize<span class="op">=</span><span class="fl">2</span>, legend<span class="op">=</span><span class="cn">false</span>)</span>
<span id="cb72-3"><a href="#cb72-3" aria-hidden="true" tabindex="-1"></a>psₛₗ <span class="op">=</span> [a<span class="op">*</span>u for (a,u) <span class="kw">in</span> <span class="fu">zip</span>(asₛ₁, usₛ)]</span>
<span id="cb72-4"><a href="#cb72-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plot!</span>(<span class="fu">polygon</span>(psₛₗ)<span class="op">...</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="47">
<p><img src="scalar_functions_applications_files/figure-html/cell-47-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>Lets see where the minimum distance point is by constructing a plot. The minimum must be on the boundary, as the only point where the gradient vanishes is the origin, not in the triangle. The plot of the triangle has a contour plot of the distance function, so we see clearly that the minimum happens at the point <code>[0.5, -0.866025]</code>. On this plot, we drew the gradient at some points along the boundary. The gradient points in the direction of greatest increase - away from the minimum. That the gradient vectors have a non-zero projection onto the edges of the triangle in a direction pointing away from the point indicates that the function <code>d</code> would increase if moved along the boundary in that direction, as indeed it does.</p>
<div class="cell" data-execution_count="47">
<div class="sourceCode cell-code" id="cb73"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb73-1"><a href="#cb73-1" aria-hidden="true" tabindex="-1"></a><span class="fu">euclid_dist</span>(x; ps<span class="op">=</span>psₛₗ) <span class="op">=</span> <span class="fu">sum</span>(<span class="fu">norm</span>(x<span class="op">-</span>p) <span class="cf">for</span> p <span class="kw">in</span> ps)</span>
<span id="cb73-2"><a href="#cb73-2" aria-hidden="true" tabindex="-1"></a><span class="fu">euclid_dist</span>(x,y; ps<span class="op">=</span>psₛₗ) <span class="op">=</span> <span class="fu">euclid_dist</span>([x,y]; ps<span class="op">=</span>ps)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="48">
<pre><code>euclid_dist (generic function with 2 methods)</code></pre>
</div>
</div>
<div class="cell" data-hold="true" data-execution_count="48">
<div class="sourceCode cell-code" id="cb75"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb75-1"><a href="#cb75-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">1.5</span>, <span class="fl">1.5</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb75-2"><a href="#cb75-2" aria-hidden="true" tabindex="-1"></a>ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">3</span>, <span class="fl">1.0</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb75-3"><a href="#cb75-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb75-4"><a href="#cb75-4" aria-hidden="true" tabindex="-1"></a>p <span class="op">=</span> <span class="fu">plot</span>(<span class="fu">polygon</span>(psₛₗ)<span class="op">...</span>, linewidth<span class="op">=</span><span class="fl">3</span>, legend<span class="op">=</span><span class="cn">false</span>)</span>
<span id="cb75-5"><a href="#cb75-5" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter!</span>(p, <span class="fu">unzip</span>(psₛₗ)<span class="op">...</span>, markersize<span class="op">=</span><span class="fl">3</span>)</span>
<span id="cb75-6"><a href="#cb75-6" aria-hidden="true" tabindex="-1"></a><span class="fu">contour!</span>(p, xs, ys, euclid_dist)</span>
<span id="cb75-7"><a href="#cb75-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb75-8"><a href="#cb75-8" aria-hidden="true" tabindex="-1"></a><span class="co"># add some gradients along boundary</span></span>
<span id="cb75-9"><a href="#cb75-9" aria-hidden="true" tabindex="-1"></a><span class="fu">li</span>(t, p1, p2) <span class="op">=</span> p1 <span class="op">+</span> <span class="fu">t*</span>(p2<span class="op">-</span>p1) <span class="co"># t in [0,1]</span></span>
<span id="cb75-10"><a href="#cb75-10" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> t <span class="kw">in</span> <span class="fu">range</span>(<span class="fl">1</span><span class="op">/</span><span class="fl">100</span>, <span class="fl">1</span><span class="op">/</span><span class="fl">2</span>, length<span class="op">=</span><span class="fl">3</span>)</span>
<span id="cb75-11"><a href="#cb75-11" aria-hidden="true" tabindex="-1"></a> pt <span class="op">=</span> <span class="fu">li</span>(t, psₛₗ[<span class="fl">2</span>], psₛₗ[<span class="fl">3</span>])</span>
<span id="cb75-12"><a href="#cb75-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrow!</span>(pt, ForwardDiff.<span class="fu">gradient</span>(euclid_dist, pt))</span>
<span id="cb75-13"><a href="#cb75-13" aria-hidden="true" tabindex="-1"></a> pt <span class="op">=</span> <span class="fu">li</span>(t, psₛₗ[<span class="fl">2</span>], psₛₗ[<span class="fl">1</span>])</span>
<span id="cb75-14"><a href="#cb75-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrow!</span>(pt, ForwardDiff.<span class="fu">gradient</span>(euclid_dist, pt))</span>
<span id="cb75-15"><a href="#cb75-15" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb75-16"><a href="#cb75-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb75-17"><a href="#cb75-17" aria-hidden="true" tabindex="-1"></a>p</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="49">
<p><img src="scalar_functions_applications_files/figure-html/cell-49-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>The following graph, shows distance along each edge:</p>
<div class="cell" data-hold="true" data-execution_count="49">
<div class="sourceCode cell-code" id="cb76"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb76-1"><a href="#cb76-1" aria-hidden="true" tabindex="-1"></a><span class="fu">li</span>(t, p1, p2) <span class="op">=</span> p1 <span class="op">+</span> <span class="fu">t*</span>(p2<span class="op">-</span>p1)</span>
<span id="cb76-2"><a href="#cb76-2" aria-hidden="true" tabindex="-1"></a>p <span class="op">=</span> <span class="fu">plot</span>(legend<span class="op">=</span><span class="cn">false</span>)</span>
<span id="cb76-3"><a href="#cb76-3" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i <span class="kw">in</span> <span class="fl">1</span><span class="op">:</span><span class="fl">2</span>, j <span class="kw">in</span> (i<span class="op">+</span><span class="fl">1</span>)<span class="op">:</span><span class="fl">3</span></span>
<span id="cb76-4"><a href="#cb76-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">plot!</span>(p, t <span class="op">-&gt;</span> <span class="fu">euclid_dist</span>(<span class="fu">li</span>(t, psₛₗ[i], psₛₗ[j]); ps<span class="op">=</span>psₛₗ), <span class="fl">0</span>, <span class="fl">1</span>)</span>
<span id="cb76-5"><a href="#cb76-5" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb76-6"><a href="#cb76-6" aria-hidden="true" tabindex="-1"></a>p</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="50">
<p><img src="scalar_functions_applications_files/figure-html/cell-50-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>The smallest value is when <span class="math inline">\(t=0\)</span> or <span class="math inline">\(t=1\)</span>, so at one of the points, as <code>li</code> is defined above.</p>
</section>
<section id="example-least-squares" class="level5">
<h5 class="anchored" data-anchor-id="example-least-squares">Example: least squares</h5>
<p>We know that two points determine a line. What happens when there are more than two points? This is common in statistics where a bivariate data set (pairs of points <span class="math inline">\((x,y)\)</span>) are summarized through a linear model <span class="math inline">\(\mu_{y|x} = \alpha + \beta x\)</span>, That is the average value for <span class="math inline">\(y\)</span> given a particular <span class="math inline">\(x\)</span> value is given through the equation of a line. The data is used to identify what the slope and intercept are for this line. We consider a simple case - <span class="math inline">\(3\)</span> points. The case of <span class="math inline">\(n \geq 3\)</span> being similar.</p>
<p>We have a line <span class="math inline">\(l(x) = \alpha + \beta(x)\)</span> and three points <span class="math inline">\((x_1, y_1)\)</span>, <span class="math inline">\((x_2, y_2)\)</span>, and <span class="math inline">\((x_3, y_3)\)</span>. Unless these three points <em>happen</em> to be collinear, they cant possibly all lie on the same line. So to <em>approximate</em> a relationship by a line requires some inexactness. One measure of inexactness is the <em>vertical</em> distance to the line:</p>
<p><span class="math display">\[
d1(\alpha, \beta) = |y_1 - l(x_1)| + |y_2 - l(x_2)| + |y_3 - l(x_3)|.
\]</span></p>
<p>Another might be the vertical squared distance to the line:</p>
<p><span class="math display">\[
\begin{align*}
d2(\alpha, \beta) &amp;= (y_1 - l(x_1))^2 + (y_2 - l(x_2))^2 + (y_3 - l(x_3))^2 \\
&amp;= (y1 - (\alpha + \beta x_1))^2 + (y3 - (\alpha + \beta x_3))^2 + (y3 - (\alpha + \beta x_3))^2
\end{align*}
\]</span></p>
<p>Another might be the <em>shortest</em> distance to the line:</p>
<p><span class="math display">\[
d3(\alpha, \beta) = \frac{\beta x_1 - y_1 + \alpha}{\sqrt{1 + \beta^2}} + \frac{\beta x_2 - y_2 + \alpha}{\sqrt{1 + \beta^2}} + \frac{\beta x_3 - y_3 + \alpha}{\sqrt{1 + \beta^2}}.
\]</span></p>
<p>The method of least squares minimizes the second one of these. That is, it chooses <span class="math inline">\(\alpha\)</span> and <span class="math inline">\(\beta\)</span> that make the expression a minimum.</p>
<div class="cell" data-execution_count="50">
<div class="sourceCode cell-code" id="cb77"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb77-1"><a href="#cb77-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> xₗₛ[<span class="fl">1</span><span class="op">:</span><span class="fl">3</span>] yₗₛ[<span class="fl">1</span><span class="op">:</span><span class="fl">3</span>] α β</span>
<span id="cb77-2"><a href="#cb77-2" aria-hidden="true" tabindex="-1"></a><span class="fu">li</span>(x, alpha, beta) <span class="op">=</span> alpha <span class="op">+</span> beta <span class="op">*</span> x</span>
<span id="cb77-3"><a href="#cb77-3" aria-hidden="true" tabindex="-1"></a><span class="fu">d₂</span>(alpha, beta) <span class="op">=</span> <span class="fu">sum</span>((y <span class="op">-</span> <span class="fu">li</span>(x, alpha, beta))<span class="op">^</span><span class="fl">2</span> <span class="cf">for</span> (y,x) <span class="kw">in</span> <span class="fu">zip</span>(yₗₛ, xₗₛ))</span>
<span id="cb77-4"><a href="#cb77-4" aria-hidden="true" tabindex="-1"></a><span class="fu">d₂</span>(α, β)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="51">
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
\[
\left(- xₗₛ₁ β + yₗₛ₁ - α\right)^{2} + \left(- xₗₛ₂ β + yₗₛ₂ - α\right)^{2} + \left(- xₗₛ₃ β + yₗₛ₃ - α\right)^{2}
\]
</span>
</div>
</div>
<p>To identify <span class="math inline">\(\alpha\)</span> and <span class="math inline">\(\beta\)</span> we find the gradient:</p>
<div class="cell" data-execution_count="51">
<div class="sourceCode cell-code" id="cb78"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb78-1"><a href="#cb78-1" aria-hidden="true" tabindex="-1"></a>grad_d₂ <span class="op">=</span> <span class="fu">diff</span>.(<span class="fu">d₂</span>(α, β), [α, β])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="52">
<pre><code>2-element Vector{Sym}:
2⋅xₗₛ₁⋅β + 2⋅xₗₛ₂⋅β + 2⋅xₗₛ₃⋅β - 2⋅yₗₛ₁ - 2⋅yₗₛ₂ - 2⋅yₗₛ₃ + 6⋅α
-2*xₗₛ₁*(-xₗₛ₁*β + yₗₛ₁ - α) - 2*xₗₛ₂*(-xₗₛ₂*β + yₗₛ₂ - α) - 2*xₗₛ₃*(-xₗₛ₃*β + yₗₛ₃ - α)</code></pre>
</div>
</div>
<div class="cell" data-execution_count="52">
<div class="sourceCode cell-code" id="cb80"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb80-1"><a href="#cb80-1" aria-hidden="true" tabindex="-1"></a>outₗₛ <span class="op">=</span> <span class="fu">solve</span>(grad_d₂, [α, β])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="53">
<pre><code>Dict{Any, Any} with 2 entries:
β =&gt; (2*xₗₛ₁*yₗₛ₁ - xₗₛ₁*yₗₛ₂ - xₗₛ₁*yₗₛ₃ - xₗₛ₂*yₗₛ₁ + 2*xₗₛ₂*yₗₛ₂ - xₗₛ₂*yₗ…
α =&gt; (xₗₛ₁^2*yₗₛ₂ + xₗₛ₁^2*yₗₛ₃ - xₗₛ₁*xₗₛ₂*yₗₛ₁ - xₗₛ₁*xₗₛ₂*yₗₛ₂ - xₗₛ₁*xₗₛ₃…</code></pre>
</div>
</div>
<p>As found, the formulas arent pretty. If <span class="math inline">\(x_1 + x_2 + x_3 = 0\)</span> they simplify. For example:</p>
<div class="cell" data-execution_count="53">
<div class="sourceCode cell-code" id="cb82"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb82-1"><a href="#cb82-1" aria-hidden="true" tabindex="-1"></a><span class="fu">subs</span>(outₗₛ[β], <span class="fu">sum</span>(xₗₛ) <span class="op">=&gt;</span> <span class="fl">0</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="54">
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
\[
\frac{2 xₗₛ₁ yₗₛ₁ - xₗₛ₁ yₗₛ₂ - xₗₛ₁ yₗₛ₃ - xₗₛ₂ yₗₛ₁ + 2 xₗₛ₂ yₗₛ₂ - xₗₛ₂ yₗₛ₃ - xₗₛ₃ yₗₛ₁ - xₗₛ₃ yₗₛ₂ + 2 xₗₛ₃ yₗₛ₃}{2 xₗₛ₁^{2} - 2 xₗₛ₁ xₗₛ₂ - 2 xₗₛ₁ xₗₛ₃ + 2 xₗₛ₂^{2} - 2 xₗₛ₂ xₗₛ₃ + 2 xₗₛ₃^{2}}
\]
</span>
</div>
</div>
<p>Let <span class="math inline">\(\vec{x} = \langle x_1, x_2, x_3 \rangle\)</span> and <span class="math inline">\(\vec{y} = \langle y_1, y_2, y_3 \rangle\)</span> this is simply <span class="math inline">\((\vec{x} \cdot \vec{y})/(\vec{x}\cdot \vec{x})\)</span>, a formula that will generalize to <span class="math inline">\(n &gt; 3\)</span>. The assumption is not a restriction - it comes about by subtracting the mean, <span class="math inline">\(\bar{x} = (x_1 + x_2 + x_3)/3\)</span>, from each <span class="math inline">\(x\)</span> term (and similarly subtract <span class="math inline">\(\bar{y}\)</span> from each <span class="math inline">\(y\)</span> term). A process called “centering.”</p>
<p>With this observation, the formulas can be re-expressed through:</p>
<p><span class="math display">\[
\beta = \frac{\sum{x_i - \bar{x}}(y_i - \bar{y})}{\sum(x_i-\bar{x})^2},
\quad
\alpha = \bar{y} - \beta \bar{x}.
\]</span></p>
<p>Relative to the centered values, this may be viewed as a line through <span class="math inline">\((\bar{x}, \bar{y})\)</span> with slope given by <span class="math inline">\((\vec{x}-\bar{x})\cdot(\vec{y}-\bar{y}) / \|\vec{x}-\bar{x}\|\)</span>.</p>
<p>As an example, if the point are <span class="math inline">\((1,1), (2,3), (5,8)\)</span> we get:</p>
<div class="cell" data-execution_count="54">
<div class="sourceCode cell-code" id="cb83"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb83-1"><a href="#cb83-1" aria-hidden="true" tabindex="-1"></a>[k <span class="op">=&gt;</span> <span class="fu">subs</span>(v, xₗₛ[<span class="fl">1</span>]<span class="op">=&gt;</span><span class="fl">1</span>, yₗₛ[<span class="fl">1</span>]<span class="op">=&gt;</span><span class="fl">1</span>, xₗₛ[<span class="fl">2</span>]<span class="op">=&gt;</span><span class="fl">2</span>, yₗₛ[<span class="fl">2</span>]<span class="op">=&gt;</span><span class="fl">3</span>,</span>
<span id="cb83-2"><a href="#cb83-2" aria-hidden="true" tabindex="-1"></a> xₗₛ[<span class="fl">3</span>]<span class="op">=&gt;</span><span class="fl">5</span>, yₗₛ[<span class="fl">3</span>]<span class="op">=&gt;</span><span class="fl">8</span>) for (k,v) <span class="kw">in</span> outₗₛ]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="55">
<pre><code>2-element Vector{Pair{Sym, Sym}}:
β =&gt; 45/26
α =&gt; -8/13</code></pre>
</div>
</div>
</section>
<section id="gradient-descent" class="level3" data-number="56.4.1">
<h3 data-number="56.4.1" class="anchored" data-anchor-id="gradient-descent"><span class="header-section-number">56.4.1</span> Gradient descent</h3>
<p>As seen in the examples above, extrema may be identified analytically by solving for when the gradient is <span class="math inline">\(0\)</span>. Here we discuss some numeric algorithms for finding extrema.</p>
<p>An algorithm to identify where a surface is at its minimum is <a href="https://en.wikipedia.org/wiki/Gradient_descent">gradient descent</a>. The gradient points in the direction of the steepest ascent of the surface and the negative gradient the direction of the steepest descent. To move to a minimum then, it make intuitive sense to move in the direction of the negative gradient. How far? That is a different question and one with different answers. Lets formulate the movement first, then discuss how far.</p>
<p>Let <span class="math inline">\(\vec{x}_0\)</span>, <span class="math inline">\(\vec{x}_1\)</span>, <span class="math inline">\(\dots\)</span>, <span class="math inline">\(\vec{x}_n\)</span> be the position of the algorithm for <span class="math inline">\(n\)</span> steps starting from an initial point <span class="math inline">\(\vec{x}_0\)</span>. The difference between these points is given by:</p>
<p><span class="math display">\[
\vec{x}_{n+1} = \vec{x}_n - \gamma \nabla{f}(\vec{x}_n),
\]</span></p>
<p>where <span class="math inline">\(\gamma\)</span> is some scaling factor for the gradient. The above quantifies the idea: to go from <span class="math inline">\(\vec{x}_n\)</span> to <span class="math inline">\(\vec{x}_{n+1}\)</span>, move along <span class="math inline">\(-\nabla{f}\)</span> by a certain amount.</p>
<p>Let <span class="math inline">\(\Delta_x =\vec{x}_{n}- \vec{x}_{n-1}\)</span> and <span class="math inline">\(\Delta_y = \nabla{f}(\vec{x}_{n}) - \nabla{f}(\vec{x}_{n-1})\)</span> A variant of the Barzilai-Borwein method is to take <span class="math inline">\(\gamma_n = | \Delta_x \cdot \Delta_y / \Delta_y \cdot \Delta_y |\)</span>.</p>
<p>To illustrate, take <span class="math inline">\(f(x,y) = -(x^2 + y^2) \cdot e^{-(2x^2 + y^2)}\)</span> and a starting point <span class="math inline">\(\langle 1, 1 \rangle\)</span>. We have, starting with <span class="math inline">\(\gamma_0 = 1\)</span> there are <span class="math inline">\(5\)</span> steps taken:</p>
<div class="cell" data-execution_count="55">
<div class="sourceCode cell-code" id="cb85"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb85-1"><a href="#cb85-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f₂</span>(x,y) <span class="op">=</span> <span class="fu">-exp</span>(<span class="fu">-</span>((x<span class="op">-</span><span class="fl">1</span>)<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> <span class="fl">2</span>(y<span class="op">-</span><span class="fl">1</span><span class="op">/</span><span class="fl">2</span>)<span class="op">^</span><span class="fl">2</span>))</span>
<span id="cb85-2"><a href="#cb85-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f₂</span>(x) <span class="op">=</span> <span class="fu">f₂</span>(x<span class="op">...</span>)</span>
<span id="cb85-3"><a href="#cb85-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb85-4"><a href="#cb85-4" aria-hidden="true" tabindex="-1"></a>xs₂ <span class="op">=</span> [[<span class="fl">0.0</span>, <span class="fl">0.0</span>]] <span class="co"># we store a vector</span></span>
<span id="cb85-5"><a href="#cb85-5" aria-hidden="true" tabindex="-1"></a>gammas₂ <span class="op">=</span> [<span class="fl">1.0</span>]</span>
<span id="cb85-6"><a href="#cb85-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb85-7"><a href="#cb85-7" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> n <span class="kw">in</span> <span class="fl">1</span><span class="op">:</span><span class="fl">5</span></span>
<span id="cb85-8"><a href="#cb85-8" aria-hidden="true" tabindex="-1"></a> xn <span class="op">=</span> xs₂[<span class="kw">end</span>]</span>
<span id="cb85-9"><a href="#cb85-9" aria-hidden="true" tabindex="-1"></a> gamma₀ <span class="op">=</span> gammas₂[<span class="kw">end</span>]</span>
<span id="cb85-10"><a href="#cb85-10" aria-hidden="true" tabindex="-1"></a> xn1 <span class="op">=</span> xn <span class="op">-</span> gamma₀ <span class="op">*</span> <span class="fu">gradient</span>(f₂)(xn)</span>
<span id="cb85-11"><a href="#cb85-11" aria-hidden="true" tabindex="-1"></a> dx, dy <span class="op">=</span> xn1 <span class="op">-</span> xn, <span class="fu">gradient</span>(f₂)(xn1) <span class="op">-</span> <span class="fu">gradient</span>(f₂)(xn)</span>
<span id="cb85-12"><a href="#cb85-12" aria-hidden="true" tabindex="-1"></a> gamman1 <span class="op">=</span> <span class="fu">abs</span>( (dx <span class="op"></span> dy) <span class="op">/</span> (dy <span class="op"></span> dy) )</span>
<span id="cb85-13"><a href="#cb85-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb85-14"><a href="#cb85-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">push!</span>(xs₂, xn1)</span>
<span id="cb85-15"><a href="#cb85-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">push!</span>(gammas₂, gamman1)</span>
<span id="cb85-16"><a href="#cb85-16" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb85-17"><a href="#cb85-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb85-18"><a href="#cb85-18" aria-hidden="true" tabindex="-1"></a>[(x, <span class="fu">f₂</span>(x)) for x <span class="kw">in</span> xs₂]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="56">
<pre><code>6-element Vector{Tuple{Vector{Float64}, Float64}}:
([0.0, 0.0], -0.22313016014842982)
([0.44626032029685964, 0.44626032029685964], -0.7316862045596354)
([0.5719399641782019, 0.4706543959065717], -0.8311394210020312)
([1.3127598757955443, 0.5722280351701136], -0.8974009578884475)
([0.9982224839581173, 0.4269509740243237], -0.9893813007474934)
([0.9996828943781475, 0.5469853998120562], -0.9955943772073014)</code></pre>
</div>
</div>
<p>We now visualize, using the <code>Contour</code> package to draw the contour lines in the <span class="math inline">\(x-y\)</span> plane:</p>
<div class="cell" data-hold="true" data-execution_count="56">
<div class="sourceCode cell-code" id="cb87"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb87-1"><a href="#cb87-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">surface_contour</span>(xs, ys, f; offset<span class="op">=</span><span class="fl">0</span>)</span>
<span id="cb87-2"><a href="#cb87-2" aria-hidden="true" tabindex="-1"></a> p <span class="op">=</span> <span class="fu">surface</span>(xs, ys, f, legend<span class="op">=</span><span class="cn">false</span>, fillalpha<span class="op">=</span><span class="fl">0.5</span>)</span>
<span id="cb87-3"><a href="#cb87-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb87-4"><a href="#cb87-4" aria-hidden="true" tabindex="-1"></a> <span class="co">## we add to the graphic p, then plot</span></span>
<span id="cb87-5"><a href="#cb87-5" aria-hidden="true" tabindex="-1"></a> zs <span class="op">=</span> [<span class="fu">f</span>(x,y) for x <span class="kw">in</span> xs, y <span class="kw">in</span> ys] <span class="co"># reverse order for use with Contour package</span></span>
<span id="cb87-6"><a href="#cb87-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> cl <span class="kw">in</span> <span class="fu">levels</span>(<span class="fu">contours</span>(xs, ys, zs))</span>
<span id="cb87-7"><a href="#cb87-7" aria-hidden="true" tabindex="-1"></a> lvl <span class="op">=</span> <span class="fu">level</span>(cl) <span class="co"># the z-value of this contour level</span></span>
<span id="cb87-8"><a href="#cb87-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> line <span class="kw">in</span> <span class="fu">lines</span>(cl)</span>
<span id="cb87-9"><a href="#cb87-9" aria-hidden="true" tabindex="-1"></a> _xs, _ys <span class="op">=</span> <span class="fu">coordinates</span>(line) <span class="co"># coordinates of this line segment</span></span>
<span id="cb87-10"><a href="#cb87-10" aria-hidden="true" tabindex="-1"></a> _zs <span class="op">=</span> offset <span class="op">*</span> _xs</span>
<span id="cb87-11"><a href="#cb87-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">plot!</span>(p, _xs, _ys, _zs, alpha<span class="op">=</span><span class="fl">0.5</span>) <span class="co"># add curve on x-y plane</span></span>
<span id="cb87-12"><a href="#cb87-12" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb87-13"><a href="#cb87-13" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb87-14"><a href="#cb87-14" aria-hidden="true" tabindex="-1"></a> p</span>
<span id="cb87-15"><a href="#cb87-15" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span>
<span id="cb87-16"><a href="#cb87-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb87-17"><a href="#cb87-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb87-18"><a href="#cb87-18" aria-hidden="true" tabindex="-1"></a>offset <span class="op">=</span> <span class="fl">0</span></span>
<span id="cb87-19"><a href="#cb87-19" aria-hidden="true" tabindex="-1"></a>us <span class="op">=</span> vs <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">1</span>, <span class="fl">2</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb87-20"><a href="#cb87-20" aria-hidden="true" tabindex="-1"></a><span class="fu">surface_contour</span>(vs, vs, f₂, offset<span class="op">=</span>offset)</span>
<span id="cb87-21"><a href="#cb87-21" aria-hidden="true" tabindex="-1"></a>pts <span class="op">=</span> [[pt<span class="op">...</span>, offset] for pt <span class="kw">in</span> xs₂]</span>
<span id="cb87-22"><a href="#cb87-22" aria-hidden="true" tabindex="-1"></a><span class="fu">scatter!</span>(<span class="fu">unzip</span>(pts)<span class="op">...</span>)</span>
<span id="cb87-23"><a href="#cb87-23" aria-hidden="true" tabindex="-1"></a><span class="fu">plot!</span>(<span class="fu">unzip</span>(pts)<span class="op">...</span>, linewidth<span class="op">=</span><span class="fl">3</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="57">
<p><img src="scalar_functions_applications_files/figure-html/cell-57-output-1.svg" class="img-fluid"></p>
</div>
</div>
</section>
<section id="newtons-method-for-minimization" class="level3" data-number="56.4.2">
<h3 data-number="56.4.2" class="anchored" data-anchor-id="newtons-method-for-minimization"><span class="header-section-number">56.4.2</span> Newtons method for minimization</h3>
<p>A variant of Newtons method can be used to minimize a function <span class="math inline">\(f:R^2 \rightarrow R\)</span>. We look for points where both partial derivatives of <span class="math inline">\(f\)</span> vanish. Let <span class="math inline">\(g(x,y) = \partial f/\partial x(x,y)\)</span> and <span class="math inline">\(h(x,y) = \partial f/\partial y(x,y)\)</span>. Then applying Newtons method, as above to solve simultaneously for when <span class="math inline">\(g=0\)</span> and <span class="math inline">\(h=0\)</span>, we considered this matrix:</p>
<p><span class="math display">\[
M = [\nabla{g}'; \nabla{h}'],
\]</span></p>
<p>and had a step expressible in terms of the inverse of <span class="math inline">\(M\)</span> as <span class="math inline">\(M^{-1} [g; h]\)</span>. In terms of the function <span class="math inline">\(f\)</span>, this step is <span class="math inline">\(H^{-1}\nabla{f}\)</span>, where <span class="math inline">\(H\)</span> is the Hessian matrix. <a href="https://en.wikipedia.org/wiki/Newton%27s_method_in_optimization#Higher_dimensions">Newton</a>s method then becomes:</p>
<p><span class="math display">\[
\vec{x}_{n+1} = \vec{x}_n - [H_f(\vec{x}_n]^{-1} \nabla(f)(\vec{x}_n).
\]</span></p>
<p>The Wikipedia page states where applicable, Newtons method converges much faster towards a local maximum or minimum than gradient descent.</p>
<p>We apply it to the task of characterizing the following function, which has a few different peaks over the region <span class="math inline">\([-3,3] \times [-2,2]\)</span>:</p>
<div class="cell" data-execution_count="57">
<div class="sourceCode cell-code" id="cb88"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb88-1"><a href="#cb88-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">peaks</span>(x, y)</span>
<span id="cb88-2"><a href="#cb88-2" aria-hidden="true" tabindex="-1"></a> z <span class="op">=</span> <span class="fl">3</span> <span class="op">*</span> (<span class="fl">1</span> <span class="op">-</span> x)<span class="op">^</span><span class="fl">2</span> <span class="op">*</span> <span class="fu">exp</span>(<span class="op">-</span>x<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> (y <span class="op">+</span> <span class="fl">1</span>)<span class="op">^</span><span class="fl">2</span>)</span>
<span id="cb88-3"><a href="#cb88-3" aria-hidden="true" tabindex="-1"></a> z <span class="op">+=</span> <span class="op">-</span><span class="fl">10</span> <span class="op">*</span> (x <span class="op">/</span> <span class="fl">5</span> <span class="op">-</span> x<span class="op">^</span><span class="fl">3</span> <span class="op">-</span> y<span class="op">^</span><span class="fl">5</span>) <span class="op">*</span> <span class="fu">exp</span>(<span class="op">-</span>x<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> y<span class="op">^</span><span class="fl">2</span>)</span>
<span id="cb88-4"><a href="#cb88-4" aria-hidden="true" tabindex="-1"></a> z <span class="op">+=</span> <span class="op">-</span><span class="fl">1</span><span class="op">/</span><span class="fl">3</span> <span class="op">*</span> <span class="fu">exp</span>(<span class="fu">-</span>(x<span class="op">+</span><span class="fl">1</span>)<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> y<span class="op">^</span><span class="fl">2</span>)</span>
<span id="cb88-5"><a href="#cb88-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> z</span>
<span id="cb88-6"><a href="#cb88-6" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span>
<span id="cb88-7"><a href="#cb88-7" aria-hidden="true" tabindex="-1"></a><span class="fu">peaks</span>(v) <span class="op">=</span> <span class="fu">peaks</span>(v<span class="op">...</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="58">
<pre><code>peaks (generic function with 2 methods)</code></pre>
</div>
</div>
<div class="cell" data-hold="true" data-execution_count="58">
<div class="sourceCode cell-code" id="cb90"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb90-1"><a href="#cb90-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">3</span>, stop<span class="op">=</span><span class="fl">3</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb90-2"><a href="#cb90-2" aria-hidden="true" tabindex="-1"></a>ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">2</span>, stop<span class="op">=</span><span class="fl">2</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb90-3"><a href="#cb90-3" aria-hidden="true" tabindex="-1"></a>Ps <span class="op">=</span> <span class="fu">surface</span>(xs, ys, peaks, legend<span class="op">=</span><span class="cn">false</span>)</span>
<span id="cb90-4"><a href="#cb90-4" aria-hidden="true" tabindex="-1"></a>Pc <span class="op">=</span> <span class="fu">contour</span>(xs, ys, peaks, legend<span class="op">=</span><span class="cn">false</span>)</span>
<span id="cb90-5"><a href="#cb90-5" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(Ps, Pc, layout<span class="op">=</span><span class="fl">2</span>) <span class="co"># combine plots</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="59">
<p><img src="scalar_functions_applications_files/figure-html/cell-59-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>As we will solve for the critical points numerically, we consider the contour plot as well, as it shows better where the critical points are.</p>
<p>Over this region we see clearly 5 peaks or valleys: near <span class="math inline">\((0, 1.5)\)</span>, near <span class="math inline">\((1.2, 0)\)</span>, near <span class="math inline">\((0.2, -1.8)\)</span>, near <span class="math inline">\((-0.5, -0.8)\)</span>, and near <span class="math inline">\((-1.2, 0.2)\)</span>. To classify the <span class="math inline">\(5\)</span> critical points we need to first identify them, then compute the Hessian, and then, possibly compute <span class="math inline">\(f_xx\)</span> at the point. Here we do so for one of them using a numeric approach.</p>
<p>For concreteness, consider the peak or valley near <span class="math inline">\((0,1.5)\)</span>. We use Newtons method to numerically compute the critical point. The Newton step, specialized here is:</p>
<div class="cell" data-execution_count="59">
<div class="sourceCode cell-code" id="cb91"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb91-1"><a href="#cb91-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">newton_stepₚ</span>(f, x)</span>
<span id="cb91-2"><a href="#cb91-2" aria-hidden="true" tabindex="-1"></a> M <span class="op">=</span> ForwardDiff.<span class="fu">hessian</span>(f, x)</span>
<span id="cb91-3"><a href="#cb91-3" aria-hidden="true" tabindex="-1"></a> b <span class="op">=</span> ForwardDiff.<span class="fu">gradient</span>(f, x)</span>
<span id="cb91-4"><a href="#cb91-4" aria-hidden="true" tabindex="-1"></a> x <span class="op">-</span> M <span class="op">\</span> b</span>
<span id="cb91-5"><a href="#cb91-5" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="60">
<pre><code>newton_stepₚ (generic function with 1 method)</code></pre>
</div>
</div>
<p>We perform <span class="math inline">\(3\)</span> steps of Newtons method, and see that it has found a critical point.</p>
<div class="cell" data-execution_count="60">
<div class="sourceCode cell-code" id="cb93"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb93-1"><a href="#cb93-1" aria-hidden="true" tabindex="-1"></a>xₚ <span class="op">=</span> [<span class="fl">0</span>, <span class="fl">1.5</span>]</span>
<span id="cb93-2"><a href="#cb93-2" aria-hidden="true" tabindex="-1"></a>xₚ <span class="op">=</span> <span class="fu">newton_stepₚ</span>(peaks, xₚ)</span>
<span id="cb93-3"><a href="#cb93-3" aria-hidden="true" tabindex="-1"></a>xₚ <span class="op">=</span> <span class="fu">newton_stepₚ</span>(peaks, xₚ)</span>
<span id="cb93-4"><a href="#cb93-4" aria-hidden="true" tabindex="-1"></a>xₚ <span class="op">=</span> <span class="fu">newton_stepₚ</span>(peaks, xₚ)</span>
<span id="cb93-5"><a href="#cb93-5" aria-hidden="true" tabindex="-1"></a>xₚ, ForwardDiff.<span class="fu">gradient</span>(peaks, xₚ)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="61">
<pre><code>([-0.009317581959954116, 1.5813679629389998], [1.734723475976807e-17, -6.9111383282915995e-15])</code></pre>
</div>
</div>
<p>The Hessian at this point is given by:</p>
<div class="cell" data-execution_count="61">
<div class="sourceCode cell-code" id="cb95"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb95-1"><a href="#cb95-1" aria-hidden="true" tabindex="-1"></a>Hₚ <span class="op">=</span> ForwardDiff.<span class="fu">hessian</span>(peaks, xₚ)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="62">
<pre><code>2×2 Matrix{Float64}:
-16.2944 0.493227
0.493227 -32.413</code></pre>
</div>
</div>
<p>From which we see:</p>
<div class="cell" data-hold="true" data-execution_count="62">
<div class="sourceCode cell-code" id="cb97"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb97-1"><a href="#cb97-1" aria-hidden="true" tabindex="-1"></a>fxx <span class="op">=</span> Hₚ[<span class="fl">1</span>,<span class="fl">1</span>]</span>
<span id="cb97-2"><a href="#cb97-2" aria-hidden="true" tabindex="-1"></a>d <span class="op">=</span> <span class="fu">det</span>(Hₚ)</span>
<span id="cb97-3"><a href="#cb97-3" aria-hidden="true" tabindex="-1"></a>fxx, d</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="63">
<pre><code>(-16.29442261058989, 527.9079596128478)</code></pre>
</div>
</div>
<p>Consequently we have a local maximum at this critical point.</p>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
</div>
</div>
<p>The <code>Optim.jl</code> package provides efficient implementations of these two numeric methods, and others.</p>
</section>
</section>
<section id="constrained-optimization-lagrange-multipliers" class="level2" data-number="56.5">
<h2 data-number="56.5" class="anchored" data-anchor-id="constrained-optimization-lagrange-multipliers"><span class="header-section-number">56.5</span> Constrained optimization, Lagrange multipliers</h2>
<p>We considered the problem of maximizing a function over a closed region. This maximum is achieved at a critical point <em>or</em> a boundary point. Investigating the critical points isnt so difficult and the second partial derivative test can help characterize the points along the way, but characterizing the boundary points usually involves parameterizing the boundary, which is not always so easy. However, if we put this problem into a more general setting a different technique becomes available.</p>
<p>The different setting is: maximize <span class="math inline">\(f(x,y)\)</span> subject to the constraint <span class="math inline">\(g(x,y) = k\)</span>. The constraint can be used to describe the boundary used previously.</p>
<p>Why does this help? The key is something we have seen prior: If <span class="math inline">\(g\)</span> is differentiable, and we take <span class="math inline">\(\nabla{g}\)</span>, then it will point at directions <em>orthogonal</em> to the level curve <span class="math inline">\(g(x,y) = 0\)</span>. (Parameterize the curve, then <span class="math inline">\((g\circ\vec{r})(t) = 0\)</span> and so the chain rule has <span class="math inline">\(\nabla{g}(\vec{r}(t)) \cdot \vec{r}'(t) = 0\)</span>.) For example, consider the function <span class="math inline">\(g(x,y) = x^2 +2y^2 - 1\)</span>. The level curve <span class="math inline">\(g(x,y) = 0\)</span> is an ellipse. Here we plot the level curve, along with a few gradient vectors at points satisfying <span class="math inline">\(g(x,y) = 0\)</span>:</p>
<div class="cell" data-hold="true" data-execution_count="63">
<div class="sourceCode cell-code" id="cb99"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb99-1"><a href="#cb99-1" aria-hidden="true" tabindex="-1"></a><span class="fu">g</span>(x,y) <span class="op">=</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> <span class="fl">2</span>y<span class="op">^</span><span class="fl">2</span> <span class="op">-</span><span class="fl">1</span></span>
<span id="cb99-2"><a href="#cb99-2" aria-hidden="true" tabindex="-1"></a><span class="fu">g</span>(v) <span class="op">=</span> <span class="fu">g</span>(v<span class="op">...</span>)</span>
<span id="cb99-3"><a href="#cb99-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb99-4"><a href="#cb99-4" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">3</span>, <span class="fl">3</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb99-5"><a href="#cb99-5" aria-hidden="true" tabindex="-1"></a>ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">1</span>, <span class="fl">4</span>, length<span class="op">=</span><span class="fl">100</span>)</span>
<span id="cb99-6"><a href="#cb99-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb99-7"><a href="#cb99-7" aria-hidden="true" tabindex="-1"></a>p <span class="op">=</span> <span class="fu">plot</span>(aspect_ratio<span class="op">=:</span>equal, legend<span class="op">=</span><span class="cn">false</span>)</span>
<span id="cb99-8"><a href="#cb99-8" aria-hidden="true" tabindex="-1"></a><span class="fu">contour!</span>(xs, ys, g, levels<span class="op">=</span>[<span class="fl">0</span>])</span>
<span id="cb99-9"><a href="#cb99-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb99-10"><a href="#cb99-10" aria-hidden="true" tabindex="-1"></a><span class="fu">gi</span>(x) <span class="op">=</span> <span class="fu">sqrt</span>(<span class="fl">1</span><span class="op">/</span><span class="fl">2</span><span class="fu">*</span>(<span class="fl">1</span><span class="op">-</span>x<span class="op">^</span><span class="fl">2</span>)) <span class="co"># solve for y in terms of x</span></span>
<span id="cb99-11"><a href="#cb99-11" aria-hidden="true" tabindex="-1"></a>pts <span class="op">=</span> [[x, <span class="fu">gi</span>(x)] for x <span class="kw">in</span> (<span class="op">-</span><span class="fl">3</span><span class="op">/</span><span class="fl">4</span>, <span class="op">-</span><span class="fl">1</span><span class="op">/</span><span class="fl">4</span>, <span class="fl">1</span><span class="op">/</span><span class="fl">4</span>, <span class="fl">3</span><span class="op">/</span><span class="fl">4</span>)]</span>
<span id="cb99-12"><a href="#cb99-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb99-13"><a href="#cb99-13" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> pt <span class="kw">in</span> pts</span>
<span id="cb99-14"><a href="#cb99-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrow!</span>(pt, ForwardDiff.<span class="fu">gradient</span>(g, pt) )</span>
<span id="cb99-15"><a href="#cb99-15" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb99-16"><a href="#cb99-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb99-17"><a href="#cb99-17" aria-hidden="true" tabindex="-1"></a>p</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="64">
<p><img src="scalar_functions_applications_files/figure-html/cell-64-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>From the plot we see the key property that <span class="math inline">\(g\)</span> is orthogonal to the level curve.</p>
<p>Now consider <span class="math inline">\(f(x,y)\)</span>, a function we wish to maximize. The gradient points in the direction of <em>greatest</em> increase, provided <span class="math inline">\(f\)</span> is smooth. We are interested in the value of this gradient along the level curve of <span class="math inline">\(g\)</span>. Consider this figure representing a portion of the level curve, its tangent, normal, the gradient of <span class="math inline">\(f\)</span>, and the contours of <span class="math inline">\(f\)</span>:</p>
<div class="cell" data-hold="true" data-execution_count="64">
<div class="cell-output cell-output-display" data-execution_count="65">
<p><img src="scalar_functions_applications_files/figure-html/cell-65-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>We can identify the tangent, the normal, and subsequently the gradient of <span class="math inline">\(f\)</span>. Is the point drawn a maximum of <span class="math inline">\(f\)</span> subject to the constraint <span class="math inline">\(g\)</span>?</p>
<p>The answer is no, but why? By adding the contours of <span class="math inline">\(f\)</span>, we see that moving along the curve from this point will increase or decrease <span class="math inline">\(f\)</span>, depending on which direction we move in. As the <em>gradient</em> is the direction of greatest increase, we can see that the <em>projection</em> of the gradient on the tangent will point in a direction of <em>increase</em>.</p>
<p>It isnt just because the point picked was chosen to make a pretty picture, and not be a maximum. Rather, the fact that <span class="math inline">\(\nabla{f}\)</span> has a non-trivial projection onto the tangent vector. What does it say if we move the point in the direction of this projection?</p>
<p>The gradient points in the direction of greatest increase. If we first move in one component of the gradient we will increase, just not as fast. This is because the directional derivative in the direction of the tangent will be non-zero. In the picture, if we were to move the point to the right along the curve <span class="math inline">\(f(x,y)\)</span> will increase.</p>
<p>Now consider this figure at a different point of the figure:</p>
<div class="cell" data-hold="true" data-execution_count="65">
<div class="cell-output cell-output-display" data-execution_count="66">
<p><img src="scalar_functions_applications_files/figure-html/cell-66-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>We can still identify the tangent and normal directions. What is different about this point is that local movement on the constraint curve is also local movement on the contour line of <span class="math inline">\(f\)</span>, so <span class="math inline">\(f\)</span> doesnt increase or decrease here, as it would if this point were an extrema along the contraint. The key to seeing this is the contour lines of <span class="math inline">\(f\)</span> are <em>tangent</em> to the constraint. The respective gradients are <em>orthogonal</em> to their tangent lines, and in dimension <span class="math inline">\(2\)</span>, this implies they are parallel to each other.</p>
<blockquote class="blockquote">
<p><em>The method of Lagrange multipliers</em>: To optimize <span class="math inline">\(f(x,y)\)</span> subject to a constraint <span class="math inline">\(g(x,y) = k\)</span> we solve for all <em>simultaneous</em> solutions to</p>
<p><span class="math display">\[
\begin{align}
\nabla{f}(x,y) &amp;= \lambda \nabla{g}(x,y), \text{and}\\
g(x,y) &amp;= k.
\end{align}
\]</span></p>
<p>These <em>possible</em> points are evaluated to see if they are maxima or minima.</p>
</blockquote>
<p>The method will not work if <span class="math inline">\(\nabla{g} = \vec{0}\)</span> or if <span class="math inline">\(f\)</span> and <span class="math inline">\(g\)</span> are not differentiable.</p>
<hr>
<section id="example-8" class="level5">
<h5 class="anchored" data-anchor-id="example-8">Example</h5>
<p>We consider <a href="%22../derivatives/optimization.html%22">again</a> the problem of maximizing all rectangles subject to the perimeter being <span class="math inline">\(20\)</span>. We have seen this results in a square. This time we use the Lagrange multiplier technique. We have two equations:</p>
<p><span class="math display">\[
A(x,y) = xy, \quad P(x,y) = 2x + 2y = 25.
\]</span></p>
<p>We see <span class="math inline">\(\nabla{A} = \lambda \nabla{P}\)</span>, or <span class="math inline">\(\langle y, x \rangle = \lambda \langle 2, 2\rangle\)</span>. We see the solution has <span class="math inline">\(x = y\)</span> and from the constraint <span class="math inline">\(x=y = 5\)</span>.</p>
<p>This is clearly the maximum for this problem, though the Lagrange technique does not imply that, it only identifies possible extrema.</p>
</section>
<section id="example-9" class="level5">
<h5 class="anchored" data-anchor-id="example-9">Example</h5>
<p>We can reverse the question: what are the ranges for the perimeter when the area is a fixed value of <span class="math inline">\(25\)</span>? We have:</p>
<p><span class="math display">\[
P(x,y) = 2x + 2y, \quad A(x,y) = xy = 25.
\]</span></p>
<p>Now we look for <span class="math inline">\(\nabla{P} = \lambda \nabla{A}\)</span> and will get, as the last example, that <span class="math inline">\(\langle 2, 2 \rangle = \lambda \langle y, x\rangle\)</span>. So <span class="math inline">\(x=y\)</span> and from the constraint <span class="math inline">\(x=y=5\)</span>.</p>
<p>However this is <em>not</em> the maximum perimeter, but rather the minimal perimeter. The maximum is <span class="math inline">\(\infty\)</span>, which comes about in the limit by considering long skinny rectangles.</p>
</section>
<section id="example-a-rephrasing" class="level5">
<h5 class="anchored" data-anchor-id="example-a-rephrasing">Example: A rephrasing</h5>
<p>An slightly different formulation of the Lagrange method is to combine the equation and the constraint into one equation:</p>
<p><span class="math display">\[
L(x,y,\lambda) = f(x,y) - \lambda (g(x,y) - k).
\]</span></p>
<p>The we have</p>
<p><span class="math display">\[
\begin{align}
\frac{\partial L}{\partial{x}} &amp;= \frac{\partial{f}}{\partial{x}} - \lambda \frac{\partial{g}}{\partial{x}}\\
\frac{\partial L}{\partial{y}} &amp;= \frac{\partial{f}}{\partial{y}} - \lambda \frac{\partial{g}}{\partial{y}}\\
\frac{\partial L}{\partial{\lambda}} &amp;= 0 + (g(x,y) - k).
\end{align}
\]</span></p>
<p>But if the Lagrange condition holds, each term is <span class="math inline">\(0\)</span>, so Lagranges method can be seen as solving for point <span class="math inline">\(\nabla{L} = \vec{0}\)</span>. The optimization problem in two variables with a constraint becomes a problem of finding and classifying zeros of a function with <em>three</em> variables.</p>
<p>Apply this to the optimization problem:</p>
<p>Find the extrema of <span class="math inline">\(f(x,y) = x^2 - y^2\)</span> subject to the constraint <span class="math inline">\(g(x,y) = x^2 + y^2 = 1\)</span>.</p>
<p>We have:</p>
<p><span class="math display">\[
L(x, y, \lambda) = f(x,y) - \lambda(g(x,y) - 1)
\]</span></p>
<p>We can solve for <span class="math inline">\(\nabla{L} = \vec{0}\)</span> by hand, but we do so symbolically:</p>
<div class="cell" data-execution_count="66">
<div class="sourceCode cell-code" id="cb100"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb100-1"><a href="#cb100-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> lambda</span>
<span id="cb100-2"><a href="#cb100-2" aria-hidden="true" tabindex="-1"></a><span class="fu">fₗₐ</span>(x, y) <span class="op">=</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> y<span class="op">^</span><span class="fl">2</span></span>
<span id="cb100-3"><a href="#cb100-3" aria-hidden="true" tabindex="-1"></a><span class="fu">gₗₐ</span>(x, y) <span class="op">=</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span></span>
<span id="cb100-4"><a href="#cb100-4" aria-hidden="true" tabindex="-1"></a><span class="fu">Lₗₐ</span>(x, y, lambda) <span class="op">=</span> <span class="fu">fₗₐ</span>(x,y) <span class="op">-</span> lambda <span class="op">*</span> (<span class="fu">gₗₐ</span>(x,y) <span class="op">-</span> <span class="fl">1</span>)</span>
<span id="cb100-5"><a href="#cb100-5" aria-hidden="true" tabindex="-1"></a>dsₗₐ <span class="op">=</span> <span class="fu">solve</span>(<span class="fu">diff</span>.(<span class="fu">Lₗₐ</span>(x, y, lambda), [x, y, lambda]))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="67">
<pre><code>4-element Vector{Dict{Any, Any}}:
Dict(lambda =&gt; -1, x =&gt; 0, y =&gt; -1)
Dict(lambda =&gt; -1, x =&gt; 0, y =&gt; 1)
Dict(lambda =&gt; 1, x =&gt; -1, y =&gt; 0)
Dict(lambda =&gt; 1, x =&gt; 1, y =&gt; 0)</code></pre>
</div>
</div>
<p>This has <span class="math inline">\(4\)</span> easy solutions, here are the values at each point:</p>
<div class="cell" data-execution_count="67">
<div class="sourceCode cell-code" id="cb102"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb102-1"><a href="#cb102-1" aria-hidden="true" tabindex="-1"></a>[<span class="fu">fₗₐ</span>(d[x], d[y]) for d <span class="kw">in</span> dsₗₐ]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="68">
<pre><code>4-element Vector{Sym}:
-1
-1
1
1</code></pre>
</div>
</div>
<p>So <span class="math inline">\(1\)</span> is a maximum value and <span class="math inline">\(-1\)</span> a minimum value.</p>
</section>
<section id="example-didos-problem" class="level5">
<h5 class="anchored" data-anchor-id="example-didos-problem">Example: Didos problem</h5>
<p>Consider a slightly different problem: What shape should a rope (curve) of fixed length make to <em>maximize</em> the area between the rope and <span class="math inline">\(x\)</span> axis?</p>
<p>Let <span class="math inline">\(L\)</span> be the length of the rope and suppose <span class="math inline">\(y(x)\)</span> describes the curve. Then we wish to</p>
<p><span class="math display">\[
\text{Maximize } \int y(x) dx, \quad\text{subject to }
\int \sqrt{1 + y'(x)^2} dx = L.
\]</span></p>
<p>The latter being the formula for arc length. This is very much like a optimization problem that Lagranges method could help solve, but with one big difference: the answer is <em>not</em> a point but a <em>function</em>.</p>
<p>This is a variant of <a href="http://www.ams.org/publications/journals/notices/201709/rnoti-p980.pdf">Dido</a>s problem, described by Bandle as</p>
<blockquote class="blockquote">
<p><em>Didos problem</em>: The Roman poet Publius Vergilius Maro (7019 B.C.) tells in his epic Aeneid the story of queen Dido, the daughter of the Phoenician king of the 9th century B.C. After the assassination of her husband by her brother she fled to a haven near Tunis. There she asked the local leader, Yarb, for as much land as could be enclosed by the hide of a bull. Since the deal seemed very modest, he agreed. Dido cut the hide into narrow strips, tied them together and encircled a large tract of land which became the city of Carthage. Dido faced the following mathematical problem, which is also known as the isoperimetric problem: Find among all curves of given length the one which encloses maximal area. Dido found intuitively the right answer.</p>
</blockquote>
<p>The problem as stated above and method of solution follows notes by <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.368.1522&amp;rep=rep1&amp;type=pdf">Wang</a> though Bandle attributes the ideas back to a 19-year old Lagrange in a letter to Euler.</p>
<p>The method of solution will be to <em>assume</em> we have the function and then characterize this function in such a way that it can be identified.</p>
<p>Following Lagrange, we generalize the problem to the following: maximize <span class="math inline">\(\int_{x_0}^{x_1} f(x, y(x), y'(x)) dx\)</span> subject to a constraint <span class="math inline">\(\int_{x_0}^{x_1} g(x,y(x), y'(x)) dx = K\)</span>. Suppose <span class="math inline">\(y(x)\)</span> is a solution.</p>
<p>The starting point is a <em>perturbation</em>: <span class="math inline">\(\hat{y}(x) = y(x) + \epsilon_1 \eta_1(x) + \epsilon_2 \eta_2(x)\)</span>. There are two perturbation terms, were only one term added, then the perturbation may make <span class="math inline">\(\hat{y}\)</span> not satisfy the constraint, the second term is used to ensure the constraint is not violated. If <span class="math inline">\(\hat{y}\)</span> is to be a possible solution to our problem, we would want <span class="math inline">\(\hat{y}(x_0) = \hat{y}(x_1) = 0\)</span>, as it does for <span class="math inline">\(y(x)\)</span>, so we <em>assume</em> <span class="math inline">\(\eta_1\)</span> and <span class="math inline">\(\eta_2\)</span> satisfy this boundary condition.</p>
<p>With this notation, and fixing <span class="math inline">\(y\)</span> we can re-express the equations in terms ot <span class="math inline">\(\epsilon_1\)</span> and <span class="math inline">\(\epsilon_2\)</span>:</p>
<p><span class="math display">\[
\begin{align}
F(\epsilon_1, \epsilon_2) &amp;= \int f(x, \hat{y}, \hat{y}') dx =
\int f(x, y + \epsilon_1 \eta_1 + \epsilon_2 \eta_2, y' + \epsilon_1 \eta_1' + \epsilon_2 \eta_2') dx,\\
G(\epsilon_1, \epsilon_2) &amp;= \int g(x, \hat{y}, \hat{y}') dx =
\int g(x, y + \epsilon_1 \eta_1 + \epsilon_2 \eta_2, y' + \epsilon_1 \eta_1' + \epsilon_2 \eta_2') dx.
\end{align}
\]</span></p>
<p>Then our problem is restated as:</p>
<p><span class="math display">\[
\text{Maximize } F(\epsilon_1, \epsilon_2) \text{ subject to }
G(\epsilon_1, \epsilon_2) = L.
\]</span></p>
<p>Now, Lagranges method can be employed. This will be fruitful - even though we know the answer - it being <span class="math inline">\(\epsilon_1 = \epsilon_2 = 0\)</span>!</p>
<p>Forging ahead, we compute <span class="math inline">\(\nabla{F}\)</span> and <span class="math inline">\(\lambda \nabla{G}\)</span> and set <span class="math inline">\(\epsilon_1 = \epsilon_2 = 0\)</span> where the two are equal. This will lead to a description of <span class="math inline">\(y\)</span> in terms of <span class="math inline">\(y'\)</span>.</p>
<p>Lagranges method has:</p>
<p><span class="math display">\[
\frac{\partial{F}}{\partial{\epsilon_1}}(0,0) - \lambda \frac{\partial{G}}{\partial{\epsilon_1}}(0,0) = 0, \text{ and }
\frac{\partial{F}}{\partial{\epsilon_2}}(0,0) - \lambda \frac{\partial{G}}{\partial{\epsilon_2}}(0,0) = 0.
\]</span></p>
<p>Computing just the first one, we have using the chain rule and assuming interchanging the derivative and integral is possible:</p>
<p><span class="math display">\[
\begin{align}
\frac{\partial{F}}{\partial{\epsilon_1}}
&amp;= \int \frac{\partial}{\partial{\epsilon_1}}(
f(x, y + \epsilon_1 \eta_1 + \epsilon_2 \eta_2, y' + \epsilon_1 \eta_1' + \epsilon_2 \eta_2')) dx\\
&amp;= \int \left(\frac{\partial{f}}{\partial{y}} \eta_1 + \frac{\partial{f}}{\partial{y'}} \eta_1'\right) dx\quad\quad(\text{from }\nabla{f} \cdot \langle 0, \eta_1, \eta_1'\rangle)\\
&amp;=\int \eta_1 \left(\frac{\partial{f}}{\partial{y}} - \frac{d}{dx}\frac{\partial{f}}{\partial{y'}}\right) dx.
\end{align}
\]</span></p>
<p>The last line by integration by parts: $u(x) v(x) dx = (u v)(x)_{x_0}^{x_1} - u(x) v(x) dx = - u(x) v(x) dx $. The last lines, as <span class="math inline">\(\eta_1 = 0\)</span> at <span class="math inline">\(x_0\)</span> and <span class="math inline">\(x_1\)</span> by assumption. We get:</p>
<p><span class="math display">\[
0 = \int \eta_1\left(\frac{\partial{f}}{\partial{y}} - \frac{d}{dx}\frac{\partial{f}}{\partial{y'}}\right).
\]</span></p>
<p>Similarly were <span class="math inline">\(G\)</span> considered, we would find a similar statement. Setting <span class="math inline">\(L(x, y, y') = f(x, y, y') - \lambda g(x, y, y')\)</span>, the combination of terms gives:</p>
<p><span class="math display">\[
0 = \int \eta_1\left(\frac{\partial{L}}{\partial{y}} - \frac{d}{dx}\frac{\partial{L}}{\partial{y'}}\right) dx.
\]</span></p>
<p>Since <span class="math inline">\(\eta_1\)</span> is arbitrary save for its boundary conditions, under smoothness conditions on <span class="math inline">\(L\)</span> this will imply the rest of the integrand <em>must</em> be <span class="math inline">\(0\)</span>.</p>
<p>That is, If <span class="math inline">\(y(x)\)</span> is a maximizer of <span class="math inline">\(\int_{x_0}^{x_1} f(x, y, y')dx\)</span> and sufficiently smooth over <span class="math inline">\([x_0, x_1]\)</span> and <span class="math inline">\(y(x)\)</span> satisfies the constraint <span class="math inline">\(\int_{x_0}^{x_1} g(x, y, y')dx = K\)</span> then there exists a constant <span class="math inline">\(\lambda\)</span> such that <span class="math inline">\(L = f -\lambda g\)</span> will satisfy:</p>
<p><span class="math display">\[
\frac{d}{dx}\frac{\partial{L}}{\partial{y'}} - \frac{\partial{L}}{\partial{y}} = 0.
\]</span></p>
<p>If <span class="math inline">\(\partial{L}/\partial{x} = 0\)</span>, this simplifies to the <a href="https://en.wikipedia.org/wiki/Beltrami_identity">Beltrami</a> identity:</p>
<p><span class="math display">\[
L - y' \frac{\partial{L}}{\partial{y'}} = C.\quad(\text{Beltrami identity})
\]</span></p>
<hr>
<p>For Didos problem, <span class="math inline">\(f(x,y,y') = y\)</span> and <span class="math inline">\(g(x, y, y') = \sqrt{1 + y'^2}\)</span>, so <span class="math inline">\(L = y - \lambda\sqrt{1 + y'^2}\)</span> will have <span class="math inline">\(0\)</span> partial derivative with respect to <span class="math inline">\(x\)</span>. Using the Beltrami identify we have:</p>
<p><span class="math display">\[
(y - \lambda\sqrt{1 + y'^2}) - \lambda y' \frac{2y'}{2\sqrt{1 + y'^2}} = C.
\]</span></p>
<p>by multiplying through by the denominator and squaring to remove the square root, a quadratic equation in <span class="math inline">\(y'^2\)</span> can be found. This can be solved to give:</p>
<p><span class="math display">\[
y' = \frac{dy}{dx} = \sqrt{\frac{\lambda^2 -(y + C)^2}{(y+C)^2}}.
\]</span></p>
<p>Here is a snippet of <code>SymPy</code> code to verify the above:</p>
<div class="cell" data-hold="true" data-execution_count="68">
<div class="sourceCode cell-code" id="cb104"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb104-1"><a href="#cb104-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@vars</span> y y λ C</span>
<span id="cb104-2"><a href="#cb104-2" aria-hidden="true" tabindex="-1"></a>ex <span class="op">=</span> <span class="fu">Eq</span>(<span class="op">-</span>λ<span class="op">*</span>y<span class="op">^</span><span class="fl">2</span><span class="op">/</span><span class="fu">sqrt</span>(<span class="fl">1</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span>) <span class="op">+</span> <span class="fu">λ*sqrt</span>(<span class="fl">1</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span>), C <span class="op">+</span> y)</span>
<span id="cb104-3"><a href="#cb104-3" aria-hidden="true" tabindex="-1"></a>Δ <span class="op">=</span> <span class="fu">sqrt</span>(<span class="fl">1</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span>) <span class="op">/</span> (C<span class="op">+</span>y)</span>
<span id="cb104-4"><a href="#cb104-4" aria-hidden="true" tabindex="-1"></a>ex1 <span class="op">=</span> <span class="fu">Eq</span>(<span class="fu">simplify</span>(ex.<span class="fu">lhs</span>()<span class="op">*</span>Δ), <span class="fu">simplify</span>(ex.<span class="fu">rhs</span>() <span class="op">*</span> Δ))</span>
<span id="cb104-5"><a href="#cb104-5" aria-hidden="true" tabindex="-1"></a>ex2 <span class="op">=</span> <span class="fu">Eq</span>(ex1.<span class="fu">lhs</span>()<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> <span class="fl">1</span>, <span class="fu">simplify</span>(ex1.<span class="fu">rhs</span>()<span class="op">^</span><span class="fl">2</span>) <span class="op">-</span> <span class="fl">1</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="69">
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
\[
\frac{λ^{2}}{\left(C + y\right)^{2}} - 1 = y^{2}
\]
</span>
</div>
</div>
<p>Now <span class="math inline">\(y'\)</span> can be integrated using the substitution <span class="math inline">\(y + C = \lambda \cos\theta\)</span> to give: <span class="math inline">\(-\lambda\int\cos\theta d\theta = x + D\)</span>, <span class="math inline">\(D\)</span> some constant. That is:</p>
<p><span class="math display">\[
\begin{align}
x + D &amp;= - \lambda \sin\theta\\
y + C &amp;= \lambda\cos\theta.
\end{align}
\]</span></p>
<p>Squaring gives the equation of a circle: <span class="math inline">\((x +D)^2 + (y+C)^2 = \lambda^2\)</span>.</p>
<p>We center and <em>rescale</em> the problem so that <span class="math inline">\(x_0 = -1, x_1 = 1\)</span>. Then <span class="math inline">\(L &gt; 2\)</span> as otherwise the rope is too short. From here, we describe the radius and center of the circle.</p>
<p>We have <span class="math inline">\(y=0\)</span> at <span class="math inline">\(x=1\)</span> and <span class="math inline">\(-1\)</span> giving:</p>
<p><span class="math display">\[
\begin{align}
(-1 + D)^2 + (0 + C)^2 &amp;= \lambda^2\\
(+1 + D)^2 + (0 + C)^2 &amp;= \lambda^2.
\end{align}
\]</span></p>
<p>Squaring out and solving gives <span class="math inline">\(D=0\)</span>, <span class="math inline">\(1 + C^2 = \lambda^2\)</span>. That is, an arc of circle with radius <span class="math inline">\(1+C^2\)</span> and centered at <span class="math inline">\((0, -C)\)</span>.</p>
<p><span class="math display">\[
x^2 + (y + C)^2 = 1 + C^2.
\]</span></p>
<p>Now to identify <span class="math inline">\(C\)</span> in terms of <span class="math inline">\(L\)</span>. <span class="math inline">\(L\)</span> is the length of arc of circle of radius <span class="math inline">\(r =\sqrt{1 + C^2}\)</span> and angle <span class="math inline">\(2\theta\)</span>, so <span class="math inline">\(L = 2r\theta\)</span> But using the boundary conditions in the equations for <span class="math inline">\(x\)</span> and <span class="math inline">\(y\)</span> gives <span class="math inline">\(\tan\theta = 1/C\)</span>, so <span class="math inline">\(L = 2\sqrt{1 + C^2}\tan^{-1}(1/C)\)</span> which can be solved for <span class="math inline">\(C\)</span> provided <span class="math inline">\(L \geq 2\)</span>.</p>
</section>
<section id="example-more-constraints" class="level5">
<h5 class="anchored" data-anchor-id="example-more-constraints">Example: more constraints</h5>
<p>Consider now the case of maximizing <span class="math inline">\(f(x,y,z)\)</span> subject to <span class="math inline">\(g(x,y,z)=c\)</span> and <span class="math inline">\(h(x,y,z) = d\)</span>. Can something similar be said to characterize potential values for this to occur? Trying to describe where <span class="math inline">\(g(x,y,z) = c\)</span> and <span class="math inline">\(h(x,y,z)=d\)</span> in general will prove difficult. The easy case would be it the two equations were linear, in which case they would describe planes. Two non-parallel planes would intersect in a line. If the general case, imagine the surfaces locally replaced by their tangent planes, then their intersection would be a line, and this line would point in along the curve given by the intersection of the surfaces formed by the contraints. This line is similar to the tangent line in the <span class="math inline">\(2\)</span>-variable case. Now if <span class="math inline">\(\nabla{f}\)</span>, which points in the direction of greatest increase of <span class="math inline">\(f\)</span>, had a non-zero projection onto this line, then moving the point in that direction along the line would increase <span class="math inline">\(f\)</span> and still leave the point following the contraints. That is, if there is a non-zero directional derivative the point is not a maximum.</p>
<p>The tangent planes are <em>orthogonal</em> to the vectors <span class="math inline">\(\nabla{g}\)</span> and <span class="math inline">\(\nabla{h}\)</span>, so in this case parallel to <span class="math inline">\(\nabla{g} \times \nabla{h}\)</span>. The condition that <span class="math inline">\(\nabla{f}\)</span> be <em>orthogonal</em> to this vector, means that <span class="math inline">\(\nabla{f}\)</span> <em>must</em> sit in the plane described by <span class="math inline">\(\nabla{g}\)</span> and <span class="math inline">\(\nabla{h}\)</span> - the plane of orthogonal vectors to <span class="math inline">\(\nabla{g} \times \nabla{h}\)</span>. That is, this condition is needed:</p>
<p><span class="math display">\[
\nabla{f}(x,y,z) = \lambda_1 \nabla{g}(x,y,z) + \lambda_2 \nabla{h}(x,y,z).
\]</span></p>
<p>At a point satisfying the above, we would have the tangent “plane” of <span class="math inline">\(f\)</span> is contained in the intersection of the tangent “plane”s to <span class="math inline">\(g\)</span> and <span class="math inline">\(h\)</span>.</p>
<hr>
<p>Consider a curve given through the intersection of two expressions: <span class="math inline">\(g_1(x,y,z) = x^2 + y^2 - z^2 = 0\)</span> and <span class="math inline">\(g_2(x,y,z) = x - 2z = 3\)</span>. What is the minimum distance to the origin along this curve?</p>
<p>We have <span class="math inline">\(f(x,y,z) = \text{distance}(\vec{x},\vec{0}) = \sqrt{x^2 + y^2 + z^2}\)</span>, subject to the two constraints. As the square root is increasing, we can actually just consider <span class="math inline">\(f(x,y,z) = x^2 + y^2 + z^2\)</span>, ignoring the square root. The Lagrange multiplier technique instructs us to look for solutions to:</p>
<p><span class="math display">\[
\langle 2x, 2y ,2x \rangle = \lambda_1\langle 2x, 2y, -2z\rangle + \lambda_2 \langle 1, 0, -2 \rangle.
\]</span></p>
<p>Here we use <code>SymPy</code>:</p>
<div class="cell" data-execution_count="69">
<div class="sourceCode cell-code" id="cb105"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb105-1"><a href="#cb105-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> z lambda1 lambda2</span>
<span id="cb105-2"><a href="#cb105-2" aria-hidden="true" tabindex="-1"></a><span class="fu">g1</span>(x, y, z) <span class="op">=</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> z<span class="op">^</span><span class="fl">2</span></span>
<span id="cb105-3"><a href="#cb105-3" aria-hidden="true" tabindex="-1"></a><span class="fu">g2</span>(x, y, z) <span class="op">=</span> x <span class="op">-</span> <span class="fl">2</span>z <span class="op">-</span> <span class="fl">3</span></span>
<span id="cb105-4"><a href="#cb105-4" aria-hidden="true" tabindex="-1"></a><span class="fu">fₘ</span>(x,y,z)<span class="op">=</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> z<span class="op">^</span><span class="fl">2</span></span>
<span id="cb105-5"><a href="#cb105-5" aria-hidden="true" tabindex="-1"></a><span class="fu">Lₘ</span>(x,y,z,lambda1, lambda2) <span class="op">=</span> <span class="fu">fₘ</span>(x,y,z) <span class="op">-</span> <span class="fu">lambda1*</span>(<span class="fu">g1</span>(x,y,z) <span class="op">-</span> <span class="fl">0</span>) <span class="op">-</span> <span class="fu">lambda2*</span>(<span class="fu">g2</span>(x,y,z) <span class="op">-</span> <span class="fl">0</span>)</span>
<span id="cb105-6"><a href="#cb105-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb105-7"><a href="#cb105-7" aria-hidden="true" tabindex="-1"></a>∇Lₘ <span class="op">=</span> <span class="fu">diff</span>.(<span class="fu">Lₘ</span>(x,y,z,lambda1, lambda2), [x, y, z,lambda1, lambda2])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="70">
<pre><code>5-element Vector{Sym}:
-2⋅λ₁⋅x - λ₂ + 2⋅x
-2⋅λ₁⋅y + 2⋅y
2⋅λ₁⋅z + 2⋅λ₂ + 2⋅z
-x^2 - y^2 + z^2
-x + 2⋅z + 3</code></pre>
</div>
</div>
<p>Before trying to solve for <span class="math inline">\(\nabla{L} = \vec{0}\)</span> we see from the second equation that <em>either</em> <span class="math inline">\(\lambda_1 = 1\)</span> or <span class="math inline">\(y = 0\)</span>. First we solve with <span class="math inline">\(\lambda_1 = 1\)</span>:</p>
<div class="cell" data-execution_count="70">
<div class="sourceCode cell-code" id="cb107"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb107-1"><a href="#cb107-1" aria-hidden="true" tabindex="-1"></a><span class="fu">solve</span>(<span class="fu">subs</span>.(∇Lₘ, lambda1 <span class="op">.=&gt;</span> <span class="fl">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="71">
<pre><code>2-element Vector{Dict{Any, Any}}:
Dict(z =&gt; 0, x =&gt; 3, lambda2 =&gt; 0, y =&gt; -3*I)
Dict(z =&gt; 0, x =&gt; 3, lambda2 =&gt; 0, y =&gt; 3*I)</code></pre>
</div>
</div>
<p>There are no real solutions. Next when <span class="math inline">\(y = 0\)</span> we get:</p>
<div class="cell" data-execution_count="71">
<div class="sourceCode cell-code" id="cb109"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb109-1"><a href="#cb109-1" aria-hidden="true" tabindex="-1"></a>outₘ <span class="op">=</span> <span class="fu">solve</span>(<span class="fu">subs</span>.(∇Lₘ, y <span class="op">.=&gt;</span> <span class="fl">0</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="72">
<pre><code>2-element Vector{Dict{Any, Any}}:
Dict(z =&gt; -1, x =&gt; 1, lambda2 =&gt; 4/3, lambda1 =&gt; 1/3)
Dict(z =&gt; -3, x =&gt; -3, lambda2 =&gt; 12, lambda1 =&gt; 3)</code></pre>
</div>
</div>
<p>The two solutions have values yielding the extrema:</p>
<div class="cell" data-execution_count="72">
<div class="sourceCode cell-code" id="cb111"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb111-1"><a href="#cb111-1" aria-hidden="true" tabindex="-1"></a>[<span class="fu">fₘ</span>(d[x], <span class="fl">0</span>, d[z]) for d <span class="kw">in</span> outₘ]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="73">
<pre><code>2-element Vector{Sym}:
2
18</code></pre>
</div>
</div>
</section>
</section>
<section id="taylors-theorem" class="level2" data-number="56.6">
<h2 data-number="56.6" class="anchored" data-anchor-id="taylors-theorem"><span class="header-section-number">56.6</span> Taylors theorem</h2>
<p>Taylors theorem for a univariate function states that if <span class="math inline">\(f\)</span> has <span class="math inline">\(k+1\)</span> derivatives in an open interval around <span class="math inline">\(a\)</span>, <span class="math inline">\(f^{(k)}\)</span> is continuous between the closed interval from <span class="math inline">\(a\)</span> to <span class="math inline">\(x\)</span> then:</p>
<p><span class="math display">\[
f(x) = \sum_{j=0}^k \frac{f^{j}(a)}{j!} (x-a)^k + R_k(x),
\]</span></p>
<p>where <span class="math inline">\(R_k(x) = f^{k+1}(\xi)/(k+1)!(x-a)^{k+1}\)</span> for some <span class="math inline">\(\xi\)</span> between <span class="math inline">\(a\)</span> and <span class="math inline">\(x\)</span>.</p>
<p>This theorem can be generalized to scalar functions, but the notation can be cumbersome. Following <a href="https://sites.math.washington.edu/~folland/Math425/taylor2.pdf">Folland</a> we use <em>multi-index</em> notation. Suppose <span class="math inline">\(f:R^n \rightarrow R\)</span>, and let <span class="math inline">\(\alpha=(\alpha_1, \alpha_2, \dots, \alpha_n)\)</span>. Then define the following notation:</p>
<p><span class="math display">\[
\begin{align*}
|\alpha| &amp;= \alpha_1 + \cdots + \alpha_n, \\
\alpha! &amp;= \alpha_1!\alpha_2!\cdot\cdots\cdot\alpha_n!, \\
\vec{x}^\alpha &amp;= x_1^{\alpha_1}x_2^{\alpha_2}\cdots x_n^{\alpha^n}, \\
\partial^\alpha f &amp;= \partial_1^{\alpha_1}\partial_2^{\alpha_2}\cdots \partial_n^{\alpha_n} f \\
&amp; = \frac{\partial^{|\alpha|}f}{\partial x_1^{\alpha_1} \partial x_2^{\alpha_2} \cdots \partial x_n^{\alpha_n}}.
\endalign*}
\]</span></p>
<p>This notation makes many formulas from one dimension carry over to higher dimensions. For example, the binomial theorem says:</p>
<p><span class="math display">\[
(a+b)^n = \sum_{k=0}^n \frac{n!}{k!(n-k)!}a^kb^{n-k},
\]</span></p>
<p>and this becomes:</p>
<p><span class="math display">\[
(x_1 + x_2 + \cdots + x_n)^n = \sum_{|\alpha|=k} \frac{k!}{\alpha!} \vec{x}^\alpha.
\]</span></p>
<p>Taylors theorem then becomes:</p>
<p>If <span class="math inline">\(f: R^n \rightarrow R\)</span> is sufficiently smooth (<span class="math inline">\(C^{k+1}\)</span>) on an open convex set <span class="math inline">\(S\)</span> about <span class="math inline">\(\vec{a}\)</span> then if <span class="math inline">\(\vec{a}\)</span> and <span class="math inline">\(\vec{a}+\vec{h}\)</span> are in <span class="math inline">\(S\)</span>,</p>
<p><span class="math display">\[
f(\vec{a} + \vec{h}) = \sum_{|\alpha| \leq k}\frac{\partial^\alpha f(\vec{a})}{\alpha!}\vec{h}^\alpha + R_{\vec{a},k}(\vec{h}),
\]</span></p>
<p>where <span class="math inline">\(R_{\vec{a},k} = \sum_{|\alpha|=k+1}\partial^\alpha \frac{f(\vec{a} + c\vec{h})}{\alpha!} \vec{h}^\alpha\)</span> for some <span class="math inline">\(c\)</span> in <span class="math inline">\((0,1)\)</span>.</p>
<section id="example-10" class="level5">
<h5 class="anchored" data-anchor-id="example-10">Example</h5>
<p>The elegant notation masks what can be complicated expressions. Consider the simple case <span class="math inline">\(f:R^2 \rightarrow R\)</span> and <span class="math inline">\(k=2\)</span>. Then this says:</p>
<p><span class="math display">\[
\begin{align*}
f(x + dx, y+dy) &amp;= f(x, y) + \frac{\partial f}{\partial x} dx + \frac{\partial f}{\partial y} dy \\
&amp;+ \frac{\partial^2 f}{\partial x^2} \frac{dx^2}{2} + 2\frac{\partial^2 f}{\partial x\partial y} \frac{dx dy}{2}\\
&amp;+ \frac{\partial^2 f}{\partial y^2} \frac{dy^2}{2} + R_{\langle x, y \rangle, k}(\langle dx, dy \rangle).
\end{align*}
\]</span></p>
<p>Using <span class="math inline">\(\nabla\)</span> and <span class="math inline">\(H\)</span> for the Hessian and <span class="math inline">\(\vec{x} = \langle x, y \rangle\)</span> and <span class="math inline">\(d\vec{x} = \langle dx, dy \rangle\)</span>, this can be expressed as:</p>
<p><span class="math display">\[
f(\vec{x} + d\vec{x}) = f(\vec{x}) + \nabla{f} \cdot d\vec{x} + d\vec{x} \cdot (H d\vec{x}) +R_{\vec{x}, k}d\vec{x}.
\]</span></p>
<p>As for <span class="math inline">\(R\)</span>, the full term involves terms for <span class="math inline">\(\alpha = (3,0), (2,1), (1,2)\)</span>, and <span class="math inline">\((0,3)\)</span>. Using <span class="math inline">\(\vec{a} = \langle x, y\rangle\)</span> and <span class="math inline">\(\vec{h}=\langle dx, dy\rangle\)</span>:</p>
<p><span class="math display">\[
\frac{\partial^3 f(\vec{a}+c\vec{h})}{\partial x^3} \frac{dx^3}{3!}+
\frac{\partial^3 f(\vec{a}+c\vec{h})}{\partial x^2\partial y} \frac{dx^2 dy}{2!1!} +
\frac{\partial^3 f(\vec{a}+c\vec{h})}{\partial x\partial y^2} \frac{dxdy^2}{1!2!} +
\frac{\partial^3 f(\vec{a}+c\vec{h})}{\partial y^3} \frac{dy^3}{3!}.
\]</span></p>
<p>The exact answer is usually not as useful as the bound: <span class="math inline">\(|R| \leq M/(k+1)! \|\vec{h}\|^{k+1}\)</span>, for some finite constant <span class="math inline">\(M\)</span>.</p>
</section>
<section id="example-11" class="level5">
<h5 class="anchored" data-anchor-id="example-11">Example</h5>
<p>We can encode multiindices using <code>SymPy</code>. The basic definitions are fairly straightforward using <code>zip</code> to pair variables with components of <span class="math inline">\(\alpha\)</span>. We define a new type so that we can overload the familiar notation:</p>
<div class="cell" data-execution_count="73">
<div class="sourceCode cell-code" id="cb113"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb113-1"><a href="#cb113-1" aria-hidden="true" tabindex="-1"></a><span class="kw">struct</span> MultiIndex</span>
<span id="cb113-2"><a href="#cb113-2" aria-hidden="true" tabindex="-1"></a> alpha<span class="op">::</span><span class="dt">Vector{Int}</span></span>
<span id="cb113-3"><a href="#cb113-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">end</span></span>
<span id="cb113-4"><a href="#cb113-4" aria-hidden="true" tabindex="-1"></a><span class="bu">Base</span>.<span class="fu">show</span>(io<span class="op">::</span><span class="dt">IO</span>, α<span class="op">::</span><span class="dt">MultiIndex</span>) <span class="op">=</span> <span class="fu">println</span>(io, <span class="st">"α = (</span><span class="sc">$</span>(<span class="fu">join</span>(α.alpha, <span class="st">", "</span>))<span class="st">)"</span>)</span>
<span id="cb113-5"><a href="#cb113-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb113-6"><a href="#cb113-6" aria-hidden="true" tabindex="-1"></a><span class="co">## |α| = α_1 + ... + α_m</span></span>
<span id="cb113-7"><a href="#cb113-7" aria-hidden="true" tabindex="-1"></a><span class="bu">Base</span>.<span class="fu">length</span>(α<span class="op">::</span><span class="dt">MultiIndex</span>) <span class="op">=</span> <span class="fu">sum</span>(α.alpha)</span>
<span id="cb113-8"><a href="#cb113-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb113-9"><a href="#cb113-9" aria-hidden="true" tabindex="-1"></a><span class="co">## factorial(α) computes α!</span></span>
<span id="cb113-10"><a href="#cb113-10" aria-hidden="true" tabindex="-1"></a><span class="bu">Base</span>.<span class="fu">factorial</span>(α<span class="op">::</span><span class="dt">MultiIndex</span>) <span class="op">=</span> <span class="fu">prod</span>(<span class="fu">factorial</span>(<span class="fu">Sym</span>(a)) <span class="cf">for</span> a <span class="kw">in</span> α.alpha)</span>
<span id="cb113-11"><a href="#cb113-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb113-12"><a href="#cb113-12" aria-hidden="true" tabindex="-1"></a><span class="co">## x^α = x_1^α_1 * x_2^α^2 * ... * x_n^α_n</span></span>
<span id="cb113-13"><a href="#cb113-13" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> <span class="bu">Base</span>: ^</span>
<span id="cb113-14"><a href="#cb113-14" aria-hidden="true" tabindex="-1"></a><span class="op">^</span>(x, α<span class="op">::</span><span class="dt">MultiIndex</span>) <span class="op">=</span> <span class="fu">prod</span>(u<span class="op">^</span>a <span class="cf">for</span> (u,a) <span class="kw">in</span> <span class="fu">zip</span>(x, α.alpha))</span>
<span id="cb113-15"><a href="#cb113-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb113-16"><a href="#cb113-16" aria-hidden="true" tabindex="-1"></a><span class="co">## ∂^α(ex) = ∂_1^α_1 ∘ ∂_2^α_2 ∘ ... ∘ ∂_n^α_n (ex)</span></span>
<span id="cb113-17"><a href="#cb113-17" aria-hidden="true" tabindex="-1"></a><span class="fu">partial</span>(ex<span class="op">::</span><span class="dt">SymPy.SymbolicObject</span>, α<span class="op">::</span><span class="dt">MultiIndex</span>, vars<span class="op">=</span><span class="fu">free_symbols</span>(ex)) <span class="op">=</span> <span class="fu">diff</span>(ex, <span class="fu">zip</span>(vars, α.alpha)<span class="op">...</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="74">
<pre><code>partial (generic function with 2 methods)</code></pre>
</div>
</div>
<div class="cell" data-execution_count="74">
<div class="sourceCode cell-code" id="cb115"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb115-1"><a href="#cb115-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> w</span>
<span id="cb115-2"><a href="#cb115-2" aria-hidden="true" tabindex="-1"></a>alpha <span class="op">=</span> <span class="fu">MultiIndex</span>([<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">1</span>,<span class="fl">3</span>])</span>
<span id="cb115-3"><a href="#cb115-3" aria-hidden="true" tabindex="-1"></a><span class="fu">length</span>(alpha) <span class="co"># 1 + 2 + 1 + 3=7</span></span>
<span id="cb115-4"><a href="#cb115-4" aria-hidden="true" tabindex="-1"></a>[<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">3</span>,<span class="fl">4</span>]<span class="op">^</span>alpha</span>
<span id="cb115-5"><a href="#cb115-5" aria-hidden="true" tabindex="-1"></a>exₜ <span class="op">=</span> x<span class="op">^</span><span class="fl">3</span> <span class="op">*</span> <span class="fu">cos</span>(w<span class="op">*</span>y<span class="op">*</span>z)</span>
<span id="cb115-6"><a href="#cb115-6" aria-hidden="true" tabindex="-1"></a><span class="fu">partial</span>(exₜ, alpha, [w,x,y,z])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="75">
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
\[
6 w^{2} x y^{2} \left(- w^{2} y^{2} z^{2} \sin{\left(w y z \right)} + 7 w y z \cos{\left(w y z \right)} + 9 \sin{\left(w y z \right)}\right)
\]
</span>
</div>
</div>
<p>The remainder term needs to know information about sets like <span class="math inline">\(|\alpha| =k\)</span>. This is a combinatoric problem, even to identify the length. Here we define an iterator to iterate over all possible MultiIndexes. This is low level, and likely could be done in a much better style, so shouldnt be parsed unless there is curiosity. It manually chains together iterators.</p>
<div class="sourceCode cell-code" id="cb116"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb116-1"><a href="#cb116-1" aria-hidden="true" tabindex="-1"></a><span class="kw">struct</span> MultiIndices</span>
<span id="cb116-2"><a href="#cb116-2" aria-hidden="true" tabindex="-1"></a> n<span class="op">::</span><span class="dt">Int</span></span>
<span id="cb116-3"><a href="#cb116-3" aria-hidden="true" tabindex="-1"></a> k<span class="op">::</span><span class="dt">Int</span></span>
<span id="cb116-4"><a href="#cb116-4" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span>
<span id="cb116-5"><a href="#cb116-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb116-6"><a href="#cb116-6" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="bu">Base</span>.<span class="fu">length</span>(as<span class="op">::</span><span class="dt">MultiIndices</span>)</span>
<span id="cb116-7"><a href="#cb116-7" aria-hidden="true" tabindex="-1"></a> n,k <span class="op">=</span> as.n, as.k</span>
<span id="cb116-8"><a href="#cb116-8" aria-hidden="true" tabindex="-1"></a> n <span class="op">==</span> <span class="fl">1</span> <span class="op">&amp;&amp;</span> <span class="cf">return</span> <span class="fl">1</span></span>
<span id="cb116-9"><a href="#cb116-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">sum</span>(<span class="fu">length</span>(<span class="fu">MultiIndices</span>(n<span class="op">-</span><span class="fl">1</span>, j)) <span class="cf">for</span> j <span class="kw">in</span> <span class="fl">0</span><span class="op">:</span>k) <span class="co"># recursively identify length</span></span>
<span id="cb116-10"><a href="#cb116-10" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb116-11"><a href="#cb116-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb116-12"><a href="#cb116-12" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="bu">Base</span>.<span class="fu">iterate</span>(alphas<span class="op">::</span><span class="dt">MultiIndices</span>)</span>
<span id="cb116-13"><a href="#cb116-13" aria-hidden="true" tabindex="-1"></a> k, n <span class="op">=</span> alphas.k, alphas.n</span>
<span id="cb116-14"><a href="#cb116-14" aria-hidden="true" tabindex="-1"></a> n <span class="op">==</span> <span class="fl">1</span> <span class="op">&amp;&amp;</span> <span class="cf">return</span> ([k],(<span class="fl">0</span>, <span class="fu">MultiIndices</span>(<span class="fl">0</span>,<span class="fl">0</span>), <span class="cn">nothing</span>))</span>
<span id="cb116-15"><a href="#cb116-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb116-16"><a href="#cb116-16" aria-hidden="true" tabindex="-1"></a> m <span class="op">=</span> <span class="fu">zeros</span>(<span class="dt">Int</span>, n)</span>
<span id="cb116-17"><a href="#cb116-17" aria-hidden="true" tabindex="-1"></a> m[<span class="fl">1</span>] <span class="op">=</span> k</span>
<span id="cb116-18"><a href="#cb116-18" aria-hidden="true" tabindex="-1"></a> betas <span class="op">=</span> <span class="fu">MultiIndices</span>(n<span class="op">-</span><span class="fl">1</span>, <span class="fl">0</span>)</span>
<span id="cb116-19"><a href="#cb116-19" aria-hidden="true" tabindex="-1"></a> stb <span class="op">=</span> <span class="fu">iterate</span>(betas)</span>
<span id="cb116-20"><a href="#cb116-20" aria-hidden="true" tabindex="-1"></a> st <span class="op">=</span> (k, <span class="fu">MultiIndices</span>(n<span class="op">-</span><span class="fl">1</span>, <span class="fl">0</span>), stb)</span>
<span id="cb116-21"><a href="#cb116-21" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> (m, st)</span>
<span id="cb116-22"><a href="#cb116-22" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span>
<span id="cb116-23"><a href="#cb116-23" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb116-24"><a href="#cb116-24" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="bu">Base</span>.<span class="fu">iterate</span>(alphas<span class="op">::</span><span class="dt">MultiIndices</span>, st)</span>
<span id="cb116-25"><a href="#cb116-25" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb116-26"><a href="#cb116-26" aria-hidden="true" tabindex="-1"></a> st <span class="op">==</span> <span class="cn">nothing</span> <span class="op">&amp;&amp;</span> <span class="cf">return</span> <span class="cn">nothing</span></span>
<span id="cb116-27"><a href="#cb116-27" aria-hidden="true" tabindex="-1"></a> k,n <span class="op">=</span> alphas.k, alphas.n</span>
<span id="cb116-28"><a href="#cb116-28" aria-hidden="true" tabindex="-1"></a> k <span class="op">==</span> <span class="fl">0</span> <span class="op">&amp;&amp;</span> <span class="cf">return</span> <span class="cn">nothing</span></span>
<span id="cb116-29"><a href="#cb116-29" aria-hidden="true" tabindex="-1"></a> n <span class="op">==</span> <span class="fl">1</span> <span class="op">&amp;&amp;</span> <span class="cf">return</span> <span class="cn">nothing</span></span>
<span id="cb116-30"><a href="#cb116-30" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb116-31"><a href="#cb116-31" aria-hidden="true" tabindex="-1"></a> <span class="co"># can we iterate the next on</span></span>
<span id="cb116-32"><a href="#cb116-32" aria-hidden="true" tabindex="-1"></a> bk, bs, stb <span class="op">=</span> st</span>
<span id="cb116-33"><a href="#cb116-33" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb116-34"><a href="#cb116-34" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> stb<span class="op">==</span><span class="cn">nothing</span></span>
<span id="cb116-35"><a href="#cb116-35" aria-hidden="true" tabindex="-1"></a> bk <span class="op">=</span> bk<span class="op">-</span><span class="fl">1</span></span>
<span id="cb116-36"><a href="#cb116-36" aria-hidden="true" tabindex="-1"></a> bk <span class="op">&lt;</span> <span class="fl">0</span> <span class="op">&amp;&amp;</span> <span class="cf">return</span> <span class="cn">nothing</span></span>
<span id="cb116-37"><a href="#cb116-37" aria-hidden="true" tabindex="-1"></a> bs <span class="op">=</span> <span class="fu">MultiIndices</span>(bs.n, bs.k<span class="op">+</span><span class="fl">1</span>)</span>
<span id="cb116-38"><a href="#cb116-38" aria-hidden="true" tabindex="-1"></a> val, stb <span class="op">=</span> <span class="fu">iterate</span>(bs)</span>
<span id="cb116-39"><a href="#cb116-39" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> (<span class="fu">vcat</span>(bk,val), (bk, bs, stb))</span>
<span id="cb116-40"><a href="#cb116-40" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb116-41"><a href="#cb116-41" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb116-42"><a href="#cb116-42" aria-hidden="true" tabindex="-1"></a> resp <span class="op">=</span> <span class="fu">iterate</span>(bs, stb)</span>
<span id="cb116-43"><a href="#cb116-43" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> resp <span class="op">==</span> <span class="cn">nothing</span></span>
<span id="cb116-44"><a href="#cb116-44" aria-hidden="true" tabindex="-1"></a> bk <span class="op">=</span> bk<span class="op">-</span><span class="fl">1</span></span>
<span id="cb116-45"><a href="#cb116-45" aria-hidden="true" tabindex="-1"></a> bk <span class="op">&lt;</span> <span class="fl">0</span> <span class="op">&amp;&amp;</span> <span class="cf">return</span> <span class="cn">nothing</span></span>
<span id="cb116-46"><a href="#cb116-46" aria-hidden="true" tabindex="-1"></a> bs <span class="op">=</span> <span class="fu">MultiIndices</span>(bs.n, bs.k<span class="op">+</span><span class="fl">1</span>)</span>
<span id="cb116-47"><a href="#cb116-47" aria-hidden="true" tabindex="-1"></a> val, stb <span class="op">=</span> <span class="fu">iterate</span>(bs)</span>
<span id="cb116-48"><a href="#cb116-48" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> (<span class="fu">vcat</span>(bk, val), (bk, bs, stb))</span>
<span id="cb116-49"><a href="#cb116-49" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb116-50"><a href="#cb116-50" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb116-51"><a href="#cb116-51" aria-hidden="true" tabindex="-1"></a> val, stb <span class="op">=</span> resp</span>
<span id="cb116-52"><a href="#cb116-52" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> (<span class="fu">vcat</span>(bk, val), (bk, bs, stb))</span>
<span id="cb116-53"><a href="#cb116-53" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb116-54"><a href="#cb116-54" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>This returns a vector, not a <code>MultiIndex</code>. Here we get all multiindices in two variables of size <span class="math inline">\(3\)</span></p>
<div class="cell" data-execution_count="76">
<div class="sourceCode cell-code" id="cb117"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb117-1"><a href="#cb117-1" aria-hidden="true" tabindex="-1"></a><span class="fu">collect</span>(<span class="fu">MultiIndices</span>(<span class="fl">2</span>, <span class="fl">3</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="77">
<pre><code>4-element Vector{Any}:
[3, 0]
[2, 1]
[1, 2]
[0, 3]</code></pre>
</div>
</div>
<p>To get all of size <span class="math inline">\(3\)</span> or less, we could do something like this:</p>
<div class="cell" data-execution_count="77">
<div class="sourceCode cell-code" id="cb119"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb119-1"><a href="#cb119-1" aria-hidden="true" tabindex="-1"></a><span class="fu">union</span>((<span class="fu">collect</span>(<span class="fu">MultiIndices</span>(<span class="fl">2</span>, i)) <span class="cf">for</span> i <span class="kw">in</span> <span class="fl">0</span><span class="op">:</span><span class="fl">3</span>)<span class="op">...</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="78">
<pre><code>10-element Vector{Any}:
[0, 0]
[1, 0]
[0, 1]
[2, 0]
[1, 1]
[0, 2]
[3, 0]
[2, 1]
[1, 2]
[0, 3]</code></pre>
</div>
</div>
<p>To see the computational complexity. Suppose we had <span class="math inline">\(3\)</span> variables and were interested in the error for order <span class="math inline">\(4\)</span>:</p>
<div class="cell" data-execution_count="78">
<div class="sourceCode cell-code" id="cb121"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb121-1"><a href="#cb121-1" aria-hidden="true" tabindex="-1"></a>k <span class="op">=</span> <span class="fl">4</span></span>
<span id="cb121-2"><a href="#cb121-2" aria-hidden="true" tabindex="-1"></a><span class="fu">length</span>(<span class="fu">MultiIndices</span>(<span class="fl">3</span>, k<span class="op">+</span><span class="fl">1</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="79">
<pre><code>21</code></pre>
</div>
</div>
<p>Finally, to see how compact the notation issue, suppose <span class="math inline">\(f:R^3 \rightarrow R\)</span>, we have the third-order Taylor series expands to <span class="math inline">\(20\)</span> terms as follows:</p>
<div class="cell" data-hold="true" data-execution_count="79">
<div class="sourceCode cell-code" id="cb123"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb123-1"><a href="#cb123-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> <span class="fu">F</span>() a[<span class="fl">1</span><span class="op">:</span><span class="fl">3</span>] dx[<span class="fl">1</span><span class="op">:</span><span class="fl">3</span>]</span>
<span id="cb123-2"><a href="#cb123-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb123-3"><a href="#cb123-3" aria-hidden="true" tabindex="-1"></a><span class="fu">sum</span>(<span class="fu">partial</span>(<span class="fu">F</span>(a<span class="op">...</span>), α, a) <span class="op">/</span> <span class="fu">factorial</span>(α) <span class="op">*</span> dx<span class="op">^</span>α <span class="cf">for</span> k <span class="kw">in</span> <span class="fl">0</span><span class="op">:</span><span class="fl">3</span> <span class="cf">for</span> α <span class="kw">in</span> <span class="fu">MultiIndex</span>.(<span class="fu">MultiIndices</span>(<span class="fl">3</span>, k))) <span class="co"># 3rd order</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="80">
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
\[
\frac{dx₁^{3} \frac{\partial^{3}}{\partial a₁^{3}} F{\left(a₁,a₂,a₃ \right)}}{6} + \frac{dx₁^{2} dx₂ \frac{\partial^{3}}{\partial a₂\partial a₁^{2}} F{\left(a₁,a₂,a₃ \right)}}{2} + \frac{dx₁^{2} dx₃ \frac{\partial^{3}}{\partial a₃\partial a₁^{2}} F{\left(a₁,a₂,a₃ \right)}}{2} + \frac{dx₁^{2} \frac{\partial^{2}}{\partial a₁^{2}} F{\left(a₁,a₂,a₃ \right)}}{2} + \frac{dx₁ dx₂^{2} \frac{\partial^{3}}{\partial a₂^{2}\partial a₁} F{\left(a₁,a₂,a₃ \right)}}{2} + dx₁ dx₂ dx₃ \frac{\partial^{3}}{\partial a₃\partial a₂\partial a₁} F{\left(a₁,a₂,a₃ \right)} + dx₁ dx₂ \frac{\partial^{2}}{\partial a₂\partial a₁} F{\left(a₁,a₂,a₃ \right)} + \frac{dx₁ dx₃^{2} \frac{\partial^{3}}{\partial a₃^{2}\partial a₁} F{\left(a₁,a₂,a₃ \right)}}{2} + dx₁ dx₃ \frac{\partial^{2}}{\partial a₃\partial a₁} F{\left(a₁,a₂,a₃ \right)} + dx₁ \frac{\partial}{\partial a₁} F{\left(a₁,a₂,a₃ \right)} + \frac{dx₂^{3} \frac{\partial^{3}}{\partial a₂^{3}} F{\left(a₁,a₂,a₃ \right)}}{6} + \frac{dx₂^{2} dx₃ \frac{\partial^{3}}{\partial a₃\partial a₂^{2}} F{\left(a₁,a₂,a₃ \right)}}{2} + \frac{dx₂^{2} \frac{\partial^{2}}{\partial a₂^{2}} F{\left(a₁,a₂,a₃ \right)}}{2} + \frac{dx₂ dx₃^{2} \frac{\partial^{3}}{\partial a₃^{2}\partial a₂} F{\left(a₁,a₂,a₃ \right)}}{2} + dx₂ dx₃ \frac{\partial^{2}}{\partial a₃\partial a₂} F{\left(a₁,a₂,a₃ \right)} + dx₂ \frac{\partial}{\partial a₂} F{\left(a₁,a₂,a₃ \right)} + \frac{dx₃^{3} \frac{\partial^{3}}{\partial a₃^{3}} F{\left(a₁,a₂,a₃ \right)}}{6} + \frac{dx₃^{2} \frac{\partial^{2}}{\partial a₃^{2}} F{\left(a₁,a₂,a₃ \right)}}{2} + dx₃ \frac{\partial}{\partial a₃} F{\left(a₁,a₂,a₃ \right)} + F{\left(a₁,a₂,a₃ \right)}
\]
</span>
</div>
</div>
</section>
</section>
<section id="questions" class="level2" data-number="56.7">
<h2 data-number="56.7" class="anchored" data-anchor-id="questions"><span class="header-section-number">56.7</span> Questions</h2>
<section id="question" class="level6">
<h6 class="anchored" data-anchor-id="question">Question</h6>
<p>Let <span class="math inline">\(f(x,y) = \sqrt{x + y}\)</span>. Find the tangent plane approximation for <span class="math inline">\(f(2.1, 2.2)\)</span>?</p>
<div class="cell" data-hold="true" data-execution_count="80">
<div class="cell-output cell-output-display" data-execution_count="81">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="13233366424090514597" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_13233366424090514597">
<div style="padding-top: 5px">
<br>
<div class="input-group">
<input id="13233366424090514597" type="number" class="form-control" placeholder="Numeric answer">
</div>
</div>
</div>
<div id="13233366424090514597_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.getElementById("13233366424090514597").addEventListener("change", function() {
var correct = (Math.abs(this.value - 2.075) <= 0.001);
var msgBox = document.getElementById('13233366424090514597_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_13233366424090514597")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_13233366424090514597")
if (explanation != null) {
explanation.style.display = "block";
}
}
});
</script>
</div>
</div>
</section>
<section id="question-1" class="level6">
<h6 class="anchored" data-anchor-id="question-1">Question</h6>
<p>Let <span class="math inline">\(f(x,y,z) = xy + yz + zx\)</span>. Using a <em>linear approximation</em> estimate <span class="math inline">\(f(1.1, 1.0, 0.9)\)</span>.</p>
<div class="cell" data-hold="true" data-execution_count="81">
<div class="cell-output cell-output-display" data-execution_count="82">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="5365736842222590422" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_5365736842222590422">
<div style="padding-top: 5px">
<br>
<div class="input-group">
<input id="5365736842222590422" type="number" class="form-control" placeholder="Numeric answer">
</div>
</div>
</div>
<div id="5365736842222590422_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.getElementById("5365736842222590422").addEventListener("change", function() {
var correct = (Math.abs(this.value - 3.0) <= 0.001);
var msgBox = document.getElementById('5365736842222590422_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_5365736842222590422")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_5365736842222590422")
if (explanation != null) {
explanation.style.display = "block";
}
}
});
</script>
</div>
</div>
</section>
<section id="question-2" class="level6">
<h6 class="anchored" data-anchor-id="question-2">Question</h6>
<p>Let <span class="math inline">\(f(x,y,z) = xy + yz + zx - 3\)</span>. What equation describes the tangent approximation at <span class="math inline">\((1,1,1)\)</span>?</p>
<div class="cell" data-hold="true" data-execution_count="82">
<div class="cell-output cell-output-display" data-execution_count="83">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="7947029377363123166" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_7947029377363123166">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_7947029377363123166_1">
<input class="form-check-input" type="radio" name="radio_7947029377363123166" id="radio_7947029377363123166_1" value="1">
<span class="label-body px-1">
\(x + y + z = 3\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_7947029377363123166_2">
<input class="form-check-input" type="radio" name="radio_7947029377363123166" id="radio_7947029377363123166_2" value="2">
<span class="label-body px-1">
\(2x + y - 2z = 1\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_7947029377363123166_3">
<input class="form-check-input" type="radio" name="radio_7947029377363123166" id="radio_7947029377363123166_3" value="3">
<span class="label-body px-1">
\(x + 2y + 3z = 6\)
</span>
</label>
</div>
</div>
</div>
<div id="7947029377363123166_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_7947029377363123166"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 1;
var msgBox = document.getElementById('7947029377363123166_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_7947029377363123166")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_7947029377363123166")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-3" class="level6">
<h6 class="anchored" data-anchor-id="question-3">Question</h6>
<p>(<a href="http://www.math.harvard.edu/~knill/teaching/summer2018/handouts/week4.pdf">Knill</a>) Let <span class="math inline">\(f(x,y) = xy + x^2y + xy^2\)</span>.</p>
<p>Find the gradient of <span class="math inline">\(f\)</span>:</p>
<div class="cell" data-hold="true" data-execution_count="83">
<div class="cell-output cell-output-display" data-execution_count="84">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="14815766950292398197" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_14815766950292398197">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_14815766950292398197_1">
<input class="form-check-input" type="radio" name="radio_14815766950292398197" id="radio_14815766950292398197_1" value="1">
<span class="label-body px-1">
\(\langle 2xy + y^2 + y, 2xy + x^2 + x\rangle\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_14815766950292398197_2">
<input class="form-check-input" type="radio" name="radio_14815766950292398197" id="radio_14815766950292398197_2" value="2">
<span class="label-body px-1">
\(\langle 2y + y^2, 2x + x^2\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_14815766950292398197_3">
<input class="form-check-input" type="radio" name="radio_14815766950292398197" id="radio_14815766950292398197_3" value="3">
<span class="label-body px-1">
\(y^2 + y, x^2 + x\)
</span>
</label>
</div>
</div>
</div>
<div id="14815766950292398197_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_14815766950292398197"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 1;
var msgBox = document.getElementById('14815766950292398197_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_14815766950292398197")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_14815766950292398197")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>Is this the Hessian of <span class="math inline">\(f\)</span>?</p>
<p><span class="math display">\[
\left[\begin{matrix}2 y &amp; 2 x + 2 y + 1\\2 x + 2 y + 1 &amp; 2 x\end{matrix}\right]
\]</span></p>
<div class="cell" data-hold="true" data-execution_count="84">
<div class="cell-output cell-output-display" data-execution_count="85">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="13960229907842992198" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_13960229907842992198">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_13960229907842992198_1">
<input class="form-check-input" type="radio" name="radio_13960229907842992198" id="radio_13960229907842992198_1" value="1">
<span class="label-body px-1">
Yes
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_13960229907842992198_2">
<input class="form-check-input" type="radio" name="radio_13960229907842992198" id="radio_13960229907842992198_2" value="2">
<span class="label-body px-1">
No
</span>
</label>
</div>
</div>
</div>
<div id="13960229907842992198_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_13960229907842992198"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 1;
var msgBox = document.getElementById('13960229907842992198_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_13960229907842992198")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_13960229907842992198")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>The point <span class="math inline">\((-1/3, -1/3)\)</span> is a solution to the <span class="math inline">\(\nabla{f} = 0\)</span>. What is the <em>determinant</em>, <span class="math inline">\(d\)</span>, of the Hessian at this point?</p>
<div class="cell" data-hold="true" data-execution_count="85">
<div class="cell-output cell-output-display" data-execution_count="86">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="7518211380486903856" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_7518211380486903856">
<div style="padding-top: 5px">
<br>
<div class="input-group">
<input id="7518211380486903856" type="number" class="form-control" placeholder="Numeric answer">
</div>
</div>
</div>
<div id="7518211380486903856_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.getElementById("7518211380486903856").addEventListener("change", function() {
var correct = (Math.abs(this.value - 0.3333333333333333) <= 0.001);
var msgBox = document.getElementById('7518211380486903856_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_7518211380486903856")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_7518211380486903856")
if (explanation != null) {
explanation.style.display = "block";
}
}
});
</script>
</div>
</div>
<p>Which is true of <span class="math inline">\(f\)</span> at <span class="math inline">\((-1/3, 1/3)\)</span>:</p>
<div class="cell" data-hold="true" data-execution_count="86">
<div class="cell-output cell-output-display" data-execution_count="87">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="16139257280058477085" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_16139257280058477085">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_16139257280058477085_1">
<input class="form-check-input" type="radio" name="radio_16139257280058477085" id="radio_16139257280058477085_1" value="1">
<span class="label-body px-1">
The function \(f\) has a local minimum, as \(f_{xx} &gt; 0\) and \(d &gt;0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_16139257280058477085_2">
<input class="form-check-input" type="radio" name="radio_16139257280058477085" id="radio_16139257280058477085_2" value="2">
<span class="label-body px-1">
The function \(f\) has a local maximum, as \(f_{xx} &lt; 0\) and \(d &gt;0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_16139257280058477085_3">
<input class="form-check-input" type="radio" name="radio_16139257280058477085" id="radio_16139257280058477085_3" value="3">
<span class="label-body px-1">
The function \(f\) has a saddle point, as \(d &lt; 0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_16139257280058477085_4">
<input class="form-check-input" type="radio" name="radio_16139257280058477085" id="radio_16139257280058477085_4" value="4">
<span class="label-body px-1">
Nothing can be said, as \(d=0\)
</span>
</label>
</div>
</div>
</div>
<div id="16139257280058477085_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_16139257280058477085"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('16139257280058477085_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_16139257280058477085")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_16139257280058477085")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-4" class="level5">
<h5 class="anchored" data-anchor-id="question-4">Question</h5>
<p>(<a href="http://www.math.harvard.edu/~knill/teaching/summer2018/handouts/week4.pdf">Knill</a>) Let the Tutte polynomial be <span class="math inline">\(f(x,y) = x + 2x^2 + x^3 + y + 2xy + y^2\)</span>.</p>
<p>Does this accurately find the gradient of <span class="math inline">\(f\)</span>?</p>
<div class="cell" data-hold="true" data-results="hidden" data-execution_count="87">
<div class="sourceCode cell-code" id="cb124"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb124-1"><a href="#cb124-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y) <span class="op">=</span> x <span class="op">+</span> <span class="fl">2</span>x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> x<span class="op">^</span><span class="fl">3</span> <span class="op">+</span> y <span class="op">+</span> <span class="fl">2</span>x<span class="op">*</span>y <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span></span>
<span id="cb124-2"><a href="#cb124-2" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x<span class="op">::</span><span class="dt">real </span>y<span class="op">::</span><span class="dt">real</span></span>
<span id="cb124-3"><a href="#cb124-3" aria-hidden="true" tabindex="-1"></a>gradf <span class="op">=</span> <span class="fu">gradient</span>(<span class="fu">f</span>(x,y), [x,y])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="88">
<pre><code>2-element Vector{Sym}:
3*x^2 + 4*x + 2*y + 1
2⋅x + 2⋅y + 1</code></pre>
</div>
</div>
<div class="cell" data-hold="true" data-execution_count="88">
<div class="cell-output cell-output-display" data-execution_count="89">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="6562392979068943485" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_6562392979068943485">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_6562392979068943485_1">
<input class="form-check-input" type="radio" name="radio_6562392979068943485" id="radio_6562392979068943485_1" value="1">
<span class="label-body px-1">
Yes
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_6562392979068943485_2">
<input class="form-check-input" type="radio" name="radio_6562392979068943485" id="radio_6562392979068943485_2" value="2">
<span class="label-body px-1">
No
</span>
</label>
</div>
</div>
</div>
<div id="6562392979068943485_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_6562392979068943485"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 1;
var msgBox = document.getElementById('6562392979068943485_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_6562392979068943485")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_6562392979068943485")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>How many answers does this find to <span class="math inline">\(\nabla{f} = \vec{0}\)</span>?</p>
<div class="cell" data-hold="true" data-results="hidden" data-execution_count="89">
<div class="sourceCode cell-code" id="cb126"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb126-1"><a href="#cb126-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y) <span class="op">=</span> x <span class="op">+</span> <span class="fl">2</span>x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> x<span class="op">^</span><span class="fl">3</span> <span class="op">+</span> y <span class="op">+</span> <span class="fl">2</span>x<span class="op">*</span>y <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span></span>
<span id="cb126-2"><a href="#cb126-2" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x<span class="op">::</span><span class="dt">real </span>y<span class="op">::</span><span class="dt">real</span></span>
<span id="cb126-3"><a href="#cb126-3" aria-hidden="true" tabindex="-1"></a>gradf <span class="op">=</span> <span class="fu">gradient</span>(<span class="fu">f</span>(x,y), [x,y])</span>
<span id="cb126-4"><a href="#cb126-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb126-5"><a href="#cb126-5" aria-hidden="true" tabindex="-1"></a><span class="fu">solve</span>(gradf, [x,y])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="90">
<pre><code>2-element Vector{Tuple{Sym, Sym}}:
(-2/3, 1/6)
(0, -1/2)</code></pre>
</div>
</div>
<div class="cell" data-hold="true" data-execution_count="90">
<div class="cell-output cell-output-display" data-execution_count="91">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="3948121091910850081" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_3948121091910850081">
<div style="padding-top: 5px">
<br>
<div class="input-group">
<input id="3948121091910850081" type="number" class="form-control" placeholder="Numeric answer">
</div>
</div>
</div>
<div id="3948121091910850081_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.getElementById("3948121091910850081").addEventListener("change", function() {
var correct = (Math.abs(this.value - 2) <= 0);
var msgBox = document.getElementById('3948121091910850081_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_3948121091910850081")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_3948121091910850081")
if (explanation != null) {
explanation.style.display = "block";
}
}
});
</script>
</div>
</div>
<p>The Hessian is found by</p>
<div class="cell" data-hold="true" data-execution_count="91">
<div class="sourceCode cell-code" id="cb128"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb128-1"><a href="#cb128-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y) <span class="op">=</span> x <span class="op">+</span> <span class="fl">2</span>x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> x<span class="op">^</span><span class="fl">3</span> <span class="op">+</span> y <span class="op">+</span> <span class="fl">2</span>x<span class="op">*</span>y <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span></span>
<span id="cb128-2"><a href="#cb128-2" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x<span class="op">::</span><span class="dt">real </span>y<span class="op">::</span><span class="dt">real</span></span>
<span id="cb128-3"><a href="#cb128-3" aria-hidden="true" tabindex="-1"></a>gradf <span class="op">=</span> <span class="fu">gradient</span>(<span class="fu">f</span>(x,y), [x,y])</span>
<span id="cb128-4"><a href="#cb128-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb128-5"><a href="#cb128-5" aria-hidden="true" tabindex="-1"></a>sympy.<span class="fu">hessian</span>(<span class="fu">f</span>(x,y), [x,y])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="92">
<pre><code>2×2 Matrix{Sym}:
6⋅x + 4 2
2 2</code></pre>
</div>
</div>
<p>Which is true of <span class="math inline">\(f\)</span> at <span class="math inline">\((-1/3, 1/3)\)</span>:</p>
<div class="cell" data-hold="true" data-execution_count="92">
<div class="cell-output cell-output-display" data-execution_count="93">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="17039922540856643607" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_17039922540856643607">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_17039922540856643607_1">
<input class="form-check-input" type="radio" name="radio_17039922540856643607" id="radio_17039922540856643607_1" value="1">
<span class="label-body px-1">
The function \(f\) has a local minimum, as \(f_{xx} &gt; 0\) and \(d &gt;0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_17039922540856643607_2">
<input class="form-check-input" type="radio" name="radio_17039922540856643607" id="radio_17039922540856643607_2" value="2">
<span class="label-body px-1">
The function \(f\) has a local maximum, as \(f_{xx} &lt; 0\) and \(d &gt;0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_17039922540856643607_3">
<input class="form-check-input" type="radio" name="radio_17039922540856643607" id="radio_17039922540856643607_3" value="3">
<span class="label-body px-1">
The function \(f\) has a saddle point, as \(d &lt; 0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_17039922540856643607_4">
<input class="form-check-input" type="radio" name="radio_17039922540856643607" id="radio_17039922540856643607_4" value="4">
<span class="label-body px-1">
Nothing can be said, as \(d=0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_17039922540856643607_5">
<input class="form-check-input" type="radio" name="radio_17039922540856643607" id="radio_17039922540856643607_5" value="5">
<span class="label-body px-1">
The test does not apply, as \(\nabla{f}\) is not \(0\) at this point.
</span>
</label>
</div>
</div>
</div>
<div id="17039922540856643607_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_17039922540856643607"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 3;
var msgBox = document.getElementById('17039922540856643607_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_17039922540856643607")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_17039922540856643607")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>Which is true of <span class="math inline">\(f\)</span> at <span class="math inline">\((0, -1/2)\)</span>:</p>
<div class="cell" data-hold="true" data-execution_count="93">
<div class="cell-output cell-output-display" data-execution_count="94">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="850424288700744543" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_850424288700744543">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_850424288700744543_1">
<input class="form-check-input" type="radio" name="radio_850424288700744543" id="radio_850424288700744543_1" value="1">
<span class="label-body px-1">
The function \(f\) has a local minimum, as \(f_{xx} &gt; 0\) and \(d &gt;0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_850424288700744543_2">
<input class="form-check-input" type="radio" name="radio_850424288700744543" id="radio_850424288700744543_2" value="2">
<span class="label-body px-1">
The function \(f\) has a local maximum, as \(f_{xx} &lt; 0\) and \(d &gt;0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_850424288700744543_3">
<input class="form-check-input" type="radio" name="radio_850424288700744543" id="radio_850424288700744543_3" value="3">
<span class="label-body px-1">
The function \(f\) has a saddle point, as \(d &lt; 0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_850424288700744543_4">
<input class="form-check-input" type="radio" name="radio_850424288700744543" id="radio_850424288700744543_4" value="4">
<span class="label-body px-1">
Nothing can be said, as \(d=0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_850424288700744543_5">
<input class="form-check-input" type="radio" name="radio_850424288700744543" id="radio_850424288700744543_5" value="5">
<span class="label-body px-1">
The test does not apply, as \(\nabla{f}\) is not \(0\) at this point.
</span>
</label>
</div>
</div>
</div>
<div id="850424288700744543_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_850424288700744543"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 1;
var msgBox = document.getElementById('850424288700744543_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_850424288700744543")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_850424288700744543")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>Which is true of <span class="math inline">\(f\)</span> at <span class="math inline">\((1/2, 0)\)</span>:</p>
<div class="cell" data-hold="true" data-execution_count="94">
<div class="cell-output cell-output-display" data-execution_count="95">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="13207764065677117311" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_13207764065677117311">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_13207764065677117311_1">
<input class="form-check-input" type="radio" name="radio_13207764065677117311" id="radio_13207764065677117311_1" value="1">
<span class="label-body px-1">
The function \(f\) has a local minimum, as \(f_{xx} &gt; 0\) and \(d &gt;0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_13207764065677117311_2">
<input class="form-check-input" type="radio" name="radio_13207764065677117311" id="radio_13207764065677117311_2" value="2">
<span class="label-body px-1">
The function \(f\) has a local maximum, as \(f_{xx} &lt; 0\) and \(d &gt;0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_13207764065677117311_3">
<input class="form-check-input" type="radio" name="radio_13207764065677117311" id="radio_13207764065677117311_3" value="3">
<span class="label-body px-1">
The function \(f\) has a saddle point, as \(d &lt; 0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_13207764065677117311_4">
<input class="form-check-input" type="radio" name="radio_13207764065677117311" id="radio_13207764065677117311_4" value="4">
<span class="label-body px-1">
Nothing can be said, as \(d=0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_13207764065677117311_5">
<input class="form-check-input" type="radio" name="radio_13207764065677117311" id="radio_13207764065677117311_5" value="5">
<span class="label-body px-1">
The test does not apply, as \(\nabla{f}\) is not \(0\) at this point.
</span>
</label>
</div>
</div>
</div>
<div id="13207764065677117311_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_13207764065677117311"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 5;
var msgBox = document.getElementById('13207764065677117311_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_13207764065677117311")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_13207764065677117311")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<section id="question-5" class="level6">
<h6 class="anchored" data-anchor-id="question-5">Question</h6>
<p>(Strang p509) Consider the quadratic function <span class="math inline">\(f(x,y) = ax^2 + bxy +cy^2\)</span>. Since the second partial derivative test is essentially done by replacing the function at a critical point by a quadratic function, understanding this <span class="math inline">\(f\)</span> is of some interest.</p>
<p>Is this the Hessian of <span class="math inline">\(f\)</span>?</p>
<p><span class="math display">\[
\left[
\begin{array}{}
2a &amp; 2b\\
2b &amp; 2c
\end{array}
\right]
\]</span></p>
<div class="cell" data-hold="true" data-execution_count="95">
<div class="cell-output cell-output-display" data-execution_count="96">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="16586251166787554818" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_16586251166787554818">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_16586251166787554818_1">
<input class="form-check-input" type="radio" name="radio_16586251166787554818" id="radio_16586251166787554818_1" value="1">
<span class="label-body px-1">
Yes
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_16586251166787554818_2">
<input class="form-check-input" type="radio" name="radio_16586251166787554818" id="radio_16586251166787554818_2" value="2">
<span class="label-body px-1">
No
</span>
</label>
</div>
</div>
</div>
<div id="16586251166787554818_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_16586251166787554818"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 1;
var msgBox = document.getElementById('16586251166787554818_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_16586251166787554818")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_16586251166787554818")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>Or is this the Hessian of <span class="math inline">\(f\)</span>?</p>
<p><span class="math display">\[
\left[
\begin{array}{}
2ax &amp; by\\
bx &amp; 2cy
\end{array}
\right]
\]</span></p>
<div class="cell" data-hold="true" data-execution_count="96">
<div class="cell-output cell-output-display" data-execution_count="97">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="3021134420935694381" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_3021134420935694381">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_3021134420935694381_1">
<input class="form-check-input" type="radio" name="radio_3021134420935694381" id="radio_3021134420935694381_1" value="1">
<span class="label-body px-1">
Yes
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_3021134420935694381_2">
<input class="form-check-input" type="radio" name="radio_3021134420935694381" id="radio_3021134420935694381_2" value="2">
<span class="label-body px-1">
No
</span>
</label>
</div>
</div>
</div>
<div id="3021134420935694381_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_3021134420935694381"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('3021134420935694381_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_3021134420935694381")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_3021134420935694381")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>Explain why <span class="math inline">\(ac - b^2\)</span> is of any interest here:</p>
<div class="cell" data-hold="true" data-execution_count="97">
<div class="cell-output cell-output-display" data-execution_count="98">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="15588285063080465774" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_15588285063080465774">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_15588285063080465774_1">
<input class="form-check-input" type="radio" name="radio_15588285063080465774" id="radio_15588285063080465774_1" value="1">
<span class="label-body px-1">
It isn't, \(b^2-4ac\) is from the quadratic formula
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_15588285063080465774_2">
<input class="form-check-input" type="radio" name="radio_15588285063080465774" id="radio_15588285063080465774_2" value="2">
<span class="label-body px-1">
It is the determinant of the Hessian
</span>
</label>
</div>
</div>
</div>
<div id="15588285063080465774_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_15588285063080465774"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('15588285063080465774_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_15588285063080465774")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_15588285063080465774")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>Which condition on <span class="math inline">\(a\)</span>, <span class="math inline">\(b\)</span>, and <span class="math inline">\(c\)</span> will ensure a <em>local maximum</em>:</p>
<div class="cell" data-hold="true" data-execution_count="98">
<div class="cell-output cell-output-display" data-execution_count="99">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="10802292869690826378" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_10802292869690826378">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_10802292869690826378_1">
<input class="form-check-input" type="radio" name="radio_10802292869690826378" id="radio_10802292869690826378_1" value="1">
<span class="label-body px-1">
That \(a&gt;0\) and \(ac-b^2 &gt; 0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_10802292869690826378_2">
<input class="form-check-input" type="radio" name="radio_10802292869690826378" id="radio_10802292869690826378_2" value="2">
<span class="label-body px-1">
That \(a&lt;0\) and \(ac-b^2 &gt; 0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_10802292869690826378_3">
<input class="form-check-input" type="radio" name="radio_10802292869690826378" id="radio_10802292869690826378_3" value="3">
<span class="label-body px-1">
That \(ac-b^2 &lt; 0\)
</span>
</label>
</div>
</div>
</div>
<div id="10802292869690826378_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_10802292869690826378"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('10802292869690826378_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_10802292869690826378")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_10802292869690826378")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>Which condition on <span class="math inline">\(a\)</span>, <span class="math inline">\(b\)</span>, and <span class="math inline">\(c\)</span> will ensure a saddle point?</p>
<div class="cell" data-hold="true" data-execution_count="99">
<div class="cell-output cell-output-display" data-execution_count="100">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="13134425429977985584" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_13134425429977985584">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_13134425429977985584_1">
<input class="form-check-input" type="radio" name="radio_13134425429977985584" id="radio_13134425429977985584_1" value="1">
<span class="label-body px-1">
That \(a&gt;0\) and \(ac-b^2 &gt; 0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_13134425429977985584_2">
<input class="form-check-input" type="radio" name="radio_13134425429977985584" id="radio_13134425429977985584_2" value="2">
<span class="label-body px-1">
That \(a&lt;0\) and \(ac-b^2 &gt; 0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_13134425429977985584_3">
<input class="form-check-input" type="radio" name="radio_13134425429977985584" id="radio_13134425429977985584_3" value="3">
<span class="label-body px-1">
That \(ac-b^2 &lt; 0\)
</span>
</label>
</div>
</div>
</div>
<div id="13134425429977985584_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_13134425429977985584"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 3;
var msgBox = document.getElementById('13134425429977985584_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_13134425429977985584")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_13134425429977985584")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-6" class="level6">
<h6 class="anchored" data-anchor-id="question-6">Question</h6>
<p>Let <span class="math inline">\(f(x,y) = e^{-x^2 - y^2} (2x^2 + y^2)\)</span>. Use Lagranges method to find the absolute maximum and absolute minimum over <span class="math inline">\(x^2 + y^2 = 3\)</span>.</p>
<p>Is <span class="math inline">\(\nabla{f}\)</span> given by the following?</p>
<p><span class="math display">\[
\nabla{f} =2 e^{-x^2 - y^2} \langle x(2 - 2x^2 - y^2), y(1 - 2x^2 - y^2)\rangle.
\]</span></p>
<div class="cell" data-hold="true" data-execution_count="100">
<div class="cell-output cell-output-display" data-execution_count="101">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="17844808118837065861" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_17844808118837065861">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_17844808118837065861_1">
<input class="form-check-input" type="radio" name="radio_17844808118837065861" id="radio_17844808118837065861_1" value="1">
<span class="label-body px-1">
Yes
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_17844808118837065861_2">
<input class="form-check-input" type="radio" name="radio_17844808118837065861" id="radio_17844808118837065861_2" value="2">
<span class="label-body px-1">
No
</span>
</label>
</div>
</div>
</div>
<div id="17844808118837065861_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_17844808118837065861"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 1;
var msgBox = document.getElementById('17844808118837065861_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_17844808118837065861")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_17844808118837065861")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>Which vector is orthogonal to the contour line <span class="math inline">\(x^2 + y^2 = 3\)</span>?</p>
<div class="cell" data-hold="true" data-execution_count="101">
<div class="cell-output cell-output-display" data-execution_count="102">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="8435621433221583597" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_8435621433221583597">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_8435621433221583597_1">
<input class="form-check-input" type="radio" name="radio_8435621433221583597" id="radio_8435621433221583597_1" value="1">
<span class="label-body px-1">
\(\langle 2x, 2y\rangle\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_8435621433221583597_2">
<input class="form-check-input" type="radio" name="radio_8435621433221583597" id="radio_8435621433221583597_2" value="2">
<span class="label-body px-1">
\(\langle 2x, y^2\rangle\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_8435621433221583597_3">
<input class="form-check-input" type="radio" name="radio_8435621433221583597" id="radio_8435621433221583597_3" value="3">
<span class="label-body px-1">
\(\langle x^2, 2y \rangle\)
</span>
</label>
</div>
</div>
</div>
<div id="8435621433221583597_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_8435621433221583597"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 1;
var msgBox = document.getElementById('8435621433221583597_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_8435621433221583597")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_8435621433221583597")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>Due to the form of the gradient of the constraint, finding when <span class="math inline">\(\nabla{f} = \lambda \nabla{g}\)</span> is the same as identifying when this ratio <span class="math inline">\(|f_x/f_y|\)</span> is <span class="math inline">\(1\)</span>. The following solves for this by checking each point on the constraint:</p>
<div class="cell" data-hold="true" data-execution_count="102">
<div class="sourceCode cell-code" id="cb130"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb130-1"><a href="#cb130-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y) <span class="op">=</span> <span class="fu">exp</span>(<span class="op">-</span>x<span class="op">^</span><span class="fl">2</span><span class="op">-</span>y<span class="op">^</span><span class="fl">2</span>) <span class="op">*</span> (<span class="fl">2</span>x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">2</span>)</span>
<span id="cb130-2"><a href="#cb130-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(v) <span class="op">=</span> <span class="fu">f</span>(v<span class="op">...</span>)</span>
<span id="cb130-3"><a href="#cb130-3" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>(t) <span class="op">=</span> <span class="fl">3</span><span class="op">*</span>[<span class="fu">cos</span>(t), <span class="fu">sin</span>(t)]</span>
<span id="cb130-4"><a href="#cb130-4" aria-hidden="true" tabindex="-1"></a><span class="fu">rat</span>(x) <span class="op">=</span> <span class="fu">abs</span>(x[<span class="fl">1</span>]<span class="op">/</span>x[<span class="fl">2</span>]) <span class="op">-</span> <span class="fl">1</span></span>
<span id="cb130-5"><a href="#cb130-5" aria-hidden="true" tabindex="-1"></a>fn <span class="op">=</span> rat <span class="op"></span> <span class="fu"></span>(f) <span class="op"></span> r</span>
<span id="cb130-6"><a href="#cb130-6" aria-hidden="true" tabindex="-1"></a>ts <span class="op">=</span> <span class="fu">fzeros</span>(fn, <span class="fl">0</span>, <span class="fl">2</span>pi)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="103">
<pre><code>4-element Vector{Float64}:
0.7449781871982899
2.396614466391503
3.886570840788083
5.538207119981296</code></pre>
</div>
</div>
<p>Using these points, what is the largest value on the boundary?</p>
<div class="cell" data-hold="true" data-execution_count="103">
<div class="cell-output cell-output-display" data-execution_count="104">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="187366048749738734" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_187366048749738734">
<div style="padding-top: 5px">
<br>
<div class="input-group">
<input id="187366048749738734" type="number" class="form-control" placeholder="Numeric answer">
</div>
</div>
</div>
<div id="187366048749738734_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.getElementById("187366048749738734").addEventListener("change", function() {
var correct = (Math.abs(this.value - 0.0017108774654683427) <= 0.001);
var msgBox = document.getElementById('187366048749738734_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_187366048749738734")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_187366048749738734")
if (explanation != null) {
explanation.style.display = "block";
}
}
});
</script>
</div>
</div>
</section>
</section>
</section>
</main> <!-- /main -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const clipboard = new window.ClipboardJS('.code-copy-button', {
target: function(trigger) {
return trigger.previousElementSibling;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
setTimeout(function() {
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
let href = ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const cites = ref.parentNode.getAttribute('data-cites').split(' ');
tippyHover(ref, function() {
var popup = window.document.createElement('div');
cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
var filterRegex = new RegExp('/' + window.location.host + '/');
var isInternal = (href) => {
return filterRegex.test(href) || localhostRegex.test(href);
}
// Inspect non-navigation links and adorn them if external
var links = window.document.querySelectorAll('a:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external)');
for (var i=0; i<links.length; i++) {
const link = links[i];
if (!isInternal(link.href)) {
// target, if specified
link.setAttribute("target", "_blank");
}
}
});
</script>
<nav class="page-navigation">
<div class="nav-page nav-page-previous">
<a href="../differentiable_vector_calculus/scalar_functions.html" class="pagination-link">
<i class="bi bi-arrow-left-short"></i> <span class="nav-page-text"><span class="chapter-number">55</span>&nbsp; <span class="chapter-title">Scalar functions</span></span>
</a>
</div>
<div class="nav-page nav-page-next">
<a href="../differentiable_vector_calculus/vector_fields.html" class="pagination-link">
<span class="nav-page-text"><span class="chapter-number">57</span>&nbsp; <span class="chapter-title">Functions <span class="math inline">\(R^n \rightarrow R^m\)</span></span></span> <i class="bi bi-arrow-right-short"></i>
</a>
</div>
</nav>
</div> <!-- /content -->
<footer class="footer">
<div class="nav-footer">
<div class="nav-footer-center">Copyright 2022, John Verzani</div>
</div>
</footer>
</body></html>