2185 lines
181 KiB
HTML
2185 lines
181 KiB
HTML
|
<!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 - 70 Quick introduction to Calculus with Julia</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="../references.html" rel="next">
|
|||
|
<link href="../misc/unicode.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">70</span> <span class="chapter-title">Quick introduction to Calculus with Julia</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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <span class="chapter-title">Newton’s 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> <span class="chapter-title">Derivative-free alternatives to Newton’s 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> <span class="chapter-title">L’Hospital’s 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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <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> <span class="chapter-title">Euler’s 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> <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> <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 collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-6" aria-expanded="false">Differential vector calculus</a>
|
|||
|
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-6" aria-expanded="false">
|
|||
|
<i class="bi bi-chevron-right ms-2"></i>
|
|||
|
</a>
|
|||
|
</div>
|
|||
|
<ul id="quarto-sidebar-section-6" class="collapse list-unstyled sidebar-section depth1 ">
|
|||
|
<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> <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> <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> <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> <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"><span class="chapter-number">56</span> <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> <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> <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> <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> <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> <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> <span class="chapter-title">Green’s 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> <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> <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> <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" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-9" aria-expanded="true">Appendices</a>
|
|||
|
<a class="sidebar-item-toggle text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-9" aria-expanded="true">
|
|||
|
<i class="bi bi-chevron-right ms-2"></i>
|
|||
|
</a>
|
|||
|
</div>
|
|||
|
<ul id="quarto-sidebar-section-9" class="collapse list-unstyled sidebar-section depth1 show">
|
|||
|
<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> <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> <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> <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> <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 active"><span class="chapter-number">70</span> <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="#types" id="toc-types" class="nav-link active" data-scroll-target="#types"> <span class="header-section-number">70.1</span> Types</a></li>
|
|||
|
<li><a href="#functions" id="toc-functions" class="nav-link" data-scroll-target="#functions"> <span class="header-section-number">70.2</span> Functions</a>
|
|||
|
<ul class="collapse">
|
|||
|
<li><a href="#definition" id="toc-definition" class="nav-link" data-scroll-target="#definition"> <span class="header-section-number">70.2.1</span> Definition</a></li>
|
|||
|
<li><a href="#calling-a-function" id="toc-calling-a-function" class="nav-link" data-scroll-target="#calling-a-function"> <span class="header-section-number">70.2.2</span> Calling a function</a></li>
|
|||
|
<li><a href="#multiple-dispatch" id="toc-multiple-dispatch" class="nav-link" data-scroll-target="#multiple-dispatch"> <span class="header-section-number">70.2.3</span> Multiple dispatch</a></li>
|
|||
|
<li><a href="#keyword-arguments" id="toc-keyword-arguments" class="nav-link" data-scroll-target="#keyword-arguments"> <span class="header-section-number">70.2.4</span> Keyword arguments</a></li>
|
|||
|
</ul></li>
|
|||
|
<li><a href="#symbolic-objects" id="toc-symbolic-objects" class="nav-link" data-scroll-target="#symbolic-objects"> <span class="header-section-number">70.3</span> Symbolic objects</a></li>
|
|||
|
<li><a href="#containers" id="toc-containers" class="nav-link" data-scroll-target="#containers"> <span class="header-section-number">70.4</span> Containers</a>
|
|||
|
<ul class="collapse">
|
|||
|
<li><a href="#structured-collections" id="toc-structured-collections" class="nav-link" data-scroll-target="#structured-collections"> <span class="header-section-number">70.4.1</span> Structured collections</a></li>
|
|||
|
</ul></li>
|
|||
|
<li><a href="#iteration" id="toc-iteration" class="nav-link" data-scroll-target="#iteration"> <span class="header-section-number">70.5</span> Iteration</a></li>
|
|||
|
<li><a href="#plots" id="toc-plots" class="nav-link" data-scroll-target="#plots"> <span class="header-section-number">70.6</span> Plots</a>
|
|||
|
<ul class="collapse">
|
|||
|
<li><a href="#plotting-a-univariate-function-fr-rightarrow-r" id="toc-plotting-a-univariate-function-fr-rightarrow-r" class="nav-link" data-scroll-target="#plotting-a-univariate-function-fr-rightarrow-r"> <span class="header-section-number">70.6.1</span> Plotting a univariate function <span class="math inline">\(f:R \rightarrow R\)</span></a></li>
|
|||
|
<li><a href="#plotting-a-parameterized-space-curve-function-fr-rightarrow-rn-n-2-or-3" id="toc-plotting-a-parameterized-space-curve-function-fr-rightarrow-rn-n-2-or-3" class="nav-link" data-scroll-target="#plotting-a-parameterized-space-curve-function-fr-rightarrow-rn-n-2-or-3"> <span class="header-section-number">70.6.2</span> Plotting a parameterized (space) curve function <span class="math inline">\(f:R \rightarrow R^n\)</span>, <span class="math inline">\(n = 2\)</span> or <span class="math inline">\(3\)</span></a></li>
|
|||
|
<li><a href="#plotting-a-scalar-function-fr2-rightarrow-r" id="toc-plotting-a-scalar-function-fr2-rightarrow-r" class="nav-link" data-scroll-target="#plotting-a-scalar-function-fr2-rightarrow-r"> <span class="header-section-number">70.6.3</span> Plotting a scalar function <span class="math inline">\(f:R^2 \rightarrow R\)</span></a></li>
|
|||
|
<li><a href="#plotting-a-parameterized-surface-fr2-rightarrow-r3" id="toc-plotting-a-parameterized-surface-fr2-rightarrow-r3" class="nav-link" data-scroll-target="#plotting-a-parameterized-surface-fr2-rightarrow-r3"> <span class="header-section-number">70.6.4</span> Plotting a parameterized surface <span class="math inline">\(f:R^2 \rightarrow R^3\)</span></a></li>
|
|||
|
<li><a href="#plotting-a-vector-field-fr2-rightarrow-r2." id="toc-plotting-a-vector-field-fr2-rightarrow-r2." class="nav-link" data-scroll-target="#plotting-a-vector-field-fr2-rightarrow-r2."> <span class="header-section-number">70.6.5</span> Plotting a vector field <span class="math inline">\(F:R^2 \rightarrow R^2\)</span>.</a></li>
|
|||
|
</ul></li>
|
|||
|
<li><a href="#limits" id="toc-limits" class="nav-link" data-scroll-target="#limits"> <span class="header-section-number">70.7</span> Limits</a></li>
|
|||
|
<li><a href="#derivatives" id="toc-derivatives" class="nav-link" data-scroll-target="#derivatives"> <span class="header-section-number">70.8</span> Derivatives</a>
|
|||
|
<ul class="collapse">
|
|||
|
<li><a href="#derivatives-of-univariate-functions" id="toc-derivatives-of-univariate-functions" class="nav-link" data-scroll-target="#derivatives-of-univariate-functions"> <span class="header-section-number">70.8.1</span> Derivatives of univariate functions</a></li>
|
|||
|
<li><a href="#partial-derivatives" id="toc-partial-derivatives" class="nav-link" data-scroll-target="#partial-derivatives"> <span class="header-section-number">70.8.2</span> Partial derivatives</a></li>
|
|||
|
<li><a href="#jacobian" id="toc-jacobian" class="nav-link" data-scroll-target="#jacobian"> <span class="header-section-number">70.8.3</span> Jacobian</a></li>
|
|||
|
<li><a href="#divergence" id="toc-divergence" class="nav-link" data-scroll-target="#divergence"> <span class="header-section-number">70.8.4</span> Divergence</a></li>
|
|||
|
<li><a href="#curl" id="toc-curl" class="nav-link" data-scroll-target="#curl"> <span class="header-section-number">70.8.5</span> Curl</a></li>
|
|||
|
</ul></li>
|
|||
|
<li><a href="#integrals" id="toc-integrals" class="nav-link" data-scroll-target="#integrals"> <span class="header-section-number">70.9</span> Integrals</a>
|
|||
|
<ul class="collapse">
|
|||
|
<li><a href="#integrals-of-univariate-functions" id="toc-integrals-of-univariate-functions" class="nav-link" data-scroll-target="#integrals-of-univariate-functions"> <span class="header-section-number">70.9.1</span> Integrals of univariate functions</a></li>
|
|||
|
<li><a href="#d-and-3d-iterated-integrals" id="toc-d-and-3d-iterated-integrals" class="nav-link" data-scroll-target="#d-and-3d-iterated-integrals"> <span class="header-section-number">70.9.2</span> 2D and 3D iterated integrals</a></li>
|
|||
|
<li><a href="#line-integrals" id="toc-line-integrals" class="nav-link" data-scroll-target="#line-integrals"> <span class="header-section-number">70.9.3</span> Line integrals</a></li>
|
|||
|
<li><a href="#surface-integrals" id="toc-surface-integrals" class="nav-link" data-scroll-target="#surface-integrals"> <span class="header-section-number">70.9.4</span> Surface integrals</a></li>
|
|||
|
</ul></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/misc/quick_notes.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">70</span> <span class="chapter-title">Quick introduction to Calculus with Julia</span></h1>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<div class="quarto-title-meta">
|
|||
|
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
</header>
|
|||
|
|
|||
|
<p>The <code>Julia</code> programming language with a design that makes it well suited as a supplement for the learning of calculus, as this collection of notes is intended to illustrate.</p>
|
|||
|
<p>As <code>Julia</code> is open source, it can be downloaded and used like many other programming languages.</p>
|
|||
|
<p>Julia can be used through the internet for free using the <a href="https://mybinder.org">mybinder.org</a> service. This link: <a href="https://mybinder.org/v2/gh/CalculusWithJulia/CwJScratchPad.git/master">launch binder</a> will take you to website that allows this. Just click on the <code>CalcululsWithJulia.ipynb</code> file after launching Binder by clicking on the badge. Binder provides the Jupyter interface.</p>
|
|||
|
<hr>
|
|||
|
<p>Here are some <code>Julia</code> usages to create calculus objects.</p>
|
|||
|
<p>The <code>Julia</code> packages loaded below are all loaded when the <code>CalculusWithJulia</code> package is loaded.</p>
|
|||
|
<p>A <code>Julia</code> package is loaded with the <code>using</code> command:</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">LinearAlgebra</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|||
|
<p>The <code>LinearAlgebra</code> package comes with a <code>Julia</code> installation. Other packages can be added. Something like:</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">using</span> <span class="bu">Pkg</span></span>
|
|||
|
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="bu">Pkg</span>.<span class="fu">add</span>(<span class="st">"SomePackageName"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|||
|
<p>These notes have an accompanying package, <code>CalculusWithJulia</code>, that when installed, as above, also installs most of the necessary packages to perform the examples.</p>
|
|||
|
<p>Packages need only be installed once, but they must be loaded into <em>each</em> session for which they will be used.</p>
|
|||
|
<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="im">using</span> <span class="bu">CalculusWithJulia</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|||
|
<p>Packages can also be loaded through <code>import PackageName</code>. Importing does not add the exported objects of a function into the namespace, so is used when there are possible name collisions.</p>
|
|||
|
<section id="types" class="level2" data-number="70.1">
|
|||
|
<h2 data-number="70.1" class="anchored" data-anchor-id="types"><span class="header-section-number">70.1</span> Types</h2>
|
|||
|
<p>Objects in <code>Julia</code> are “typed.” Common numeric types are <code>Float64</code>, <code>Int64</code> for floating point numbers and integers. Less used here are types like <code>Rational{Int64}</code>, specifying rational numbers with a numerator and denominator as <code>Int64</code>; or <code>Complex{Float64}</code>, specifying a comlex number with floating point components. Julia also has <code>BigFloat</code> and <code>BigInt</code> for arbitrary precision types. Typically, operations use “promotion” to ensure the combination of types is appropriate. Other useful types are <code>Function</code>, an abstract type describing functions; <code>Bool</code> for true and false values; <code>Sym</code> for symbolic values (through <code>SymPy</code>); and <code>Vector{Float64}</code> for vectors with floating point components.</p>
|
|||
|
<p>For the most part the type will not be so important, but it is useful to know that for some function calls the type of the argument will decide what method ultimately gets called. (This allows symbolic types to interact with Julia functions in an idiomatic manner.)</p>
|
|||
|
</section>
|
|||
|
<section id="functions" class="level2" data-number="70.2">
|
|||
|
<h2 data-number="70.2" class="anchored" data-anchor-id="functions"><span class="header-section-number">70.2</span> Functions</h2>
|
|||
|
<section id="definition" class="level3" data-number="70.2.1">
|
|||
|
<h3 data-number="70.2.1" class="anchored" data-anchor-id="definition"><span class="header-section-number">70.2.1</span> Definition</h3>
|
|||
|
<p>Functions can be defined four basic ways:</p>
|
|||
|
<ul>
|
|||
|
<li>one statement functions follow traditional mathematics notation:</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="5">
|
|||
|
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> <span class="fu">exp</span>(x) <span class="op">*</span> <span class="fl">2</span>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="5">
|
|||
|
<pre><code>f (generic function with 1 method)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<ul>
|
|||
|
<li>multi-statement functions are defined with the <code>function</code> keyword. The <code>end</code> statement ends the definition. The last evaluated command is returned. There is no need for explicit <code>return</code> statement, though it can be useful for control flow.</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-execution_count="6">
|
|||
|
<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">g</span>(x)</span>
|
|||
|
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> a <span class="op">=</span> <span class="fu">sin</span>(x)<span class="op">^</span><span class="fl">2</span></span>
|
|||
|
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> a <span class="op">+</span> a<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> a<span class="op">^</span><span class="fl">3</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="6">
|
|||
|
<pre><code>g (generic function with 1 method)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<ul>
|
|||
|
<li>Anonymous functions, useful for example, as arguments to other functions or as return values, are defined using an arrow, <code>-></code>, as follows:</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-execution_count="7">
|
|||
|
<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>fn <span class="op">=</span> x <span class="op">-></span> <span class="fu">sin</span>(<span class="fl">2</span>x)</span>
|
|||
|
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="fu">fn</span>(<span class="cn">pi</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="7">
|
|||
|
<pre><code>1.2246467991473532e-16</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>In the following, the defined function, <code>Derivative</code>, returns an anonymously defined function that uses a <code>Julia</code> package, loaded with <code>CalculusWithJulia</code>, to take a derivative:</p>
|
|||
|
<div class="cell" data-execution_count="8">
|
|||
|
<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="fu">Derivatve</span>(f<span class="op">::</span><span class="dt">Function</span>) <span class="op">=</span> x <span class="op">-></span> ForwardDiff.<span class="fu">derivative</span>(f, x) <span class="co"># ForwardDiff is loaded in CalculusWithJulia</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>Derivatve (generic function with 1 method)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>(The <code>D</code> function of <code>CalculusWithJulia</code> implements something similar.)</p>
|
|||
|
<ul>
|
|||
|
<li>Anonymous function may also be created using the <code>function</code> keyword.</li>
|
|||
|
</ul>
|
|||
|
<p>For mathematical functions <span class="math inline">\(f: R^n \rightarrow R^m\)</span> when <span class="math inline">\(n\)</span> or <span class="math inline">\(m\)</span> is bigger than 1 we have:</p>
|
|||
|
<ul>
|
|||
|
<li>When <span class="math inline">\(n =1\)</span> and <span class="math inline">\(m > 1\)</span> we use a “vector” for the return value</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="9">
|
|||
|
<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">r</span>(t) <span class="op">=</span> [<span class="fu">sin</span>(t), <span class="fu">cos</span>(t), 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="9">
|
|||
|
<pre><code>r (generic function with 1 method)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>(An alternative would be to create a vector of functions.)</p>
|
|||
|
<ul>
|
|||
|
<li>When <span class="math inline">\(n > 1\)</span> and <span class="math inline">\(m=1\)</span> we use multiple arguments or pass the arguments in a container. This pattern is common, as it allows both calling styles.</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="10">
|
|||
|
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x, y, z) <span class="op">=</span> x<span class="op">*</span>y <span class="op">+</span> y<span class="op">*</span>z <span class="op">+</span> z<span class="op">*</span>x</span>
|
|||
|
<span id="cb14-2"><a href="#cb14-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></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>f (generic function with 2 methods)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Some functions need to pass in a container of values, for this the last definition is useful to expand the values. Splatting takes a container and treats the values like individual arguments.</p>
|
|||
|
<p>Alternatively, indexing can be used directly, as in:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="11">
|
|||
|
<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">f</span>(x) <span class="op">=</span> x[<span class="fl">1</span>]<span class="op">*</span>x[<span class="fl">2</span>] <span class="op">+</span> x[<span class="fl">2</span>]<span class="op">*</span>x[<span class="fl">3</span>] <span class="op">+</span> x[<span class="fl">3</span>]<span class="op">*</span>x[<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="11">
|
|||
|
<pre><code>f (generic function with 2 methods)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<ul>
|
|||
|
<li>For vector fields (<span class="math inline">\(n,m > 1\)</span>) a combination is used:</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="12">
|
|||
|
<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">F</span>(x,y,z) <span class="op">=</span> [<span class="op">-</span>y, x, z]</span>
|
|||
|
<span id="cb18-2"><a href="#cb18-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></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>F (generic function with 2 methods)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
<section id="calling-a-function" class="level3" data-number="70.2.2">
|
|||
|
<h3 data-number="70.2.2" class="anchored" data-anchor-id="calling-a-function"><span class="header-section-number">70.2.2</span> Calling a function</h3>
|
|||
|
<p>Functions are called using parentheses to group the arguments.</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="13">
|
|||
|
<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>(t) <span class="op">=</span> <span class="fu">sin</span>(t)<span class="fu">*sqrt</span>(t)</span>
|
|||
|
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a><span class="fu">sin</span>(<span class="fl">1</span>), <span class="fu">sqrt</span>(<span class="fl">1</span>), <span class="fu">f</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="13">
|
|||
|
<pre><code>(0.8414709848078965, 1.0, 0.8414709848078965)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>When a function has multiple arguments, yet the value passed in is a container holding the arguments, splatting is used to expand the arguments, as is done in the definition <code>F(v) = F(v...)</code>, above.</p>
|
|||
|
</section>
|
|||
|
<section id="multiple-dispatch" class="level3" data-number="70.2.3">
|
|||
|
<h3 data-number="70.2.3" class="anchored" data-anchor-id="multiple-dispatch"><span class="header-section-number">70.2.3</span> Multiple dispatch</h3>
|
|||
|
<p><code>Julia</code> can have many methods for a single generic function. (E.g., it can have many different implementations of addiion when the <code>+</code> sign is encountered.) The <em>type</em>s of the arguments and the number of arguments are used for dispatch.</p>
|
|||
|
<p>Here the number of arguments is used:</p>
|
|||
|
<div class="cell" data-execution_count="14">
|
|||
|
<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="fu">Area</span>(w, h) <span class="op">=</span> w <span class="op">*</span> h <span class="co"># area of rectangle</span></span>
|
|||
|
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a><span class="fu">Area</span>(w) <span class="op">=</span> <span class="fu">Area</span>(w, w) <span class="co"># area of square using area of rectangle defintion</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>Area (generic function with 2 methods)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Calling <code>Area(5)</code> will call <code>Area(5,5)</code> which will return <code>5*5</code>.</p>
|
|||
|
<p>Similarly, the definition for a vector field:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="15">
|
|||
|
<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="fu">F</span>(x,y,z) <span class="op">=</span> [<span class="op">-</span>y, x, z]</span>
|
|||
|
<span id="cb24-2"><a href="#cb24-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></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>F (generic function with 2 methods)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>takes advantage of multiple dispatch to allow either a vector argument or individual arguments.</p>
|
|||
|
<p>Type parameters can be used to restrict the type of arguments that are permitted. The <code>Derivative(f::Function)</code> definition illustrates how the <code>Derivative</code> function, defined above, is restricted to <code>Function</code> objects.</p>
|
|||
|
</section>
|
|||
|
<section id="keyword-arguments" class="level3" data-number="70.2.4">
|
|||
|
<h3 data-number="70.2.4" class="anchored" data-anchor-id="keyword-arguments"><span class="header-section-number">70.2.4</span> Keyword arguments</h3>
|
|||
|
<p>Optional arguments may be specified with keywords, when the function is defined to use them. Keywords are separated from positional arguments using a semicolon, <code>;</code>:</p>
|
|||
|
<div class="cell" data-execution_count="16">
|
|||
|
<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">circle</span>(x; r<span class="op">=</span><span class="fl">1</span>) <span class="op">=</span> <span class="fu">sqrt</span>(r<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> x<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">circle</span>(<span class="fl">0.5</span>), <span class="fu">circle</span>(<span class="fl">0.5</span>, r<span class="op">=</span><span class="fl">10</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>(0.8660254037844386, 9.987492177719089)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The main (but not sole) use of keyword arguments will be with plotting, where various plot attribute are passed as <code>key=value</code> pairs.</p>
|
|||
|
</section>
|
|||
|
</section>
|
|||
|
<section id="symbolic-objects" class="level2" data-number="70.3">
|
|||
|
<h2 data-number="70.3" class="anchored" data-anchor-id="symbolic-objects"><span class="header-section-number">70.3</span> Symbolic objects</h2>
|
|||
|
<p>The add-on <code>SymPy</code> package allows for symbolic expressions to be used. Symbolic values are defined with <code>@syms</code>, as below.</p>
|
|||
|
<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="im">using</span> <span class="bu">SymPy</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|||
|
<div class="cell" data-execution_count="18">
|
|||
|
<div class="sourceCode cell-code" id="cb29"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x y z</span>
|
|||
|
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a>x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> y<span class="op">^</span><span class="fl">3</span> <span class="op">+</span> 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="18">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
x^{2} + y^{3} + z
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Assumptions on the variables can be useful, particularly with simplification, as in</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="19">
|
|||
|
<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="pp">@syms</span> x<span class="op">::</span><span class="dt">real </span>y<span class="op">::</span><span class="dt">integer </span>z<span class="op">::</span><span class="dt">positive</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>(x, y, z)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Symbolic expressions flow through <code>Julia</code> functions symbolically</p>
|
|||
|
<div class="cell" data-execution_count="20">
|
|||
|
<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="fu">sin</span>(x)<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> <span class="fu">cos</span>(x)<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="20">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
\sin^{2}{\left(x \right)} + \cos^{2}{\left(x \right)}
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Numbers are symbolic once <code>SymPy</code> interacts with them:</p>
|
|||
|
<div class="cell" data-execution_count="21">
|
|||
|
<div class="sourceCode cell-code" id="cb33"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a>x <span class="op">-</span> x <span class="op">+</span> <span class="fl">1</span> <span class="co"># 1 is now symbolic</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">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
1
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The number <code>PI</code> is a symbolic <code>pi</code>.</p>
|
|||
|
<div class="cell" data-execution_count="22">
|
|||
|
<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><span class="fu">sin</span>(PI), <span class="fu">sin</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="22">
|
|||
|
<pre><code>(0, 1.2246467991473532e-16)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Use <code>Sym</code> to create symbolic numbers, <code>N</code> to find a <code>Julia</code> number from a symbolic number:</p>
|
|||
|
<div class="cell" data-execution_count="23">
|
|||
|
<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="fl">1</span> <span class="op">/</span> <span class="fu">Sym</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="23">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
\frac{1}{2}
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="cell" data-execution_count="24">
|
|||
|
<div class="sourceCode cell-code" id="cb37"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="fu">N</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="24">
|
|||
|
<pre><code>π = 3.1415926535897...</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Many generic <code>Julia</code> functions will work with symbolic objects through multiple dispatch (e.g., <code>sin</code>, <code>cos</code>, …). Sympy functions that are not in <code>Julia</code> can be accessed through the <code>sympy</code> object using dot-call notation:</p>
|
|||
|
<div class="cell" data-execution_count="25">
|
|||
|
<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>sympy.<span class="fu">harmonic</span>(<span class="fl">10</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="25">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
\frac{7381}{2520}
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Some Sympy methods belong to the object and a called via the pattern <code>object.method(...)</code>. This too is the case using SymPy with <code>Julia</code>. For example:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="26">
|
|||
|
<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>A <span class="op">=</span> [x <span class="fl">1</span>; x <span class="fl">2</span>]</span>
|
|||
|
<span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a>A.<span class="fu">det</span>() <span class="co"># determinant of symbolic matrix 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="26">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
x
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
<section id="containers" class="level2" data-number="70.4">
|
|||
|
<h2 data-number="70.4" class="anchored" data-anchor-id="containers"><span class="header-section-number">70.4</span> Containers</h2>
|
|||
|
<p>We use a few different containers:</p>
|
|||
|
<ul>
|
|||
|
<li>Tuples. These are objects grouped together using parentheses. They need not be of the same type</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-execution_count="27">
|
|||
|
<div class="sourceCode cell-code" id="cb41"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a>x1 <span class="op">=</span> (<span class="fl">1</span>, <span class="st">"two"</span>, <span class="fl">3.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="27">
|
|||
|
<pre><code>(1, "two", 3.0)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Tuples are useful for programming. For example, they are uesd to return multiple values from a function.</p>
|
|||
|
<ul>
|
|||
|
<li>Vectors. These are objects of the same type (typically) grouped together using square brackets, values separated by commas:</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-execution_count="28">
|
|||
|
<div class="sourceCode cell-code" id="cb43"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>x2 <span class="op">=</span> [<span class="fl">1</span>, <span class="fl">2</span>, <span class="fl">3.0</span>] <span class="co"># 3.0 makes theses all floating point</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="28">
|
|||
|
<pre><code>3-element Vector{Float64}:
|
|||
|
1.0
|
|||
|
2.0
|
|||
|
3.0</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Unlike tuples, the expected arithmatic from Linear Algebra is implemented for vectors.</p>
|
|||
|
<ul>
|
|||
|
<li>Matrices. Like vectors, combine values of the same type, only they are 2-dimensional. Use spaces to separate values along a row; semicolons to separate rows:</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-execution_count="29">
|
|||
|
<div class="sourceCode cell-code" id="cb45"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a>x3 <span class="op">=</span> [<span class="fl">1</span> <span class="fl">2</span> <span class="fl">3</span>; <span class="fl">4</span> <span class="fl">5</span> <span class="fl">6</span>; <span class="fl">7</span> <span class="fl">8</span> <span class="fl">9</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>3×3 Matrix{Int64}:
|
|||
|
1 2 3
|
|||
|
4 5 6
|
|||
|
7 8 9</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<ul>
|
|||
|
<li>Row vectors. A vector is 1 dimensional, though it may be identified as a column of two dimensional matrix. A row vector is a two-dimensional matrix with a single row:</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-execution_count="30">
|
|||
|
<div class="sourceCode cell-code" id="cb47"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a>x4 <span class="op">=</span> [<span class="fl">1</span> <span class="fl">2</span> <span class="fl">3.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>1×3 Matrix{Float64}:
|
|||
|
1.0 2.0 3.0</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>These have <em>indexing</em> using square brackets:</p>
|
|||
|
<div class="cell" data-execution_count="31">
|
|||
|
<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>x1[<span class="fl">1</span>], x2[<span class="fl">2</span>], x3[<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="31">
|
|||
|
<pre><code>(1, 2.0, 7)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Matrices are usually indexed by row and column:</p>
|
|||
|
<div class="cell" data-execution_count="32">
|
|||
|
<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>x3[<span class="fl">1</span>,<span class="fl">2</span>] <span class="co"># row one column two</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="32">
|
|||
|
<pre><code>2</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>For vectors and matrices - but not tuples, as they are immutable - indexing can be used to change a value in the container:</p>
|
|||
|
<div class="cell" data-execution_count="33">
|
|||
|
<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>x2[<span class="fl">1</span>], x3[<span class="fl">1</span>,<span class="fl">1</span>] <span class="op">=</span> <span class="fl">2</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="33">
|
|||
|
<pre><code>(2, 2)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Vectors and matrices are arrays. As hinted above, arrays have mathematical operations, such as addition and subtraction, defined for them. Tuples do not.</p>
|
|||
|
<p>Destructuring is an alternative to indexing to get at the entries in certain containers:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="34">
|
|||
|
<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>a,b,c <span class="op">=</span> x2</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{Float64}:
|
|||
|
2.0
|
|||
|
2.0
|
|||
|
3.0</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<section id="structured-collections" class="level3" data-number="70.4.1">
|
|||
|
<h3 data-number="70.4.1" class="anchored" data-anchor-id="structured-collections"><span class="header-section-number">70.4.1</span> Structured collections</h3>
|
|||
|
<p>An arithmetic progression, <span class="math inline">\(a, a+h, a+2h, ..., b\)</span> can be produced <em>efficiently</em> using the range operator <code>a:h:b</code>:</p>
|
|||
|
<div class="cell" data-execution_count="35">
|
|||
|
<div class="sourceCode cell-code" id="cb57"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a><span class="fl">5</span><span class="op">:</span><span class="fl">10</span><span class="op">:</span><span class="fl">55</span> <span class="co"># an object that describes 5, 15, 25, 35, 45, 55</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="35">
|
|||
|
<pre><code>5:10:55</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>If <code>h=1</code> it can be omitted:</p>
|
|||
|
<div class="cell" data-execution_count="36">
|
|||
|
<div class="sourceCode cell-code" id="cb59"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a><span class="fl">1</span><span class="op">:</span><span class="fl">10</span> <span class="co"># an object that describes 1,2,3,4,5,6,7,8,9,10</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="36">
|
|||
|
<pre><code>1:10</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The <code>range</code> function can <em>efficiently</em> describe <span class="math inline">\(n\)</span> evenly spaced points between <code>a</code> and <code>b</code>:</p>
|
|||
|
<div class="cell" data-execution_count="37">
|
|||
|
<div class="sourceCode cell-code" id="cb61"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a><span class="fu">range</span>(<span class="fl">0</span>, <span class="cn">pi</span>, length<span class="op">=</span><span class="fl">5</span>) <span class="co"># range(a, stop=b, length=n) for version 1.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="37">
|
|||
|
<pre><code>0.0:0.7853981633974483:3.141592653589793</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>This is useful for creating regularly spaced values needed for certain plots.</p>
|
|||
|
</section>
|
|||
|
</section>
|
|||
|
<section id="iteration" class="level2" data-number="70.5">
|
|||
|
<h2 data-number="70.5" class="anchored" data-anchor-id="iteration"><span class="header-section-number">70.5</span> Iteration</h2>
|
|||
|
<p>The <code>for</code> keyword is useful for iteration, Here is a traditional for loop, as <code>i</code> loops over each entry of the vector <code>[1,2,3]</code>:</p>
|
|||
|
<div class="cell" data-execution_count="38">
|
|||
|
<div class="sourceCode cell-code" id="cb63"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb63-1"><a href="#cb63-1" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i <span class="kw">in</span> [<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">3</span>]</span>
|
|||
|
<span id="cb63-2"><a href="#cb63-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">println</span>(i)</span>
|
|||
|
<span id="cb63-3"><a href="#cb63-3" aria-hidden="true" tabindex="-1"></a><span class="cf">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-stdout">
|
|||
|
<pre><code>1
|
|||
|
2
|
|||
|
3</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<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">
|
|||
|
<p>Technical aside: For assignment within a for loop at the global level, a <code>global</code> declaration may be needed to ensure proper scoping.</p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>List comprehensions are similar, but are useful as they perform the iteration and collect the values:</p>
|
|||
|
<div class="cell" data-execution_count="39">
|
|||
|
<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>[i<span class="op">^</span><span class="fl">2</span> for i <span class="kw">in</span> [<span class="fl">1</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="39">
|
|||
|
<pre><code>3-element Vector{Int64}:
|
|||
|
1
|
|||
|
4
|
|||
|
9</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Comprehesions can also be used to make matrices</p>
|
|||
|
<div class="cell" data-execution_count="40">
|
|||
|
<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>[<span class="fl">1</span><span class="op">/</span>(i<span class="op">+</span>j) for i <span class="kw">in</span> <span class="fl">1</span><span class="op">:</span><span class="fl">3</span>, j <span class="kw">in</span> <span class="fl">1</span><span class="op">:</span><span class="fl">4</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>3×4 Matrix{Float64}:
|
|||
|
0.5 0.333333 0.25 0.2
|
|||
|
0.333333 0.25 0.2 0.166667
|
|||
|
0.25 0.2 0.166667 0.142857</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>(The three rows are for <code>i=1</code>, then <code>i=2</code>, and finally for <code>i=3</code>.)</p>
|
|||
|
<p>Comprehensions apply an <em>expression</em> to each entry in a container through iteration. Applying a function to each entry of a container can be facilitated by:</p>
|
|||
|
<ul>
|
|||
|
<li>Broadcasting. Using <code>.</code> before an operation instructs <code>Julia</code> to match up sizes (possibly extending to do so) and then apply the operation element by element:</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-execution_count="41">
|
|||
|
<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>xs <span class="op">=</span> [<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">3</span>]</span>
|
|||
|
<span id="cb69-2"><a href="#cb69-2" aria-hidden="true" tabindex="-1"></a><span class="fu">sin</span>.(xs) <span class="co"># sin(1), sin(2), sin(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="41">
|
|||
|
<pre><code>3-element Vector{Float64}:
|
|||
|
0.8414709848078965
|
|||
|
0.9092974268256817
|
|||
|
0.1411200080598672</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>This example pairs off the value in <code>bases</code> and <code>xs</code>:</p>
|
|||
|
<div class="cell" data-execution_count="42">
|
|||
|
<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>bases <span class="op">=</span> [<span class="fl">5</span>,<span class="fl">5</span>,<span class="fl">10</span>]</span>
|
|||
|
<span id="cb71-2"><a href="#cb71-2" aria-hidden="true" tabindex="-1"></a><span class="fu">log</span>.(bases, xs) <span class="co"># log(5, 1), log(5,2), log(10, 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="42">
|
|||
|
<pre><code>3-element Vector{Float64}:
|
|||
|
0.0
|
|||
|
0.43067655807339306
|
|||
|
0.47712125471966244</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>This example broadcasts the scalar value for the base with <code>xs</code>:</p>
|
|||
|
<div class="cell" data-execution_count="43">
|
|||
|
<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">log</span>.(<span class="fl">5</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="43">
|
|||
|
<pre><code>3-element Vector{Float64}:
|
|||
|
0.0
|
|||
|
0.43067655807339306
|
|||
|
0.6826061944859854</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Row and column vectors can fill in:</p>
|
|||
|
<div class="cell" data-execution_count="44">
|
|||
|
<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>ys <span class="op">=</span> [<span class="fl">4</span> <span class="fl">5</span>] <span class="co"># a row vector</span></span>
|
|||
|
<span id="cb75-2"><a href="#cb75-2" aria-hidden="true" tabindex="-1"></a><span class="fu">h</span>(x,y) <span class="op">=</span> (x,y)</span>
|
|||
|
<span id="cb75-3"><a href="#cb75-3" aria-hidden="true" tabindex="-1"></a><span class="fu">h</span>.(xs, ys) <span class="co"># broadcasting a column and row vector makes a matrix, then applies f.</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="44">
|
|||
|
<pre><code>3×2 Matrix{Tuple{Int64, Int64}}:
|
|||
|
(1, 4) (1, 5)
|
|||
|
(2, 4) (2, 5)
|
|||
|
(3, 4) (3, 5)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>This should be contrasted to the case when both <code>xs</code> and <code>ys</code> are (column) vectors, as then they pair off (and here cause a dimension mismatch as they have different lengths):</p>
|
|||
|
<div class="cell" data-execution_count="45">
|
|||
|
<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="fu">h</span>.(xs, [<span class="fl">4</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-error">
|
|||
|
<pre><code>LoadError: DimensionMismatch("arrays could not be broadcast to a common size; got a dimension with lengths 3 and 2")</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<ul>
|
|||
|
<li>The <code>map</code> function is similar, it applies a function to each element:</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-execution_count="46">
|
|||
|
<div class="sourceCode cell-code" id="cb79"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb79-1"><a href="#cb79-1" aria-hidden="true" tabindex="-1"></a><span class="fu">map</span>(sin, [<span class="fl">1</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="46">
|
|||
|
<pre><code>3-element Vector{Float64}:
|
|||
|
0.8414709848078965
|
|||
|
0.9092974268256817
|
|||
|
0.1411200080598672</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<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">
|
|||
|
<p>Many different computer languages implement <code>map</code>, broadcasting is less common. <code>Julia</code>’s use of the dot syntax to indicate broadcasting is reminiscent of MATLAB, but is quite different.</p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
<section id="plots" class="level2" data-number="70.6">
|
|||
|
<h2 data-number="70.6" class="anchored" data-anchor-id="plots"><span class="header-section-number">70.6</span> Plots</h2>
|
|||
|
<p>The following commands use the <code>Plots</code> package. The <code>Plots</code> package expects a choice of backend. We will use <code>gr</code> unless, but other can be substituted by calling an appropriate command, suchas <code>pyplot()</code> or <code>plotly()</code>.</p>
|
|||
|
<div class="sourceCode cell-code" id="cb81"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb81-1"><a href="#cb81-1" aria-hidden="true" tabindex="-1"></a><span class="im">using</span> <span class="bu">Plots</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|||
|
<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">
|
|||
|
<p>The <code>plotly</code> backend and <code>gr</code> backends are available by default. The <code>plotly</code> backend is has some interactivity, <code>gr</code> is for static plots. The <code>pyplot</code> package is used for certain surface plots, when <code>gr</code> can not be used.</p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<section id="plotting-a-univariate-function-fr-rightarrow-r" class="level3" data-number="70.6.1">
|
|||
|
<h3 data-number="70.6.1" class="anchored" data-anchor-id="plotting-a-univariate-function-fr-rightarrow-r"><span class="header-section-number">70.6.1</span> Plotting a univariate function <span class="math inline">\(f:R \rightarrow R\)</span></h3>
|
|||
|
<ul>
|
|||
|
<li>using <code>plot(f, a, b)</code></li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-execution_count="48">
|
|||
|
<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">plot</span>(sin, <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="48">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-49-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Or</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="49">
|
|||
|
<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><span class="fu">f</span>(x) <span class="op">=</span> <span class="fu">exp</span>(<span class="op">-</span>x<span class="op">/</span><span class="fl">2</span>pi)<span class="fu">*sin</span>(x)</span>
|
|||
|
<span id="cb83-2"><a href="#cb83-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(f, <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="49">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-50-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Or with an anonymous function</p>
|
|||
|
<div class="cell" data-execution_count="50">
|
|||
|
<div class="sourceCode cell-code" id="cb84"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb84-1"><a href="#cb84-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(x <span class="op">-></span> <span class="fu">sin</span>(x) <span class="op">+</span> <span class="fu">sin</span>(<span class="fl">2</span>x), <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="50">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-51-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<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">
|
|||
|
<p>The time to first plot can be lengthy! This can be removed by creating a custom <code>Julia</code> image, but that is not introductory level stuff. As well, standalone plotting packages offer quicker first plots, but the simplicity of <code>Plots</code> is preferred. Subsequent plots are not so time consuming, as the initial time is spent compiling functions so their re-use is speedy.</p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Arguments of interest include</p>
|
|||
|
<table class="table">
|
|||
|
<colgroup>
|
|||
|
<col style="width: 22%">
|
|||
|
<col style="width: 77%">
|
|||
|
</colgroup>
|
|||
|
<thead>
|
|||
|
<tr class="header">
|
|||
|
<th style="text-align: center;">Attribute</th>
|
|||
|
<th style="text-align: center;">Value</th>
|
|||
|
</tr>
|
|||
|
</thead>
|
|||
|
<tbody>
|
|||
|
<tr class="odd">
|
|||
|
<td style="text-align: center;"><code>legend</code></td>
|
|||
|
<td style="text-align: center;">A boolean, specify <code>false</code> to inhibit drawing a legend</td>
|
|||
|
</tr>
|
|||
|
<tr class="even">
|
|||
|
<td style="text-align: center;"><code>aspect_ratio</code></td>
|
|||
|
<td style="text-align: center;">Use <code>:equal</code> to have x and y axis have same scale</td>
|
|||
|
</tr>
|
|||
|
<tr class="odd">
|
|||
|
<td style="text-align: center;"><code>linewidth</code></td>
|
|||
|
<td style="text-align: center;">Ingters greater than 1 will thicken lines drawn</td>
|
|||
|
</tr>
|
|||
|
<tr class="even">
|
|||
|
<td style="text-align: center;"><code>color</code></td>
|
|||
|
<td style="text-align: center;">A color may be specified by a symbol (leading <code>:</code>).</td>
|
|||
|
</tr>
|
|||
|
<tr class="odd">
|
|||
|
<td style="text-align: center;"></td>
|
|||
|
<td style="text-align: center;">E.g., <code>:black</code>, <code>:red</code>, <code>:blue</code></td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
<ul>
|
|||
|
<li>using <code>plot(xs, ys)</code></li>
|
|||
|
</ul>
|
|||
|
<p>The lower level interface to <code>plot</code> involves directly creating x and y values to plot:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="51">
|
|||
|
<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>xs <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">2</span>pi, length<span class="op">=</span><span class="fl">100</span>)</span>
|
|||
|
<span id="cb85-2"><a href="#cb85-2" aria-hidden="true" tabindex="-1"></a>ys <span class="op">=</span> <span class="fu">sin</span>.(xs)</span>
|
|||
|
<span id="cb85-3"><a href="#cb85-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(xs, ys, color<span class="op">=:</span>red)</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">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-52-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<ul>
|
|||
|
<li>plotting a symbolic expression</li>
|
|||
|
</ul>
|
|||
|
<p>A symbolic expression of single variable can be plotted as a function is:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="52">
|
|||
|
<div class="sourceCode cell-code" id="cb86"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb86-1"><a href="#cb86-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x</span>
|
|||
|
<span id="cb86-2"><a href="#cb86-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">exp</span>(<span class="op">-</span>x<span class="op">/</span><span class="fl">2</span>pi)<span class="fu">*sin</span>(x), <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="52">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-53-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<ul>
|
|||
|
<li>Multiple functions</li>
|
|||
|
</ul>
|
|||
|
<p>The <code>!</code> Julia convention to modify an object is used by the <code>plot</code> command, so <code>plot!</code> will add to the existing plot:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="53">
|
|||
|
<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="fu">plot</span>(sin, <span class="fl">0</span>, <span class="fl">2</span>pi, color<span class="op">=:</span>red)</span>
|
|||
|
<span id="cb87-2"><a href="#cb87-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot!</span>(cos, <span class="fl">0</span>, <span class="fl">2</span>pi, color<span class="op">=:</span>blue)</span>
|
|||
|
<span id="cb87-3"><a href="#cb87-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot!</span>(zero, color<span class="op">=:</span>green) <span class="co"># no a, b then inherited from graph.</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="53">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-54-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The <code>zero</code> function is just 0 (more generally useful when the type of a number is important, but used here to emphasize the <span class="math inline">\(x\)</span> axis).</p>
|
|||
|
</section>
|
|||
|
<section id="plotting-a-parameterized-space-curve-function-fr-rightarrow-rn-n-2-or-3" class="level3" data-number="70.6.2">
|
|||
|
<h3 data-number="70.6.2" class="anchored" data-anchor-id="plotting-a-parameterized-space-curve-function-fr-rightarrow-rn-n-2-or-3"><span class="header-section-number">70.6.2</span> Plotting a parameterized (space) curve function <span class="math inline">\(f:R \rightarrow R^n\)</span>, <span class="math inline">\(n = 2\)</span> or <span class="math inline">\(3\)</span></h3>
|
|||
|
<ul>
|
|||
|
<li>Using <code>plot(xs, ys)</code></li>
|
|||
|
</ul>
|
|||
|
<p>Let <span class="math inline">\(f(t) = e^{t/2\pi} \langle \cos(t), \sin(t)\rangle\)</span> be a parameterized function. Then the <span class="math inline">\(t\)</span> values can be generated as follows:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="54">
|
|||
|
<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>ts <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">2</span>pi, length <span class="op">=</span> <span class="fl">100</span>)</span>
|
|||
|
<span id="cb88-2"><a href="#cb88-2" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> [<span class="fu">exp</span>(t<span class="op">/</span><span class="fl">2</span>pi) <span class="op">*</span> <span class="fu">cos</span>(t) for t <span class="kw">in</span> ts]</span>
|
|||
|
<span id="cb88-3"><a href="#cb88-3" aria-hidden="true" tabindex="-1"></a>ys <span class="op">=</span> [<span class="fu">exp</span>(t<span class="op">/</span><span class="fl">2</span>pi) <span class="op">*</span> <span class="fu">sin</span>(t) for t <span class="kw">in</span> ts]</span>
|
|||
|
<span id="cb88-4"><a href="#cb88-4" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(xs, ys)</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">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-55-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<ul>
|
|||
|
<li>using <code>plot(f1, f2, a, b)</code>. If the two functions describing the components are available, then</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="55">
|
|||
|
<div class="sourceCode cell-code" id="cb89"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb89-1"><a href="#cb89-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f1</span>(t) <span class="op">=</span> <span class="fu">exp</span>(t<span class="op">/</span><span class="fl">2</span>pi) <span class="op">*</span> <span class="fu">cos</span>(t)</span>
|
|||
|
<span id="cb89-2"><a href="#cb89-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f2</span>(t) <span class="op">=</span> <span class="fu">exp</span>(t<span class="op">/</span><span class="fl">2</span>pi) <span class="op">*</span> <span class="fu">sin</span>(t)</span>
|
|||
|
<span id="cb89-3"><a href="#cb89-3" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(f1, f2, <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="55">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-56-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<ul>
|
|||
|
<li>Using <code>plot_parametric</code>. If the curve is described as a function of <code>t</code> with a vector output, then the <code>CalculusWithJulia</code> package provides <code>plot_parametric</code> to produce a plot:</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-execution_count="56">
|
|||
|
<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><span class="fu">r</span>(t) <span class="op">=</span> <span class="fu">exp</span>(t<span class="op">/</span><span class="fl">2</span>pi) <span class="op">*</span> [<span class="fu">cos</span>(t), <span class="fu">sin</span>(t)]</span>
|
|||
|
<span id="cb90-2"><a href="#cb90-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot_parametric</span>(<span class="fl">0</span><span class="op">..</span><span class="fl">2</span>pi, r)</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">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-57-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The low-level approach doesn’t quite work as easily as desired:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="57">
|
|||
|
<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>ts <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">2</span>pi, length <span class="op">=</span> <span class="fl">4</span>)</span>
|
|||
|
<span id="cb91-2"><a href="#cb91-2" aria-hidden="true" tabindex="-1"></a>vs <span class="op">=</span> <span class="fu">r</span>.(ts)</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">
|
|||
|
<pre><code>4-element Vector{Vector{Float64}}:
|
|||
|
[1.0, 0.0]
|
|||
|
[-0.6978062125430444, 1.2086358139617603]
|
|||
|
[-0.9738670205273388, -1.6867871593690715]
|
|||
|
[2.718281828459045, -6.657870280805568e-16]</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>As seen, the values are a vector of vectors. To plot a reshaping needs to be done:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="58">
|
|||
|
<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>ts <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">2</span>pi, length <span class="op">=</span> <span class="fl">100</span>)</span>
|
|||
|
<span id="cb93-2"><a href="#cb93-2" aria-hidden="true" tabindex="-1"></a>vs <span class="op">=</span> <span class="fu">r</span>.(ts)</span>
|
|||
|
<span id="cb93-3"><a href="#cb93-3" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> [vs[i][<span class="fl">1</span>] for i <span class="kw">in</span> <span class="fu">eachindex</span>(vs)]</span>
|
|||
|
<span id="cb93-4"><a href="#cb93-4" aria-hidden="true" tabindex="-1"></a>ys <span class="op">=</span> [vs[i][<span class="fl">2</span>] for i <span class="kw">in</span> <span class="fu">eachindex</span>(vs)]</span>
|
|||
|
<span id="cb93-5"><a href="#cb93-5" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(xs, ys)</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">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-59-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>This approach is faciliated by the <code>unzip</code> function in <code>CalculusWithJulia</code> (and used internally by <code>plot_parametric</code>):</p>
|
|||
|
<div class="cell" data-execution_count="59">
|
|||
|
<div class="sourceCode cell-code" id="cb94"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb94-1"><a href="#cb94-1" aria-hidden="true" tabindex="-1"></a>ts <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="fl">2</span>pi, length <span class="op">=</span> <span class="fl">100</span>)</span>
|
|||
|
<span id="cb94-2"><a href="#cb94-2" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(<span class="fu">unzip</span>(<span class="fu">r</span>.(ts))<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="59">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-60-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<ul>
|
|||
|
<li>Plotting an arrow</li>
|
|||
|
</ul>
|
|||
|
<p>An arrow in 2D can be plotted with the <code>quiver</code> command. We show the <code>arrow(p, v)</code> (or <code>arrow!(p,v)</code> function) from the <code>CalculusWithJulia</code> package, which has an easier syntax (<code>arrow!(p, v)</code>, where <code>p</code> is a point indicating the placement of the tail, and <code>v</code> the vector to represent):</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="60">
|
|||
|
<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><span class="fu">plot_parametric</span>(<span class="fl">0</span><span class="op">..</span><span class="fl">2</span>pi, r)</span>
|
|||
|
<span id="cb95-2"><a href="#cb95-2" aria-hidden="true" tabindex="-1"></a>t0 <span class="op">=</span> <span class="cn">pi</span><span class="op">/</span><span class="fl">8</span></span>
|
|||
|
<span id="cb95-3"><a href="#cb95-3" aria-hidden="true" tabindex="-1"></a><span class="fu">arrow!</span>(<span class="fu">r</span>(t0), r<span class="op">'</span>(t0))</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">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-61-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
<section id="plotting-a-scalar-function-fr2-rightarrow-r" class="level3" data-number="70.6.3">
|
|||
|
<h3 data-number="70.6.3" class="anchored" data-anchor-id="plotting-a-scalar-function-fr2-rightarrow-r"><span class="header-section-number">70.6.3</span> Plotting a scalar function <span class="math inline">\(f:R^2 \rightarrow R\)</span></h3>
|
|||
|
<p>The <code>surface</code> and <code>contour</code> functions are available to visualize a scalar function of <span class="math inline">\(2\)</span> variables:</p>
|
|||
|
<ul>
|
|||
|
<li>A surface plot</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="61">
|
|||
|
<div class="sourceCode cell-code" id="cb96"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb96-1"><a href="#cb96-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</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="cb96-2"><a href="#cb96-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">2</span>,<span class="fl">2</span>, length<span class="op">=</span><span class="fl">25</span>)</span>
|
|||
|
<span id="cb96-3"><a href="#cb96-3" aria-hidden="true" tabindex="-1"></a><span class="fu">surface</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="61">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-62-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The function generates the <span class="math inline">\(z\)</span> values, this can be done by the user and then passed to the <code>surface(xs, ys, zs)</code> format:</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><span class="fu">f</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="cb97-2"><a href="#cb97-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">2</span>,<span class="fl">2</span>, length<span class="op">=</span><span class="fl">25</span>)</span>
|
|||
|
<span id="cb97-3"><a href="#cb97-3" aria-hidden="true" tabindex="-1"></a><span class="fu">surface</span>(xs, ys, <span class="fu">f</span>.(xs, ys<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="62">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-63-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<ul>
|
|||
|
<li>A contour plot</li>
|
|||
|
</ul>
|
|||
|
<p>The <code>contour</code> function is like the <code>surface</code> function.</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="63">
|
|||
|
<div class="sourceCode cell-code" id="cb98"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb98-1"><a href="#cb98-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">2</span>,<span class="fl">2</span>, length<span class="op">=</span><span class="fl">25</span>)</span>
|
|||
|
<span id="cb98-2"><a href="#cb98-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</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="cb98-3"><a href="#cb98-3" aria-hidden="true" tabindex="-1"></a><span class="fu">contour</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="63">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-64-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The values can be computed easily enough, being careful where the transpose is needed:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="64">
|
|||
|
<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>xs <span class="op">=</span> ys <span class="op">=</span> <span class="fu">range</span>(<span class="op">-</span><span class="fl">2</span>,<span class="fl">2</span>, length<span class="op">=</span><span class="fl">25</span>)</span>
|
|||
|
<span id="cb99-2"><a href="#cb99-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</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="cb99-3"><a href="#cb99-3" aria-hidden="true" tabindex="-1"></a><span class="fu">contour</span>(xs, ys, <span class="fu">f</span>.(xs, ys<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="64">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-65-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<ul>
|
|||
|
<li>An implicit equation. The constraint <span class="math inline">\(f(x,y)=c\)</span> generates an implicit equation. While <code>contour</code> can be used for this type of plot - by adjusting the requested contours - the <code>ImplicitPlots</code> package does this to make a plot of the equations <span class="math inline">\(f(x,y) = 0\)</span>”</li>
|
|||
|
</ul>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="65">
|
|||
|
<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="im">using</span> <span class="bu">ImplicitPlots</span></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> <span class="fu">sin</span>(x<span class="op">*</span>y) <span class="op">-</span> <span class="fu">cos</span>(x<span class="op">*</span>y)</span>
|
|||
|
<span id="cb100-3"><a href="#cb100-3" aria-hidden="true" tabindex="-1"></a><span class="fu">implicit_plot</span>(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="65">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-66-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
<section id="plotting-a-parameterized-surface-fr2-rightarrow-r3" class="level3" data-number="70.6.4">
|
|||
|
<h3 data-number="70.6.4" class="anchored" data-anchor-id="plotting-a-parameterized-surface-fr2-rightarrow-r3"><span class="header-section-number">70.6.4</span> Plotting a parameterized surface <span class="math inline">\(f:R^2 \rightarrow R^3\)</span></h3>
|
|||
|
<p>The <code>pyplot</code> (and <code>plotly</code>) backends allow plotting of parameterized surfaces.</p>
|
|||
|
<p>The low-level <code>surface(xs,ys,zs)</code> is used, and can be specified directly as follows:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="66">
|
|||
|
<div class="sourceCode cell-code" id="cb101"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb101-1"><a href="#cb101-1" aria-hidden="true" tabindex="-1"></a><span class="fu">X</span>(theta, phi) <span class="op">=</span> <span class="fu">sin</span>(phi)<span class="fu">*cos</span>(theta)</span>
|
|||
|
<span id="cb101-2"><a href="#cb101-2" aria-hidden="true" tabindex="-1"></a><span class="fu">Y</span>(theta, phi) <span class="op">=</span> <span class="fu">sin</span>(phi)<span class="fu">*sin</span>(theta)</span>
|
|||
|
<span id="cb101-3"><a href="#cb101-3" aria-hidden="true" tabindex="-1"></a><span class="fu">Z</span>(theta, phi) <span class="op">=</span> <span class="fu">cos</span>(phi)</span>
|
|||
|
<span id="cb101-4"><a href="#cb101-4" aria-hidden="true" tabindex="-1"></a>thetas <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="cn">pi</span><span class="op">/</span><span class="fl">4</span>, length<span class="op">=</span><span class="fl">20</span>)</span>
|
|||
|
<span id="cb101-5"><a href="#cb101-5" aria-hidden="true" tabindex="-1"></a>phis <span class="op">=</span> <span class="fu">range</span>(<span class="fl">0</span>, <span class="cn">pi</span>, length<span class="op">=</span><span class="fl">20</span>)</span>
|
|||
|
<span id="cb101-6"><a href="#cb101-6" aria-hidden="true" tabindex="-1"></a><span class="fu">surface</span>(<span class="fu">X</span>.(thetas, phis<span class="op">'</span>), <span class="fu">Y</span>.(thetas, phis<span class="op">'</span>), <span class="fu">Z</span>.(thetas, phis<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="66">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-67-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
<section id="plotting-a-vector-field-fr2-rightarrow-r2." class="level3" data-number="70.6.5">
|
|||
|
<h3 data-number="70.6.5" class="anchored" data-anchor-id="plotting-a-vector-field-fr2-rightarrow-r2."><span class="header-section-number">70.6.5</span> Plotting a vector field <span class="math inline">\(F:R^2 \rightarrow R^2\)</span>.</h3>
|
|||
|
<p>The <code>CalculusWithJulia</code> package provides <code>vectorfieldplot</code>, used as:</p>
|
|||
|
<div class="cell" data-hold="true" 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>(x,y) <span class="op">=</span> [<span class="op">-</span>y, x]</span>
|
|||
|
<span id="cb102-2"><a href="#cb102-2" aria-hidden="true" tabindex="-1"></a><span class="fu">vectorfieldplot</span>(F, xlim<span class="op">=</span>(<span class="op">-</span><span class="fl">2</span>, <span class="fl">2</span>), ylim<span class="op">=</span>(<span class="op">-</span><span class="fl">2</span>,<span class="fl">2</span>), nx<span class="op">=</span><span class="fl">10</span>, ny<span class="op">=</span><span class="fl">10</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="67">
|
|||
|
<p><img src="quick_notes_files/figure-html/cell-68-output-1.svg" class="img-fluid"></p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>There is also <code>vectorfieldplot3d</code>.</p>
|
|||
|
</section>
|
|||
|
</section>
|
|||
|
<section id="limits" class="level2" data-number="70.7">
|
|||
|
<h2 data-number="70.7" class="anchored" data-anchor-id="limits"><span class="header-section-number">70.7</span> Limits</h2>
|
|||
|
<p>Limits can be investigated numerically by forming tables, eg.:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="68">
|
|||
|
<div class="sourceCode cell-code" id="cb103"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb103-1"><a href="#cb103-1" aria-hidden="true" tabindex="-1"></a>xs <span class="op">=</span> [<span class="fl">1</span>, <span class="fl">1</span><span class="op">/</span><span class="fl">10</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">1000</span>]</span>
|
|||
|
<span id="cb103-2"><a href="#cb103-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> <span class="fu">sin</span>(x)<span class="op">/</span>x</span>
|
|||
|
<span id="cb103-3"><a href="#cb103-3" aria-hidden="true" tabindex="-1"></a>[xs <span class="fu">f</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="68">
|
|||
|
<pre><code>4×2 Matrix{Float64}:
|
|||
|
1.0 0.841471
|
|||
|
0.1 0.998334
|
|||
|
0.01 0.999983
|
|||
|
0.001 1.0</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Symbolically, <code>SymPy</code> provides a <code>limit</code> function:</p>
|
|||
|
<div class="cell" data-hold="true" 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> x</span>
|
|||
|
<span id="cb105-2"><a href="#cb105-2" aria-hidden="true" tabindex="-1"></a><span class="fu">limit</span>(<span class="fu">sin</span>(x)<span class="op">/</span>x, x <span class="op">=></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="69">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
1
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Or</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="70">
|
|||
|
<div class="sourceCode cell-code" id="cb106"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb106-1"><a href="#cb106-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> h x</span>
|
|||
|
<span id="cb106-2"><a href="#cb106-2" aria-hidden="true" tabindex="-1"></a><span class="fu">limit</span>((<span class="fu">sin</span>(x<span class="op">+</span>h) <span class="op">-</span> <span class="fu">sin</span>(x))<span class="op">/</span>h, h <span class="op">=></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-error">
|
|||
|
<pre><code>LoadError: invalid redefinition of constant h</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
<section id="derivatives" class="level2" data-number="70.8">
|
|||
|
<h2 data-number="70.8" class="anchored" data-anchor-id="derivatives"><span class="header-section-number">70.8</span> Derivatives</h2>
|
|||
|
<p>There are numeric and symbolic approaches to derivatives. For the numeric approach we use the <code>ForwardDiff</code> package, which performs automatic differentiation.</p>
|
|||
|
<section id="derivatives-of-univariate-functions" class="level3" data-number="70.8.1">
|
|||
|
<h3 data-number="70.8.1" class="anchored" data-anchor-id="derivatives-of-univariate-functions"><span class="header-section-number">70.8.1</span> Derivatives of univariate functions</h3>
|
|||
|
<p>Numerically, the <code>ForwardDiff.derivative(f, x)</code> function call will find the derivative of the function <code>f</code> at the point <code>x</code>:</p>
|
|||
|
<div class="cell" data-execution_count="71">
|
|||
|
<div class="sourceCode cell-code" id="cb108"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb108-1"><a href="#cb108-1" aria-hidden="true" tabindex="-1"></a>ForwardDiff.<span class="fu">derivative</span>(sin, <span class="cn">pi</span><span class="op">/</span><span class="fl">3</span>) <span class="op">-</span> <span class="fu">cos</span>(<span class="cn">pi</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="71">
|
|||
|
<pre><code>0.0</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The <code>CalculusWithJulia</code> package overides the <code>'</code> (<code>adjoint</code>) syntax for functions to provide a derivative which takes a function and returns a function, so its usage is familiar</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="72">
|
|||
|
<div class="sourceCode cell-code" id="cb110"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb110-1"><a href="#cb110-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> <span class="fu">sin</span>(x)</span>
|
|||
|
<span id="cb110-2"><a href="#cb110-2" aria-hidden="true" tabindex="-1"></a>f<span class="op">'</span>(<span class="cn">pi</span><span class="op">/</span><span class="fl">3</span>) <span class="op">-</span> <span class="fu">cos</span>(<span class="cn">pi</span><span class="op">/</span><span class="fl">3</span>) <span class="co"># or just sin'(pi/3) - cos(pi/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="72">
|
|||
|
<pre><code>0.0</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Higher order derivatives are possible as well,</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="73">
|
|||
|
<div class="sourceCode cell-code" id="cb112"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb112-1"><a href="#cb112-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> <span class="fu">sin</span>(x)</span>
|
|||
|
<span id="cb112-2"><a href="#cb112-2" aria-hidden="true" tabindex="-1"></a>f<span class="op">''''</span>(<span class="cn">pi</span><span class="op">/</span><span class="fl">3</span>) <span class="op">-</span> <span class="fu">f</span>(<span class="cn">pi</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="73">
|
|||
|
<pre><code>0.0</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<hr>
|
|||
|
<p>Symbolically, the <code>diff</code> function of <code>SymPy</code> finds derivatives.</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="74">
|
|||
|
<div class="sourceCode cell-code" id="cb114"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb114-1"><a href="#cb114-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x</span>
|
|||
|
<span id="cb114-2"><a href="#cb114-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> <span class="fu">exp</span>(<span class="op">-</span>x)<span class="fu">*sin</span>(x)</span>
|
|||
|
<span id="cb114-3"><a href="#cb114-3" aria-hidden="true" tabindex="-1"></a>ex <span class="op">=</span> <span class="fu">f</span>(x) <span class="co"># symbolic expression</span></span>
|
|||
|
<span id="cb114-4"><a href="#cb114-4" aria-hidden="true" tabindex="-1"></a><span class="fu">diff</span>(ex, x) <span class="co"># or just diff(f(x), x)</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">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
- e^{- x} \sin{\left(x \right)} + e^{- x} \cos{\left(x \right)}
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Higher order derivatives can be specified as well</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="75">
|
|||
|
<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> x</span>
|
|||
|
<span id="cb115-2"><a href="#cb115-2" aria-hidden="true" tabindex="-1"></a>ex <span class="op">=</span> <span class="fu">exp</span>(<span class="op">-</span>x)<span class="fu">*sin</span>(x)</span>
|
|||
|
<span id="cb115-3"><a href="#cb115-3" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb115-4"><a href="#cb115-4" aria-hidden="true" tabindex="-1"></a><span class="fu">diff</span>(ex, x, 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="75">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
- 2 e^{- x} \cos{\left(x \right)}
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Or with a number:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="76">
|
|||
|
<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="pp">@syms</span> x</span>
|
|||
|
<span id="cb116-2"><a href="#cb116-2" aria-hidden="true" tabindex="-1"></a>ex <span class="op">=</span> <span class="fu">exp</span>(<span class="op">-</span>x)<span class="fu">*sin</span>(x)</span>
|
|||
|
<span id="cb116-3"><a href="#cb116-3" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb116-4"><a href="#cb116-4" aria-hidden="true" tabindex="-1"></a><span class="fu">diff</span>(ex, x, <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="76">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
4 \left(\sin{\left(x \right)} - \cos{\left(x \right)}\right) e^{- x}
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The variable is important, as this allows parameters to be symbolic</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="77">
|
|||
|
<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="pp">@syms</span> mu sigma x</span>
|
|||
|
<span id="cb117-2"><a href="#cb117-2" aria-hidden="true" tabindex="-1"></a><span class="fu">diff</span>(<span class="fu">exp</span>(<span class="fu">-</span>((x<span class="op">-</span>mu)<span class="op">/</span>sigma)<span class="op">^</span><span class="fl">2</span><span class="op">/</span><span class="fl">2</span>), 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="77">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
- \frac{\left(- 2 \mu + 2 x\right) e^{- \frac{\left(- \mu + x\right)^{2}}{2 \sigma^{2}}}}{2 \sigma^{2}}
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
<section id="partial-derivatives" class="level3" data-number="70.8.2">
|
|||
|
<h3 data-number="70.8.2" class="anchored" data-anchor-id="partial-derivatives"><span class="header-section-number">70.8.2</span> Partial derivatives</h3>
|
|||
|
<p>There is no direct partial derivative function provided by <code>ForwardDiff</code>, rather we use the result of the <code>ForwardDiff.gradient</code> function, which finds the partial derivatives for each variable. To use this, the function must be defined in terms of a point or vector.</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="78">
|
|||
|
<div class="sourceCode cell-code" id="cb118"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb118-1"><a href="#cb118-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y,z) <span class="op">=</span> x<span class="op">*</span>y <span class="op">+</span> y<span class="op">*</span>z <span class="op">+</span> z<span class="op">*</span>x</span>
|
|||
|
<span id="cb118-2"><a href="#cb118-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 class="co"># this is needed for ForwardDiff.gradient</span></span>
|
|||
|
<span id="cb118-3"><a href="#cb118-3" aria-hidden="true" tabindex="-1"></a>ForwardDiff.<span class="fu">gradient</span>(f, [<span class="fl">1</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="78">
|
|||
|
<pre><code>3-element Vector{Int64}:
|
|||
|
5
|
|||
|
4
|
|||
|
3</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>We can see directly that <span class="math inline">\(\partial{f}/\partial{x} = \langle y + z\rangle\)</span>. At the point <span class="math inline">\((1,2,3)\)</span>, this is <span class="math inline">\(5\)</span>, as returned above.</p>
|
|||
|
<hr>
|
|||
|
<p>Symbolically, <code>diff</code> is used for partial derivatives:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="79">
|
|||
|
<div class="sourceCode cell-code" id="cb120"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb120-1"><a href="#cb120-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x y z</span>
|
|||
|
<span id="cb120-2"><a href="#cb120-2" aria-hidden="true" tabindex="-1"></a>ex <span class="op">=</span> x<span class="op">*</span>y <span class="op">+</span> y<span class="op">*</span>z <span class="op">+</span> z<span class="op">*</span>x</span>
|
|||
|
<span id="cb120-3"><a href="#cb120-3" aria-hidden="true" tabindex="-1"></a><span class="fu">diff</span>(ex, x) <span class="co"># ∂f/∂x</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">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
y + z
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<blockquote class="blockquote">
|
|||
|
<p>Gradient</p>
|
|||
|
</blockquote>
|
|||
|
<p>As seen, the <code>ForwardDiff.gradient</code> function finds the gradient at a point. In <code>CalculusWithJulia</code>, the gradient is extended to return a function when called with no additional arguments:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="80">
|
|||
|
<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><span class="fu">f</span>(x,y,z) <span class="op">=</span> x<span class="op">*</span>y <span class="op">+</span> y<span class="op">*</span>z <span class="op">+</span> z<span class="op">*</span>x</span>
|
|||
|
<span id="cb121-2"><a href="#cb121-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="cb121-3"><a href="#cb121-3" aria-hidden="true" tabindex="-1"></a><span class="fu">gradient</span>(f)(<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">3</span>) <span class="op">-</span> <span class="fu">gradient</span>(f, [<span class="fl">1</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="80">
|
|||
|
<pre><code>3-element Vector{Int64}:
|
|||
|
0
|
|||
|
0
|
|||
|
0</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The <code>∇</code> symbol, formed by entering <code>\nabla[tab]</code>, is mathematical syntax for the gradient, and is defined in <code>CalculusWithJulia</code>.</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="81">
|
|||
|
<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="fu">f</span>(x,y,z) <span class="op">=</span> x<span class="op">*</span>y <span class="op">+</span> y<span class="op">*</span>z <span class="op">+</span> z<span class="op">*</span>x</span>
|
|||
|
<span id="cb123-2"><a href="#cb123-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="cb123-3"><a href="#cb123-3" aria-hidden="true" tabindex="-1"></a><span class="fu">∇</span>(f)(<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">3</span>) <span class="co"># same as gradient(f, [1,2,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="81">
|
|||
|
<pre><code>3-element Vector{Int64}:
|
|||
|
5
|
|||
|
4
|
|||
|
3</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<hr>
|
|||
|
<p>In <code>SymPy</code>, there is no gradient function, though finding the gradient is easy through broadcasting:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="82">
|
|||
|
<div class="sourceCode cell-code" id="cb125"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb125-1"><a href="#cb125-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x y z</span>
|
|||
|
<span id="cb125-2"><a href="#cb125-2" aria-hidden="true" tabindex="-1"></a>ex <span class="op">=</span> x<span class="op">*</span>y <span class="op">+</span> y<span class="op">*</span>z <span class="op">+</span> z<span class="op">*</span>x</span>
|
|||
|
<span id="cb125-3"><a href="#cb125-3" aria-hidden="true" tabindex="-1"></a><span class="fu">diff</span>.(ex, [x,y,z]) <span class="co"># [diff(ex, x), diff(ex, y), diff(ex, z)]</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="82">
|
|||
|
<pre><code>3-element Vector{Sym}:
|
|||
|
y + z
|
|||
|
x + z
|
|||
|
x + y</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The <code>CalculusWithJulia</code> package provides a method for <code>gradient</code>:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="83">
|
|||
|
<div class="sourceCode cell-code" id="cb127"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb127-1"><a href="#cb127-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x y z</span>
|
|||
|
<span id="cb127-2"><a href="#cb127-2" aria-hidden="true" tabindex="-1"></a>ex <span class="op">=</span> x<span class="op">*</span>y <span class="op">+</span> y<span class="op">*</span>z <span class="op">+</span> z<span class="op">*</span>x</span>
|
|||
|
<span id="cb127-3"><a href="#cb127-3" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb127-4"><a href="#cb127-4" aria-hidden="true" tabindex="-1"></a><span class="fu">gradient</span>(ex, [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="83">
|
|||
|
<pre><code>3-element Vector{Sym}:
|
|||
|
y + z
|
|||
|
x + z
|
|||
|
x + y</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The <code>∇</code> symbol is an alias. It can guess the order of the free symbols, but generally specifying them is needed. This is done with a tuple:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="84">
|
|||
|
<div class="sourceCode cell-code" id="cb129"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb129-1"><a href="#cb129-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x y z</span>
|
|||
|
<span id="cb129-2"><a href="#cb129-2" aria-hidden="true" tabindex="-1"></a>ex <span class="op">=</span> x<span class="op">*</span>y <span class="op">+</span> y<span class="op">*</span>z <span class="op">+</span> z<span class="op">*</span>x</span>
|
|||
|
<span id="cb129-3"><a href="#cb129-3" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb129-4"><a href="#cb129-4" aria-hidden="true" tabindex="-1"></a><span class="fu">∇</span>((ex, [x,y,z])) <span class="co"># for this, ∇(ex) also works</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="84">
|
|||
|
<pre><code>3-element Vector{Sym}:
|
|||
|
y + z
|
|||
|
x + z
|
|||
|
x + y</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
<section id="jacobian" class="level3" data-number="70.8.3">
|
|||
|
<h3 data-number="70.8.3" class="anchored" data-anchor-id="jacobian"><span class="header-section-number">70.8.3</span> Jacobian</h3>
|
|||
|
<p>The Jacobian of a function <span class="math inline">\(f:R^n \rightarrow R^m\)</span> is a <span class="math inline">\(m\times n\)</span> matrix of partial derivatives. Numerically, <code>ForwardDiff.jacobian</code> can find the Jacobian of a function at a point:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="85">
|
|||
|
<div class="sourceCode cell-code" id="cb131"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb131-1"><a href="#cb131-1" aria-hidden="true" tabindex="-1"></a><span class="fu">F</span>(u,v) <span class="op">=</span> [<span class="fu">u*cos</span>(v), <span class="fu">u*sin</span>(v), u]</span>
|
|||
|
<span id="cb131-2"><a href="#cb131-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 class="co"># needed for ForwardDiff.jacobian</span></span>
|
|||
|
<span id="cb131-3"><a href="#cb131-3" aria-hidden="true" tabindex="-1"></a>pt <span class="op">=</span> [<span class="fl">1</span>, <span class="cn">pi</span><span class="op">/</span><span class="fl">4</span>]</span>
|
|||
|
<span id="cb131-4"><a href="#cb131-4" aria-hidden="true" tabindex="-1"></a>ForwardDiff.<span class="fu">jacobian</span>(F , 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="85">
|
|||
|
<pre><code>3×2 Matrix{Float64}:
|
|||
|
0.707107 -0.707107
|
|||
|
0.707107 0.707107
|
|||
|
1.0 0.0</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<hr>
|
|||
|
<p>Symbolically, the <code>jacobian</code> function is a method of a <em>matrix</em>, so the calling pattern is different. (Of the form <code>object.method(arguments...)</code>.)</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="86">
|
|||
|
<div class="sourceCode cell-code" id="cb133"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb133-1"><a href="#cb133-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> u v</span>
|
|||
|
<span id="cb133-2"><a href="#cb133-2" aria-hidden="true" tabindex="-1"></a><span class="fu">F</span>(u,v) <span class="op">=</span> [<span class="fu">u*cos</span>(v), <span class="fu">u*sin</span>(v), u]</span>
|
|||
|
<span id="cb133-3"><a href="#cb133-3" 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="cb133-4"><a href="#cb133-4" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb133-5"><a href="#cb133-5" aria-hidden="true" tabindex="-1"></a>ex <span class="op">=</span> <span class="fu">F</span>(u,v)</span>
|
|||
|
<span id="cb133-6"><a href="#cb133-6" aria-hidden="true" tabindex="-1"></a>ex.<span class="fu">jacobian</span>([u,v])</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="86">
|
|||
|
<pre><code>3×2 Matrix{Sym}:
|
|||
|
cos(v) -u⋅sin(v)
|
|||
|
sin(v) u⋅cos(v)
|
|||
|
1 0</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>As the Jacobian can be identified as the matrix with rows given by the transpose of the gradient of the component, it can be computed directly, but it is more difficult:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="87">
|
|||
|
<div class="sourceCode cell-code" id="cb135"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb135-1"><a href="#cb135-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> u<span class="op">::</span><span class="dt">real </span>v<span class="op">::</span><span class="dt">real</span></span>
|
|||
|
<span id="cb135-2"><a href="#cb135-2" aria-hidden="true" tabindex="-1"></a><span class="fu">F</span>(u,v) <span class="op">=</span> [<span class="fu">u*cos</span>(v), <span class="fu">u*sin</span>(v), u]</span>
|
|||
|
<span id="cb135-3"><a href="#cb135-3" 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="cb135-4"><a href="#cb135-4" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb135-5"><a href="#cb135-5" aria-hidden="true" tabindex="-1"></a><span class="fu">vcat</span>([<span class="fu">diff</span>.(ex, [u,v])<span class="ch">' for ex in F(u,v)]...)</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="87">
|
|||
|
<pre><code>3×2 Matrix{Sym}:
|
|||
|
cos(v) -u⋅sin(v)
|
|||
|
sin(v) u⋅cos(v)
|
|||
|
1 0</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
<section id="divergence" class="level3" data-number="70.8.4">
|
|||
|
<h3 data-number="70.8.4" class="anchored" data-anchor-id="divergence"><span class="header-section-number">70.8.4</span> Divergence</h3>
|
|||
|
<p>Numerically, the divergence can be computed from the Jacobian by adding the diagonal elements. This is a numerically inefficient, as the other partial derivates must be found and discarded, but this is generally not an issue for these notes. The following uses <code>tr</code> (the trace from the <code>LinearAlgebra</code> package) to find the sum of a diagonal.</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="88">
|
|||
|
<div class="sourceCode cell-code" id="cb137"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb137-1"><a href="#cb137-1" aria-hidden="true" tabindex="-1"></a><span class="fu">F</span>(x,y,z) <span class="op">=</span> [<span class="op">-</span>y, x, z]</span>
|
|||
|
<span id="cb137-2"><a href="#cb137-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="cb137-3"><a href="#cb137-3" aria-hidden="true" tabindex="-1"></a>pt <span class="op">=</span> [<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">3</span>]</span>
|
|||
|
<span id="cb137-4"><a href="#cb137-4" aria-hidden="true" tabindex="-1"></a><span class="fu">tr</span>(ForwardDiff.<span class="fu">jacobian</span>(F , 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="88">
|
|||
|
<pre><code>1</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The <code>CalculusWithJulia</code> package provides <code>divergence</code> to compute the divergence and provides the <code>∇ ⋅</code> notation (<code>\nabla[tab]\cdot[tab]</code>):</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="89">
|
|||
|
<div class="sourceCode cell-code" id="cb139"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb139-1"><a href="#cb139-1" aria-hidden="true" tabindex="-1"></a><span class="fu">F</span>(x,y,z) <span class="op">=</span> [<span class="op">-</span>y, x, z]</span>
|
|||
|
<span id="cb139-2"><a href="#cb139-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="cb139-3"><a href="#cb139-3" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb139-4"><a href="#cb139-4" aria-hidden="true" tabindex="-1"></a><span class="fu">divergence</span>(F, [<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">3</span>])</span>
|
|||
|
<span id="cb139-5"><a href="#cb139-5" aria-hidden="true" tabindex="-1"></a>(∇<span class="op">⋅</span>F)(<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">3</span>) <span class="co"># not ∇⋅F(1,2,3) as that evaluates F(1,2,3) before the divergence</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="89">
|
|||
|
<pre><code>1.0</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<hr>
|
|||
|
<p>Symbolically, the divergence can be found directly:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="90">
|
|||
|
<div class="sourceCode cell-code" id="cb141"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb141-1"><a href="#cb141-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x y z</span>
|
|||
|
<span id="cb141-2"><a href="#cb141-2" aria-hidden="true" tabindex="-1"></a>ex <span class="op">=</span> [<span class="op">-</span>y, x, z]</span>
|
|||
|
<span id="cb141-3"><a href="#cb141-3" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb141-4"><a href="#cb141-4" aria-hidden="true" tabindex="-1"></a><span class="fu">sum</span>(<span class="fu">diff</span>.(ex, [x,y,z])) <span class="co"># sum of [diff(ex[1], x), diff(ex[2],y), diff(ex[3], z)]</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="90">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
1
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The <code>divergence</code> function can be used for symbolic expressions:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="91">
|
|||
|
<div class="sourceCode cell-code" id="cb142"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb142-1"><a href="#cb142-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x y z</span>
|
|||
|
<span id="cb142-2"><a href="#cb142-2" aria-hidden="true" tabindex="-1"></a>ex <span class="op">=</span> [<span class="op">-</span>y, x, z]</span>
|
|||
|
<span id="cb142-3"><a href="#cb142-3" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb142-4"><a href="#cb142-4" aria-hidden="true" tabindex="-1"></a><span class="fu">divergence</span>(ex, [x,y,z])</span>
|
|||
|
<span id="cb142-5"><a href="#cb142-5" aria-hidden="true" tabindex="-1"></a><span class="fu">∇⋅</span>(ex, [x,y,z]) <span class="co"># For this, ∇ ⋅ F(x,y,z) also works</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="91">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
1
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
<section id="curl" class="level3" data-number="70.8.5">
|
|||
|
<h3 data-number="70.8.5" class="anchored" data-anchor-id="curl"><span class="header-section-number">70.8.5</span> Curl</h3>
|
|||
|
<p>The curl can be computed from the off-diagonal elements of the Jacobian. The calculation follows the formula. The <code>CalculusWithJulia</code> package provides <code>curl</code> to compute this:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="92">
|
|||
|
<div class="sourceCode cell-code" id="cb143"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb143-1"><a href="#cb143-1" aria-hidden="true" tabindex="-1"></a><span class="fu">F</span>(x,y,z) <span class="op">=</span> [<span class="op">-</span>y, x, <span class="fl">1</span>]</span>
|
|||
|
<span id="cb143-2"><a href="#cb143-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="cb143-3"><a href="#cb143-3" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb143-4"><a href="#cb143-4" aria-hidden="true" tabindex="-1"></a><span class="fu">curl</span>(F, [<span class="fl">1</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="92">
|
|||
|
<pre><code>3-element Vector{Float64}:
|
|||
|
0.0
|
|||
|
-0.0
|
|||
|
2.0</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>As well, if no point is specified, a function is returned for which a point may be specified using 3 coordinates or a vector</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="93">
|
|||
|
<div class="sourceCode cell-code" id="cb145"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb145-1"><a href="#cb145-1" aria-hidden="true" tabindex="-1"></a><span class="fu">F</span>(x,y,z) <span class="op">=</span> [<span class="op">-</span>y, x, <span class="fl">1</span>]</span>
|
|||
|
<span id="cb145-2"><a href="#cb145-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="cb145-3"><a href="#cb145-3" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb145-4"><a href="#cb145-4" aria-hidden="true" tabindex="-1"></a><span class="fu">curl</span>(F)(<span class="fl">1</span>,<span class="fl">2</span>,<span class="fl">3</span>), <span class="fu">curl</span>(F)([<span class="fl">1</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="93">
|
|||
|
<pre><code>([0.0, -0.0, 2.0], [0.0, -0.0, 2.0])</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Finally, the <code>∇ ×</code> (<code>\nabla[tab]\times[tab]</code> notation is available)</p>
|
|||
|
<div class="cell" data-ohld="true" data-execution_count="94">
|
|||
|
<div class="sourceCode cell-code" id="cb147"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb147-1"><a href="#cb147-1" aria-hidden="true" tabindex="-1"></a><span class="fu">F</span>(x,y,z) <span class="op">=</span> [<span class="op">-</span>y, x, <span class="fl">1</span>]</span>
|
|||
|
<span id="cb147-2"><a href="#cb147-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="cb147-3"><a href="#cb147-3" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb147-4"><a href="#cb147-4" aria-hidden="true" tabindex="-1"></a>(∇<span class="op">×</span>F)(<span class="fl">1</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="94">
|
|||
|
<pre><code>3-element Vector{Float64}:
|
|||
|
0.0
|
|||
|
-0.0
|
|||
|
2.0</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>For symbolic expressions, we have the <code>∇ ×</code> times notation is available <strong>if</strong> the symbolic vector contains all <span class="math inline">\(3\)</span> variables</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="95">
|
|||
|
<div class="sourceCode cell-code" id="cb149"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb149-1"><a href="#cb149-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> x y z</span>
|
|||
|
<span id="cb149-2"><a href="#cb149-2" aria-hidden="true" tabindex="-1"></a>F <span class="op">=</span> [<span class="op">-</span>y, x, z] <span class="co"># but not [-y, x, 1] which errs; use `curl` with variables specified</span></span>
|
|||
|
<span id="cb149-3"><a href="#cb149-3" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb149-4"><a href="#cb149-4" aria-hidden="true" tabindex="-1"></a><span class="fu">curl</span>([<span class="op">-</span>y, x, <span class="fl">1</span>], (x,y,z)), ∇<span class="op">×</span>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-error">
|
|||
|
<pre><code>LoadError: invalid redefinition of constant F</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
</section>
|
|||
|
<section id="integrals" class="level2" data-number="70.9">
|
|||
|
<h2 data-number="70.9" class="anchored" data-anchor-id="integrals"><span class="header-section-number">70.9</span> Integrals</h2>
|
|||
|
<p>Numeric integration is provided by the <code>QuadGK</code> package, for univariate integrals, and the <code>HCubature</code> package for higher dimensional integrals.</p>
|
|||
|
<div class="sourceCode cell-code" id="cb151"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb151-1"><a href="#cb151-1" aria-hidden="true" tabindex="-1"></a><span class="im">using</span> <span class="bu">QuadGK</span>, <span class="bu">HCubature</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
|
|||
|
<section id="integrals-of-univariate-functions" class="level3" data-number="70.9.1">
|
|||
|
<h3 data-number="70.9.1" class="anchored" data-anchor-id="integrals-of-univariate-functions"><span class="header-section-number">70.9.1</span> Integrals of univariate functions</h3>
|
|||
|
<p>A definite integral may be computed numerically using <code>quadgk</code></p>
|
|||
|
<div class="cell" data-execution_count="97">
|
|||
|
<div class="sourceCode cell-code" id="cb152"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb152-1"><a href="#cb152-1" aria-hidden="true" tabindex="-1"></a><span class="fu">quadgk</span>(sin, <span class="fl">0</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="97">
|
|||
|
<pre><code>(2.0, 1.7905676941154525e-12)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The answer and an estimate for the worst case error is returned.</p>
|
|||
|
<p>If singularities are avoided, improper integrals are computed as well:</p>
|
|||
|
<div class="cell" data-execution_count="98">
|
|||
|
<div class="sourceCode cell-code" id="cb154"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb154-1"><a href="#cb154-1" aria-hidden="true" tabindex="-1"></a><span class="fu">quadgk</span>(x<span class="op">-></span><span class="fl">1</span><span class="op">/</span>x<span class="op">^</span>(<span class="fl">1</span><span class="op">/</span><span class="fl">2</span>), <span class="fl">0</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="98">
|
|||
|
<pre><code>(1.9999999845983916, 2.3762511924588765e-8)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<hr>
|
|||
|
<p>SymPy provides the <code>integrate</code> function to compute both definite and indefinite integrals.</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="99">
|
|||
|
<div class="sourceCode cell-code" id="cb156"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb156-1"><a href="#cb156-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> a<span class="op">::</span><span class="dt">real </span>x<span class="op">::</span><span class="dt">real</span></span>
|
|||
|
<span id="cb156-2"><a href="#cb156-2" aria-hidden="true" tabindex="-1"></a><span class="fu">integrate</span>(<span class="fu">exp</span>(a<span class="op">*</span>x)<span class="fu">*sin</span>(x), 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="99">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
\frac{a e^{a x} \sin{\left(x \right)}}{a^{2} + 1} - \frac{e^{a x} \cos{\left(x \right)}}{a^{2} + 1}
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Like <code>diff</code> the variable to integrate is specified.</p>
|
|||
|
<p>Definite integrals use a tuple, <code>(variable, a, b)</code>, to specify the variable and range to integrate over:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="100">
|
|||
|
<div class="sourceCode cell-code" id="cb157"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb157-1"><a href="#cb157-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> a<span class="op">::</span><span class="dt">real </span>x<span class="op">::</span><span class="dt">real</span></span>
|
|||
|
<span id="cb157-2"><a href="#cb157-2" aria-hidden="true" tabindex="-1"></a><span class="fu">integrate</span>(<span class="fu">sin</span>(a <span class="op">+</span> x), (x, <span class="fl">0</span>, PI)) <span class="co"># ∫_0^PI sin(a+x) dx</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="100">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
2 \cos{\left(a \right)}
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
<section id="d-and-3d-iterated-integrals" class="level3" data-number="70.9.2">
|
|||
|
<h3 data-number="70.9.2" class="anchored" data-anchor-id="d-and-3d-iterated-integrals"><span class="header-section-number">70.9.2</span> 2D and 3D iterated integrals</h3>
|
|||
|
<p>Two and three dimensional integrals over box-like regions are computed numerically with the <code>hcubature</code> function from the <code>HCubature</code> package. If the box is <span class="math inline">\([x_1, y_1]\times[x_2,y_2]\times\cdots\times[x_n,y_n]\)</span> then the limits are specified through tuples of the form <span class="math inline">\((x_1,x_2,\dots,x_n)\)</span> and <span class="math inline">\((y_1,y_2,\dots,y_n)\)</span>.</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="101">
|
|||
|
<div class="sourceCode cell-code" id="cb158"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb158-1"><a href="#cb158-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 class="op">^</span><span class="fl">2</span></span>
|
|||
|
<span id="cb158-2"><a href="#cb158-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="cb158-3"><a href="#cb158-3" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb158-4"><a href="#cb158-4" aria-hidden="true" tabindex="-1"></a><span class="fu">hcubature</span>(f, (<span class="fl">0</span>,<span class="fl">0</span>), (<span class="fl">1</span>, <span class="fl">2</span>)) <span class="co"># computes ∫₀¹∫₀² f(x,y) dy dx</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="101">
|
|||
|
<pre><code>(1.333333333333333, 4.440892098500626e-16)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The calling pattern for more dimensions is identical.</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="102">
|
|||
|
<div class="sourceCode cell-code" id="cb160"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb160-1"><a href="#cb160-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x,y,z) <span class="op">=</span> x<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="cb160-2"><a href="#cb160-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="cb160-3"><a href="#cb160-3" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb160-4"><a href="#cb160-4" aria-hidden="true" tabindex="-1"></a><span class="fu">hcubature</span>(f, (<span class="fl">0</span>,<span class="fl">0</span>,<span class="fl">0</span>), (<span class="fl">1</span>, <span class="fl">2</span>,<span class="fl">3</span>)) <span class="co"># computes ∫₀¹∫₀²∫₀³ f(x,y,z) dz dy dx</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="102">
|
|||
|
<pre><code>(27.0, 0.0)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>The box-like region requirement means a change of variables may be necessary. For example, to integrate over the region <span class="math inline">\(x^2 + y^2 \leq 1; x \geq 0\)</span>, polar coordinates can be used with <span class="math inline">\((r,\theta)\)</span> in <span class="math inline">\([0,1]\times[-\pi/2,\pi/2]\)</span>. When changing variables, the Jacobian enters into the formula, through</p>
|
|||
|
<p><span class="math display">\[
|
|||
|
~
|
|||
|
\iint_{G(S)} f(\vec{x}) dV = \iint_S (f \circ G)(\vec{u}) |\det(J_G)(\vec{u})| dU.
|
|||
|
~
|
|||
|
\]</span></p>
|
|||
|
<p>Here we implement this:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="103">
|
|||
|
<div class="sourceCode cell-code" id="cb162"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb162-1"><a href="#cb162-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 class="op">^</span><span class="fl">2</span></span>
|
|||
|
<span id="cb162-2"><a href="#cb162-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="cb162-3"><a href="#cb162-3" aria-hidden="true" tabindex="-1"></a><span class="fu">Phi</span>(r, theta) <span class="op">=</span> r <span class="op">*</span> [<span class="fu">cos</span>(theta), <span class="fu">sin</span>(theta)]</span>
|
|||
|
<span id="cb162-4"><a href="#cb162-4" aria-hidden="true" tabindex="-1"></a><span class="fu">Phi</span>(rtheta) <span class="op">=</span> <span class="fu">Phi</span>(rtheta<span class="op">...</span>)</span>
|
|||
|
<span id="cb162-5"><a href="#cb162-5" aria-hidden="true" tabindex="-1"></a><span class="fu">integrand</span>(rtheta) <span class="op">=</span> <span class="fu">f</span>(<span class="fu">Phi</span>(rtheta)) <span class="op">*</span> <span class="fu">det</span>(ForwardDiff.<span class="fu">jacobian</span>(Phi, rtheta))</span>
|
|||
|
<span id="cb162-6"><a href="#cb162-6" aria-hidden="true" tabindex="-1"></a><span class="fu">hcubature</span>(integrand, (<span class="fl">0.0</span>,<span class="op">-</span><span class="cn">pi</span><span class="op">/</span><span class="fl">2</span>), (<span class="fl">1.0</span>, <span class="cn">pi</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="103">
|
|||
|
<pre><code>(0.13333333333904918, 1.9853799966359355e-9)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<hr>
|
|||
|
<p>Symbolically, the <code>integrate</code> function allows additional terms to be specified. For example, the above could be done through:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="104">
|
|||
|
<div class="sourceCode cell-code" id="cb164"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb164-1"><a href="#cb164-1" 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="cb164-2"><a href="#cb164-2" aria-hidden="true" tabindex="-1"></a><span class="fu">integrate</span>(x <span class="op">*</span> y<span class="op">^</span><span class="fl">2</span>, (y, <span class="fu">-sqrt</span>(<span class="fl">1</span><span class="op">-</span>x<span class="op">^</span><span class="fl">2</span>), <span class="fu">sqrt</span>(<span class="fl">1</span><span class="op">-</span>x<span class="op">^</span><span class="fl">2</span>)), (x, <span class="fl">0</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="104">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
\frac{2}{15}
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
<section id="line-integrals" class="level3" data-number="70.9.3">
|
|||
|
<h3 data-number="70.9.3" class="anchored" data-anchor-id="line-integrals"><span class="header-section-number">70.9.3</span> Line integrals</h3>
|
|||
|
<p>A line integral of <span class="math inline">\(f\)</span> parameterized by <span class="math inline">\(\vec{r}(t)\)</span> is computed by:</p>
|
|||
|
<p><span class="math display">\[
|
|||
|
~
|
|||
|
\int_a^b (f\circ\vec{r})(t) \| \frac{dr}{dt}\| dt.
|
|||
|
~
|
|||
|
\]</span></p>
|
|||
|
<p>For example, if <span class="math inline">\(f(x,y) = 2 - x^2 - y^2\)</span> and <span class="math inline">\(r(t) = 1/t \langle \cos(t), \sin(t) \rangle\)</span>, then the line integral over <span class="math inline">\([1,2]\)</span> is given by:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="105">
|
|||
|
<div class="sourceCode cell-code" id="cb165"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb165-1"><a href="#cb165-1" aria-hidden="true" tabindex="-1"></a><span class="fu">f</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="cb165-2"><a href="#cb165-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="cb165-3"><a href="#cb165-3" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>(t) <span class="op">=</span> [<span class="fu">cos</span>(t), <span class="fu">sin</span>(t)]<span class="op">/</span>t</span>
|
|||
|
<span id="cb165-4"><a href="#cb165-4" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb165-5"><a href="#cb165-5" aria-hidden="true" tabindex="-1"></a><span class="fu">integrand</span>(t) <span class="op">=</span> (f<span class="op">∘</span>r)(t) <span class="op">*</span> <span class="fu">norm</span>(r<span class="op">'</span>(t))</span>
|
|||
|
<span id="cb165-6"><a href="#cb165-6" aria-hidden="true" tabindex="-1"></a><span class="fu">quadgk</span>(integrand, <span class="fl">1</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="105">
|
|||
|
<pre><code>(1.2399213772953277, 4.525271268818187e-9)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>To integrate a line integral through a vector field, say <span class="math inline">\(\int_C F \cdot\hat{T} ds=\int_C F\cdot \vec{r}'(t) dt\)</span> we have, for example,</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="106">
|
|||
|
<div class="sourceCode cell-code" id="cb167"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb167-1"><a href="#cb167-1" aria-hidden="true" tabindex="-1"></a><span class="fu">F</span>(x,y) <span class="op">=</span> [<span class="op">-</span>y, x]</span>
|
|||
|
<span id="cb167-2"><a href="#cb167-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="cb167-3"><a href="#cb167-3" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>(t) <span class="op">=</span> [<span class="fu">cos</span>(t), <span class="fu">sin</span>(t)]<span class="op">/</span>t</span>
|
|||
|
<span id="cb167-4"><a href="#cb167-4" aria-hidden="true" tabindex="-1"></a><span class="fu">integrand</span>(t) <span class="op">=</span> (F<span class="op">∘</span>r)(t) <span class="op">⋅</span> r<span class="op">'</span>(t)</span>
|
|||
|
<span id="cb167-5"><a href="#cb167-5" aria-hidden="true" tabindex="-1"></a><span class="fu">quadgk</span>(integrand, <span class="fl">1</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="106">
|
|||
|
<pre><code>(0.5, 2.1134927141730486e-10)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<hr>
|
|||
|
<p>Symbolically, there is no real difference from a 1-dimensional integral. Let <span class="math inline">\(\phi = 1/\|r\|\)</span> and integrate the gradient field over one turn of the helix <span class="math inline">\(\vec{r}(t) = \langle \cos(t), \sin(t), t\rangle\)</span>.</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="107">
|
|||
|
<div class="sourceCode cell-code" id="cb169"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb169-1"><a href="#cb169-1" 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>z<span class="op">::</span><span class="dt">real </span>t<span class="op">::</span><span class="dt">real</span></span>
|
|||
|
<span id="cb169-2"><a href="#cb169-2" aria-hidden="true" tabindex="-1"></a><span class="fu">phi</span>(x,y,z) <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fu">sqrt</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="cb169-3"><a href="#cb169-3" aria-hidden="true" tabindex="-1"></a><span class="fu">r</span>(t) <span class="op">=</span> [<span class="fu">cos</span>(t), <span class="fu">sin</span>(t), t]</span>
|
|||
|
<span id="cb169-4"><a href="#cb169-4" aria-hidden="true" tabindex="-1"></a>∇phi <span class="op">=</span> <span class="fu">diff</span>.(<span class="fu">phi</span>(x,y,z), [x,y,z])</span>
|
|||
|
<span id="cb169-5"><a href="#cb169-5" aria-hidden="true" tabindex="-1"></a>∇phi_r <span class="op">=</span> <span class="fu">subs</span>.(∇phi, x<span class="op">.=></span> <span class="fu">r</span>(t)[<span class="fl">1</span>], y<span class="op">.=></span><span class="fu">r</span>(t)[<span class="fl">2</span>], z<span class="op">.=></span><span class="fu">r</span>(t)[<span class="fl">3</span>])</span>
|
|||
|
<span id="cb169-6"><a href="#cb169-6" aria-hidden="true" tabindex="-1"></a>rp <span class="op">=</span> <span class="fu">diff</span>.(<span class="fu">r</span>(t), t)</span>
|
|||
|
<span id="cb169-7"><a href="#cb169-7" aria-hidden="true" tabindex="-1"></a><span class="kw">global</span> helix <span class="op">=</span> <span class="fu">simplify</span>(∇phi_r <span class="op">⋅</span> rp )</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="107">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
- \frac{t}{\left(t^{2} + 1\right)^{\frac{3}{2}}}
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Then</p>
|
|||
|
<div class="cell" data-execution_count="108">
|
|||
|
<div class="sourceCode cell-code" id="cb170"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb170-1"><a href="#cb170-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> t<span class="op">::</span><span class="dt">real</span></span>
|
|||
|
<span id="cb170-2"><a href="#cb170-2" aria-hidden="true" tabindex="-1"></a><span class="fu">integrate</span>(helix, (t, <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="108">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
-1 + \frac{1}{\sqrt{1 + 4 \pi^{2}}}
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</section>
|
|||
|
<section id="surface-integrals" class="level3" data-number="70.9.4">
|
|||
|
<h3 data-number="70.9.4" class="anchored" data-anchor-id="surface-integrals"><span class="header-section-number">70.9.4</span> Surface integrals</h3>
|
|||
|
<p>The surface integral for a parameterized surface involves a surface element <span class="math inline">\(\|\partial\Phi/\partial{u} \times \partial\Phi/\partial{v}\|\)</span>. This can be computed numerically with:</p>
|
|||
|
<div class="cell" data-execution_count="109">
|
|||
|
<div class="sourceCode cell-code" id="cb171"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb171-1"><a href="#cb171-1" aria-hidden="true" tabindex="-1"></a><span class="fu">Phi</span>(u,v) <span class="op">=</span> [<span class="fu">u*cos</span>(v), <span class="fu">u*sin</span>(v), u]</span>
|
|||
|
<span id="cb171-2"><a href="#cb171-2" aria-hidden="true" tabindex="-1"></a><span class="fu">Phi</span>(v) <span class="op">=</span> <span class="fu">Phi</span>(v<span class="op">...</span>)</span>
|
|||
|
<span id="cb171-3"><a href="#cb171-3" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb171-4"><a href="#cb171-4" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">SE</span>(Phi, pt)</span>
|
|||
|
<span id="cb171-5"><a href="#cb171-5" aria-hidden="true" tabindex="-1"></a> J <span class="op">=</span> ForwardDiff.<span class="fu">jacobian</span>(Phi, pt)</span>
|
|||
|
<span id="cb171-6"><a href="#cb171-6" aria-hidden="true" tabindex="-1"></a> J[<span class="op">:</span>,<span class="fl">1</span>] <span class="op">×</span> J[<span class="op">:</span>,<span class="fl">2</span>]</span>
|
|||
|
<span id="cb171-7"><a href="#cb171-7" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span>
|
|||
|
<span id="cb171-8"><a href="#cb171-8" aria-hidden="true" tabindex="-1"></a></span>
|
|||
|
<span id="cb171-9"><a href="#cb171-9" aria-hidden="true" tabindex="-1"></a><span class="fu">norm</span>(<span class="fu">SE</span>(Phi, [<span class="fl">1</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="109">
|
|||
|
<pre><code>1.4142135623730951</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>To find the surface integral (<span class="math inline">\(f=1\)</span>) for this surface over <span class="math inline">\([0,1] \times [0,2\pi]\)</span>, we have:</p>
|
|||
|
<div class="cell" data-execution_count="110">
|
|||
|
<div class="sourceCode cell-code" id="cb173"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb173-1"><a href="#cb173-1" aria-hidden="true" tabindex="-1"></a><span class="fu">hcubature</span>(pt <span class="op">-></span> <span class="fu">norm</span>(<span class="fu">SE</span>(Phi, pt)), (<span class="fl">0.0</span>,<span class="fl">0.0</span>), (<span class="fl">1.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="110">
|
|||
|
<pre><code>(4.442882938158366, 2.6645352591003757e-15)</code></pre>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Symbolically, the approach is similar:</p>
|
|||
|
<div class="cell" data-execution_count="111">
|
|||
|
<div class="sourceCode cell-code" id="cb175"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb175-1"><a href="#cb175-1" aria-hidden="true" tabindex="-1"></a><span class="pp">@syms</span> u<span class="op">::</span><span class="dt">real </span>v<span class="op">::</span><span class="dt">real</span></span>
|
|||
|
<span id="cb175-2"><a href="#cb175-2" aria-hidden="true" tabindex="-1"></a>exₚ <span class="op">=</span> <span class="fu">Phi</span>(u,v)</span>
|
|||
|
<span id="cb175-3"><a href="#cb175-3" aria-hidden="true" tabindex="-1"></a>Jₚ <span class="op">=</span> exₚ.<span class="fu">jacobian</span>([u,v])</span>
|
|||
|
<span id="cb175-4"><a href="#cb175-4" aria-hidden="true" tabindex="-1"></a>SurfEl <span class="op">=</span> <span class="fu">norm</span>(Jₚ[<span class="op">:</span>,<span class="fl">1</span>] <span class="op">×</span> Jₚ[<span class="op">:</span>,<span class="fl">2</span>]) <span class="op">|></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="111">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
\sqrt{2} \left|{u}\right|
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Then</p>
|
|||
|
<div class="cell" data-execution_count="112">
|
|||
|
<div class="sourceCode cell-code" id="cb176"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb176-1"><a href="#cb176-1" aria-hidden="true" tabindex="-1"></a><span class="fu">integrate</span>(SurfEl, (u, <span class="fl">0</span>, <span class="fl">1</span>), (v, <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="112">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
\sqrt{2} \pi
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<p>Integrating a vector field over the surface, would be similar:</p>
|
|||
|
<div class="cell" data-hold="true" data-execution_count="113">
|
|||
|
<div class="sourceCode cell-code" id="cb177"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb177-1"><a href="#cb177-1" aria-hidden="true" tabindex="-1"></a><span class="fu">F</span>(x,y,z) <span class="op">=</span> [x, y, z]</span>
|
|||
|
<span id="cb177-2"><a href="#cb177-2" aria-hidden="true" tabindex="-1"></a>ex <span class="op">=</span> <span class="fu">F</span>(<span class="fu">Phi</span>(u,v)<span class="op">...</span>) <span class="op">⋅</span> (Jₚ[<span class="op">:</span>,<span class="fl">1</span>] <span class="op">×</span> Jₚ[<span class="op">:</span>,<span class="fl">2</span>])</span>
|
|||
|
<span id="cb177-3"><a href="#cb177-3" aria-hidden="true" tabindex="-1"></a><span class="fu">integrate</span>(ex, (u,<span class="fl">0</span>,<span class="fl">1</span>), (v, <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="113">
|
|||
|
<span class="math-left-align" style="padding-left: 4px; width:0; float:left;">
|
|||
|
\[
|
|||
|
0
|
|||
|
\]
|
|||
|
</span>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
</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="../misc/unicode.html" class="pagination-link">
|
|||
|
<i class="bi bi-arrow-left-short"></i> <span class="nav-page-text"><span class="chapter-number">69</span> <span class="chapter-title">Usages of Unicode symbols</span></span>
|
|||
|
</a>
|
|||
|
</div>
|
|||
|
<div class="nav-page nav-page-next">
|
|||
|
<a href="../references.html" class="pagination-link">
|
|||
|
<span class="nav-page-text">References</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>
|