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

3332 lines
216 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.0.32">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Calculus with Julia - 7&nbsp; Functions</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script src="../site_libs/quarto-nav/quarto-nav.js"></script>
<script src="../site_libs/quarto-nav/headroom.min.js"></script>
<script src="../site_libs/clipboard/clipboard.min.js"></script>
<script src="../site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="../site_libs/quarto-search/fuse.min.js"></script>
<script src="../site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="../">
<link href="../precalc/plotting.html" rel="next">
<link href="../precalc/ranges.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">7</span>&nbsp; <span class="chapter-title">Functions</span></h1>
<button type="button" class="quarto-btn-toggle btn" aria-label="Show secondary navigation">
<i class="bi bi-chevron-right"></i>
</button>
</div>
</nav>
</header>
<!-- content -->
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
<!-- sidebar -->
<nav id="quarto-sidebar" class="sidebar collapse sidebar-navigation floating overflow-auto">
<div class="mt-2 flex-shrink-0 align-items-center">
<div class="sidebar-search">
<div id="quarto-search" class="" title="Search"></div>
</div>
</div>
<div class="sidebar-menu-container">
<ul class="list-unstyled mt-1">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../index.html" class="sidebar-item-text sidebar-link">Preface</a>
</div>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" aria-expanded="true">Precalculus Concepts</a>
<a class="sidebar-item-toggle text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" aria-expanded="true">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-1" class="collapse list-unstyled sidebar-section depth1 show">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/calculator.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">1</span>&nbsp; <span class="chapter-title">From calculator to computer</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/variables.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">2</span>&nbsp; <span class="chapter-title">Variables</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/numbers_types.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">3</span>&nbsp; <span class="chapter-title">Number systems</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/logical_expressions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">4</span>&nbsp; <span class="chapter-title">Inequalities, Logical expressions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/vectors.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">5</span>&nbsp; <span class="chapter-title">Vectors</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/ranges.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">6</span>&nbsp; <span class="chapter-title">Ranges and Sets</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/functions.html" class="sidebar-item-text sidebar-link active"><span class="chapter-number">7</span>&nbsp; <span class="chapter-title">Functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">8</span>&nbsp; <span class="chapter-title">The Graph of a Function</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/transformations.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">9</span>&nbsp; <span class="chapter-title">Function manipulations</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/inversefunctions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">10</span>&nbsp; <span class="chapter-title">The Inverse of a Function</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/polynomial.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">11</span>&nbsp; <span class="chapter-title">Polynomials</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/polynomial_roots.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">12</span>&nbsp; <span class="chapter-title">Roots of a polynomial</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/polynomials_package.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">13</span>&nbsp; <span class="chapter-title">The Polynomials package</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/rational_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">14</span>&nbsp; <span class="chapter-title">Rational functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/exp_log_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">15</span>&nbsp; <span class="chapter-title">Exponential and logarithmic functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/trig_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">16</span>&nbsp; <span class="chapter-title">Trigonometric functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../precalc/julia_overview.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">17</span>&nbsp; <span class="chapter-title">Overview of Julia commands</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-2" aria-expanded="false">Limits</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-2" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-2" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../limits/limits.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">18</span>&nbsp; <span class="chapter-title">Limits</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../limits/limits_extensions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">19</span>&nbsp; <span class="chapter-title">Limits, issues, extensions of the concept</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../limits/continuity.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">20</span>&nbsp; <span class="chapter-title">Continuity</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../limits/intermediate_value_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">21</span>&nbsp; <span class="chapter-title">Implications of continuity</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-3" aria-expanded="false">Derivatives</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-3" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-3" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">22</span>&nbsp; <span class="chapter-title">Derivatives</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/numeric_derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">23</span>&nbsp; <span class="chapter-title">Numeric derivatives</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/symbolic_derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">24</span>&nbsp; <span class="chapter-title">Symbolic derivatives</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/mean_value_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">25</span>&nbsp; <span class="chapter-title">The mean value theorem for differentiable functions.</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/optimization.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">26</span>&nbsp; <span class="chapter-title">Optimization</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/first_second_derivatives.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">27</span>&nbsp; <span class="chapter-title">The first and second derivatives</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/curve_sketching.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">28</span>&nbsp; <span class="chapter-title">Curve Sketching</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/linearization.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">29</span>&nbsp; <span class="chapter-title">Linearization</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/newtons_method.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">30</span>&nbsp; <span class="chapter-title">Newtons method</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/more_zeros.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">31</span>&nbsp; <span class="chapter-title">Derivative-free alternatives to Newtons method</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/lhospitals_rule.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">32</span>&nbsp; <span class="chapter-title">LHospitals Rule</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/implicit_differentiation.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">33</span>&nbsp; <span class="chapter-title">Implicit Differentiation</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/related_rates.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">34</span>&nbsp; <span class="chapter-title">Related rates</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../derivatives/taylor_series_polynomials.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">35</span>&nbsp; <span class="chapter-title">Taylor Polynomials and other Approximating Polynomials</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-4" aria-expanded="false">Integrals</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-4" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-4" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/area.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">36</span>&nbsp; <span class="chapter-title">Area under a curve</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/ftc.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">37</span>&nbsp; <span class="chapter-title">Fundamental Theorem or Calculus</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/substitution.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">38</span>&nbsp; <span class="chapter-title">Substitution</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/integration_by_parts.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">39</span>&nbsp; <span class="chapter-title">Integration By Parts</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/partial_fractions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">40</span>&nbsp; <span class="chapter-title">Partial Fractions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/improper_integrals.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">41</span>&nbsp; <span class="chapter-title">Improper Integrals</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/mean_value_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">42</span>&nbsp; <span class="chapter-title">Mean value theorem for integrals</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/area_between_curves.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">43</span>&nbsp; <span class="chapter-title">Area between two curves</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/center_of_mass.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">44</span>&nbsp; <span class="chapter-title">Center of Mass</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/volumes_slice.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">45</span>&nbsp; <span class="chapter-title">Volumes by slicing</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/arc_length.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">46</span>&nbsp; <span class="chapter-title">Arc length</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integrals/surface_area.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">47</span>&nbsp; <span class="chapter-title">Surface Area</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-5" aria-expanded="false">ODEs</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-5" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-5" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../ODEs/odes.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">48</span>&nbsp; <span class="chapter-title">ODEs</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../ODEs/euler.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">49</span>&nbsp; <span class="chapter-title">Eulers method</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../ODEs/solve.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">50</span>&nbsp; <span class="chapter-title">The problem-algorithm-solve interface</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../ODEs/differential_equations.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">51</span>&nbsp; <span class="chapter-title">The <code>DifferentialEquations</code> suite</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start 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>&nbsp; <span class="chapter-title">Polar Coordinates and Curves</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/vectors.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">53</span>&nbsp; <span class="chapter-title">Vectors and matrices</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/vector_valued_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">54</span>&nbsp; <span class="chapter-title">Vector-valued functions, <span class="math inline">\(f:R \rightarrow R^n\)</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/scalar_functions.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">55</span>&nbsp; <span class="chapter-title">Scalar functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/scalar_functions_applications.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">56</span>&nbsp; <span class="chapter-title">Applications with scalar functions</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/vector_fields.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">57</span>&nbsp; <span class="chapter-title">Functions <span class="math inline">\(R^n \rightarrow R^m\)</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../differentiable_vector_calculus/plots_plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">58</span>&nbsp; <span class="chapter-title">2D and 3D plots in Julia with Plots</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-7" aria-expanded="false">Integral vector calculus</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-7" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-7" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/double_triple_integrals.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">59</span>&nbsp; <span class="chapter-title">Multi-dimensional integrals</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/line_integrals.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">60</span>&nbsp; <span class="chapter-title">Line and Surface Integrals</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/div_grad_curl.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">61</span>&nbsp; <span class="chapter-title">The Gradient, Divergence, and Curl</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/stokes_theorem.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">62</span>&nbsp; <span class="chapter-title">Greens Theorem, Stokes Theorem, and the Divergence Theorem</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../integral_vector_calculus/review.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">63</span>&nbsp; <span class="chapter-title">Quick Review of Vector Calculus</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-8" aria-expanded="false">Alternatives</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-8" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-8" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../alternatives/plotly_plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">64</span>&nbsp; <span class="chapter-title">JavaScript based plotting libraries</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../alternatives/makie_plotting.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">65</span>&nbsp; <span class="chapter-title">Calculus plots with Makie</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-9" aria-expanded="false">Appendices</a>
<a class="sidebar-item-toggle text-start collapsed" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-9" aria-expanded="false">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-9" class="collapse list-unstyled sidebar-section depth1 ">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/getting_started_with_julia.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">66</span>&nbsp; <span class="chapter-title">Getting started with Julia</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/julia_interfaces.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">67</span>&nbsp; <span class="chapter-title">Julia interfaces</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/calculus_with_julia.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">68</span>&nbsp; <span class="chapter-title">The <code>CalculusWithJulia</code> package</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/unicode.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">69</span>&nbsp; <span class="chapter-title">Usages of Unicode symbols</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../misc/quick_notes.html" class="sidebar-item-text sidebar-link"><span class="chapter-number">70</span>&nbsp; <span class="chapter-title">Quick introduction to Calculus with Julia</span></a>
</div>
</li>
</ul>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../references.html" class="sidebar-item-text sidebar-link">References</a>
</div>
</li>
</ul>
</div>
</nav>
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#defining-simple-mathematical-functions" id="toc-defining-simple-mathematical-functions" class="nav-link active" data-scroll-target="#defining-simple-mathematical-functions"> <span class="header-section-number">7.1</span> Defining simple mathematical functions</a>
<ul class="collapse">
<li><a href="#the-domain-of-a-function" id="toc-the-domain-of-a-function" class="nav-link" data-scroll-target="#the-domain-of-a-function"> <span class="header-section-number">7.1.1</span> The domain of a function</a></li>
<li><a href="#equations-functions-calling-a-function" id="toc-equations-functions-calling-a-function" class="nav-link" data-scroll-target="#equations-functions-calling-a-function"> <span class="header-section-number">7.1.2</span> Equations, functions, calling a function</a></li>
<li><a href="#cases" id="toc-cases" class="nav-link" data-scroll-target="#cases"> <span class="header-section-number">7.1.3</span> Cases</a></li>
</ul></li>
<li><a href="#functions-defined-with-the-function-keyword" id="toc-functions-defined-with-the-function-keyword" class="nav-link" data-scroll-target="#functions-defined-with-the-function-keyword"> <span class="header-section-number">7.2</span> Functions defined with the “function” keyword</a></li>
<li><a href="#parameters-function-context-scope-keyword-arguments" id="toc-parameters-function-context-scope-keyword-arguments" class="nav-link" data-scroll-target="#parameters-function-context-scope-keyword-arguments"> <span class="header-section-number">7.3</span> Parameters, function context (scope), keyword arguments</a>
<ul class="collapse">
<li><a href="#the-fxp-style-for-parameterization" id="toc-the-fxp-style-for-parameterization" class="nav-link" data-scroll-target="#the-fxp-style-for-parameterization"> <span class="header-section-number">7.3.1</span> The <code>f(x,p)</code> style for parameterization</a></li>
</ul></li>
<li><a href="#multiple-dispatch" id="toc-multiple-dispatch" class="nav-link" data-scroll-target="#multiple-dispatch"> <span class="header-section-number">7.4</span> Multiple dispatch</a></li>
<li><a href="#function-application" id="toc-function-application" class="nav-link" data-scroll-target="#function-application"> <span class="header-section-number">7.5</span> Function application</a></li>
<li><a href="#other-types-of-functions" id="toc-other-types-of-functions" class="nav-link" data-scroll-target="#other-types-of-functions"> <span class="header-section-number">7.6</span> Other types of functions</a>
<ul class="collapse">
<li><a href="#anonymous-functions" id="toc-anonymous-functions" class="nav-link" data-scroll-target="#anonymous-functions"> <span class="header-section-number">7.6.1</span> Anonymous functions</a></li>
<li><a href="#the-do-notation" id="toc-the-do-notation" class="nav-link" data-scroll-target="#the-do-notation"> <span class="header-section-number">7.6.2</span> The <code>do</code> notation</a></li>
</ul></li>
<li><a href="#questions" id="toc-questions" class="nav-link" data-scroll-target="#questions"> <span class="header-section-number">7.7</span> Questions</a></li>
</ul>
<div class="toc-actions"><div><i class="bi bi-github"></i></div><div class="action-links"><p><a href="https://github.com/jverzani/CalculusWithJuliaNotes.jl/edit/main/quarto/precalc/functions.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">7</span>&nbsp; <span class="chapter-title">Functions</span></h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<p>This section will use the following add-on packages:</p>
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">using</span> <span class="bu">CalculusWithJulia</span>, <span class="bu">Plots</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<hr>
<p>A mathematical <a href="http://en.wikipedia.org/wiki/Function_(mathematics)">function</a> is defined abstractly by:</p>
<blockquote class="blockquote">
<p><strong>Function:</strong> A function is a <em>relation</em> which assigns to each element in the domain a <em>single</em> element in the range. A <strong>relation</strong> is a set of ordered pairs, <span class="math inline">\((x,y)\)</span>. The set of first coordinates is the domain, the set of second coordinates the range of the relation.</p>
</blockquote>
<p>That is, a function gives a correspondence between values in its domain with values in its range.</p>
<p>This definition is abstract, as functions can be very general. With single-variable calculus, we generally specialize to real-valued functions of a single variable (<em>univariate, scalar functions</em>). These typically have the correspondence given by a rule, such as <span class="math inline">\(f(x) = x^2\)</span> or <span class="math inline">\(f(x) = \sqrt{x}\)</span>. The functions domain may be implicit (as in all <span class="math inline">\(x\)</span> for which the rule is defined) or may be explicitly given as part of the rule. The functions range is then the image of its domain, or the set of all <span class="math inline">\(f(x)\)</span> for each <span class="math inline">\(x\)</span> in the domain (<span class="math inline">\(\{f(x): x \in \text{ domain}\}\)</span>).</p>
<p>Some examples of mathematical functions are:</p>
<p><span class="math display">\[
f(x) = \cos(x), \quad g(x) = x^2 - x, \quad h(x) = \sqrt{x}, \quad
s(x) = \begin{cases} -1 &amp; x &lt; 0\\1&amp;x&gt;0\end{cases}.
\]</span></p>
<p>For these examples, the domain of both <span class="math inline">\(f(x)\)</span> and <span class="math inline">\(g(x)\)</span> is all real values of <span class="math inline">\(x\)</span>, where as for <span class="math inline">\(h(x)\)</span> it is implicitly just the set of non-negative numbers, <span class="math inline">\([0, \infty)\)</span>. Finally, for <span class="math inline">\(s(x)\)</span>, we can see that the domain is defined for every <span class="math inline">\(x\)</span> but <span class="math inline">\(0\)</span>.</p>
<p>In general the range is harder to identify than the domain, and this is the case for these functions too. For <span class="math inline">\(f(x)\)</span> we may know the <span class="math inline">\(\cos\)</span> function is trapped in <span class="math inline">\([-1,1]\)</span> and it is intuitively clear than all values in that set are possible. The function <span class="math inline">\(h(x)\)</span> would have range <span class="math inline">\([0,\infty)\)</span>. The <span class="math inline">\(s(x)\)</span> function is either <span class="math inline">\(-1\)</span> or <span class="math inline">\(1\)</span>, so only has two possible values in its range. What about <span class="math inline">\(g(x)\)</span>? It is a parabola that opens upward, so any <span class="math inline">\(y\)</span> values below the <span class="math inline">\(y\)</span> value of its vertex will not appear in the range. In this case, the symmetry indicates that the vertex will be at <span class="math inline">\((1/2, -1/4)\)</span>, so the range is <span class="math inline">\([-1/4, \infty)\)</span>.</p>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>Thanks to Euler (1707-1783):</strong> The formal idea of a function is a relatively modern concept in mathematics. According to <a href="http://www.maa.org/sites/default/files/pdf/upload_library/22/Ford/dunham1.pdf">Dunham</a>,</p>
</div>
</div>
<p>Euler defined a function as an “analytic expression composed in any way whatsoever of the variable quantity and numbers or constant quantities.” He goes on to indicate that as Euler matured, so did his notion of function, ending up closer to the modern idea of a correspondence not necessarily tied to a particular formula or “analytic expression.” He finishes by saying: “It is fair to say that we now study functions in analysis because of him.”</p>
<p>We will see that defining functions within <code>Julia</code> can be as simple a concept as Euler started with, but that the more abstract concept has a great advantage that is exploited in the design of the language.</p>
<section id="defining-simple-mathematical-functions" class="level2" data-number="7.1">
<h2 data-number="7.1" class="anchored" data-anchor-id="defining-simple-mathematical-functions"><span class="header-section-number">7.1</span> Defining simple mathematical functions</h2>
<p>The notation <code>Julia</code> uses to define simple mathematical functions could not be more closely related to how they are written mathematically. For example, the functions <span class="math inline">\(f(x)\)</span>, <span class="math inline">\(g(x)\)</span>, and <span class="math inline">\(h(x)\)</span> above may be defined by:</p>
<div class="cell" data-execution_count="4">
<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="fu">f</span>(x) <span class="op">=</span> <span class="fu">cos</span>(x)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="fu">g</span>(x) <span class="op">=</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> x</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="fu">h</span>(x) <span class="op">=</span> <span class="fu">sqrt</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>h (generic function with 1 method)</code></pre>
</div>
</div>
<p>The left-hand sign of the equals sign is an assignment. In this use, a function with a given signature is defined and attached to a method table for the given function name. The right-hand side is simply <code>Julia</code> code to compute the <em>rule</em> corresponding to the function.</p>
<p>Calling the function also follows standard math notation:</p>
<div class="cell" 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>(<span class="cn">pi</span>), <span class="fu">g</span>(<span class="fl">2</span>), <span class="fu">h</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="6">
<pre><code>(-1.0, 2, 2.0)</code></pre>
</div>
</div>
<p>For typical cases like the three above, there isnt really much new to learn.</p>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The equals sign in <code>Julia</code> always indicates either an assignment or a mutation of the object on the left side. The definition of a function above is an <em>assignment</em>, in that a function is added (or modified) in a table holding the methods associated with the functions name.</p>
<p>The equals sign restricts the expressions available on the <em>left</em>-hand side to a) a variable name, for assignment; b) mutating an object at an index, as in <code>xs[1]</code>; c) mutating a property of a stuct; or d) a function assignment following this form <code>function_name(args...)</code>.</p>
<p>Whereas function definitions and usage in <code>Julia</code> mirrors standard math notation; equations in math are not so mirrored in <code>Julia</code>. In mathematical equations, the left-hand of an equation is typically a complicated algebraic expression. Not so with <code>Julia</code>, where the left hand side of the equals sign is prescribed and quite limited.</p>
</div>
</div>
<section id="the-domain-of-a-function" class="level3" data-number="7.1.1">
<h3 data-number="7.1.1" class="anchored" data-anchor-id="the-domain-of-a-function"><span class="header-section-number">7.1.1</span> The domain of a function</h3>
<p>Functions in <code>Julia</code> have an implicit domain, just as they do mathematically. In the case of <span class="math inline">\(f(x)\)</span> and <span class="math inline">\(g(x)\)</span>, the right-hand side is defined for all real values of <span class="math inline">\(x\)</span>, so the domain is all <span class="math inline">\(x\)</span>. For <span class="math inline">\(h(x)\)</span> this isnt the case, of course. Trying to call <span class="math inline">\(h(x)\)</span> when <span class="math inline">\(x &lt; 0\)</span> will give an error:</p>
<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="fu">h</span>(<span class="op">-</span><span class="fl">1</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-error">
<pre><code>LoadError: DomainError with -1.0:
sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).</code></pre>
</div>
</div>
<p>The <code>DomainError</code> is one of many different error types <code>Julia</code> has, in this case it is quite apt: the value <span class="math inline">\(-1\)</span> is not in the domain of the function.</p>
</section>
<section id="equations-functions-calling-a-function" class="level3" data-number="7.1.2">
<h3 data-number="7.1.2" class="anchored" data-anchor-id="equations-functions-calling-a-function"><span class="header-section-number">7.1.2</span> Equations, functions, calling a function</h3>
<p>Mathematically we tend to blur the distinction between the equation</p>
<p><span class="math display">\[
y = 5/9 \cdot (x - 32)
\]</span></p>
<p>and the function</p>
<p><span class="math display">\[
f(x) = 5/9 \cdot (x - 32)
\]</span></p>
<p>In fact, the graph of a function <span class="math inline">\(f(x)\)</span> is simply defined as the graph of the equation <span class="math inline">\(y=f(x)\)</span>. There is a distinction in <code>Julia</code> as a command such as</p>
<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>x <span class="op">=</span> <span class="op">-</span><span class="fl">40</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>y <span class="op">=</span> <span class="fl">5</span><span class="op">/</span><span class="fl">9</span> <span class="op">*</span> (x <span class="op">-</span> <span class="fl">32</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>-40.0</code></pre>
</div>
</div>
<p>will evaluate the right-hand side with the value of <code>x</code> bound at the time of assignment to <code>y</code>, whereas assignment to a function</p>
<div class="cell" data-hold="true" 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">f</span>(x) <span class="op">=</span> <span class="fl">5</span><span class="op">/</span><span class="fl">9</span> <span class="op">*</span> (x <span class="op">-</span> <span class="fl">32</span>)</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(<span class="fl">72</span>) <span class="co">## room temperature</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="9">
<pre><code>22.22222222222222</code></pre>
</div>
</div>
<p>will create a function object with a value of <code>x</code> determined at a later time - the time the function is called. So the value of <code>x</code> defined when the function is created is not important here (as the value of <code>x</code> used by <code>f</code> is passed in as an argument).</p>
<p>Within <code>Julia</code>, we make note of the distinction between a function object versus a function call. In the definition <code>f(x)=cos(x)</code>, the variable <code>f</code> refers to a function object, whereas the expression <code>f(pi)</code> is a function call. This mirrors the math notation where an <span class="math inline">\(f\)</span> is used when properties of a function are being emphasized (such as <span class="math inline">\(f \circ g\)</span> for composition) and <span class="math inline">\(f(x)\)</span> is used when the values related to the function are being emphasized (such as saying “the plot of the equation <span class="math inline">\(y=f(x)\)</span>).</p>
<p>Distinguishing these three related but different concepts (equations, function objects, and function calls) is important when modeling on the computer.</p>
</section>
<section id="cases" class="level3" data-number="7.1.3">
<h3 data-number="7.1.3" class="anchored" data-anchor-id="cases"><span class="header-section-number">7.1.3</span> Cases</h3>
<p>The definition of <span class="math inline">\(s(x)\)</span> above has two cases:</p>
<p><span class="math display">\[
s(x) = \begin{cases} -1 &amp; s &lt; 0\\ 1 &amp; s &gt; 0. \end{cases}
\]</span></p>
<p>We learn to read this as: when <span class="math inline">\(s\)</span> is less than <span class="math inline">\(0\)</span>, then the answer is <span class="math inline">\(-1\)</span>. If <span class="math inline">\(s\)</span> is greater than <span class="math inline">\(0\)</span> the answer is <span class="math inline">\(1.\)</span> Often - but not in this example - there is an “otherwise” case to catch those values of <span class="math inline">\(x\)</span> that are not explicitly mentioned. As there is no such “otherwise” case here, we can see that this function has no definition when <span class="math inline">\(x=0\)</span>. This function is often called the “sign” function and is also defined by <span class="math inline">\(\lvert x\rvert/x\)</span>. (<code>Julia</code>s <code>sign</code> function actually defines <code>sign(0)</code> to be <code>0</code>.)</p>
<p>How do we create conditional statements in <code>Julia</code>? Programming languages generally have “if-then-else” constructs to handle conditional evaluation. In <code>Julia</code>, the following code will handle the above condition:</p>
<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="cf">if</span> x <span class="op">&lt;</span> <span class="fl">0</span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> <span class="op">-</span><span class="fl">1</span></span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a><span class="cf">elseif</span> x <span class="op">&gt;</span> <span class="fl">0</span></span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> <span class="fl">1</span></span>
<span id="cb12-5"><a href="#cb12-5" 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>
<p>The “otherwise” case would be caught with an <code>else</code> addition. So, for example, this would implement <code>Julia</code>s definition of <code>sign</code> (which also assigns <span class="math inline">\(0\)</span> to <span class="math inline">\(0\)</span>):</p>
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> x <span class="op">&lt;</span> <span class="fl">0</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> <span class="op">-</span><span class="fl">1</span></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a><span class="cf">elseif</span> x <span class="op">&gt;</span> <span class="fl">0</span></span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a> <span class="fl">1</span></span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a><span class="cf">else</span></span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a> <span class="fl">0</span></span>
<span id="cb13-7"><a href="#cb13-7" 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>
<p>The conditions for the <code>if</code> statements are expressions that evaluate to either <code>true</code> or <code>false</code>, such as generated by the Boolean operators <code>&lt;</code>, <code>&lt;=</code>, <code>==</code>, <code>!-</code>, <code>&gt;=</code>, and <code>&gt;</code>.</p>
<p>If familiar with <code>if</code> conditions, they are natural to use. However, for simpler cases of “if-else” <code>Julia</code> provides the more convenient <em>ternary</em> operator: <code>cond ? if_true : if_false</code>. (The name comes from the fact that there are three arguments specified.) The ternary operator checks the condition and if true returns the first expression, whereas if the condition is false the second condition is returned. Both expressions are evaluated. (The <a href="http://julia.readthedocs.org/en/latest/manual/control-flow/#short-circuit-evaluation">short-circuit</a> operators can be used to avoid both evaluations.)</p>
<p>For example, here is one way to define an absolute value function:</p>
<div class="cell" data-execution_count="11">
<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">abs_val</span>(x) <span class="op">=</span> x <span class="op">&gt;=</span> <span class="fl">0</span> ? x <span class="op">:</span> <span class="op">-</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="10">
<pre><code>abs_val (generic function with 1 method)</code></pre>
</div>
</div>
<p>The condition is <code>x &gt;= 0</code> - or is <code>x</code> non-negative? If so, the value <code>x</code> is used, otherwise <code>-x</code> is used.</p>
<p>Here is a means to implement a function which takes the larger of <code>x</code> or <code>10</code>:</p>
<div class="cell" data-execution_count="12">
<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">bigger_10</span>(x) <span class="op">=</span> x <span class="op">&gt;</span> <span class="fl">10</span> ? x <span class="op">:</span> <span class="fl">10.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="11">
<pre><code>bigger_10 (generic function with 1 method)</code></pre>
</div>
</div>
<p>(This could also utilize the <code>max</code> function: <code>f(x) = max(x, 10.0)</code>.)</p>
<p>Or similarly, a function to represent a cell phone plan where the first <span class="math inline">\(500\)</span> minutes are <span class="math inline">\(20\)</span> dollars and every additional minute is <span class="math inline">\(5\)</span> cents:</p>
<div class="cell" data-execution_count="13">
<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">cellplan</span>(x) <span class="op">=</span> x <span class="op">&lt;</span> <span class="fl">500</span> ? <span class="fl">20.0</span> <span class="op">:</span> <span class="fl">20.0</span> <span class="op">+</span> <span class="fl">0.05</span> <span class="op">*</span> (x<span class="op">-</span><span class="fl">500</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>cellplan (generic function with 1 method)</code></pre>
</div>
</div>
<div class="callout-warning 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">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Type stability. These last two definitions used <code>10.0</code> and <code>20.0</code> instead of the integers <code>10</code> and <code>20</code> for the answer. Why the extra typing? When <code>Julia</code> can predict the type of the output from the type of inputs, it can be more efficient. So when possible, we help out and ensure the output is always the same type.</p>
</div>
</div>
<section id="example" class="level5">
<h5 class="anchored" data-anchor-id="example">Example</h5>
<p>The <code>ternary</code> operator can be used to define an explicit domain. For example, a falling body might have height given by <span class="math inline">\(h(t) = 10 - 16t^2\)</span>. This model only applies for non-negative <span class="math inline">\(t\)</span> and non-negative <span class="math inline">\(h\)</span> values. So, in particular <span class="math inline">\(0 \leq t \leq \sqrt{10/16}\)</span>. To implement this function we might have:</p>
<div class="cell" data-execution_count="14">
<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">hᵣ</span>(t) <span class="op">=</span> <span class="fl">0</span> <span class="op">&lt;=</span> t <span class="op">&lt;=</span> <span class="fu">sqrt</span>(<span class="fl">10</span><span class="op">/</span><span class="fl">16</span>) ? <span class="fl">10.0</span> <span class="op">-</span> <span class="fl">16</span>t<span class="op">^</span><span class="fl">2</span> <span class="op">:</span> <span class="fu">error</span>(<span class="st">"t is not in the domain"</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>hᵣ (generic function with 1 method)</code></pre>
</div>
</div>
</section>
<section id="nesting-ternary-operators" class="level4">
<h4 class="anchored" data-anchor-id="nesting-ternary-operators">Nesting ternary operators</h4>
<p>The function <code>s(x)</code> isnt quite so easy to implement, as there isnt an “otherwise” case. We could use an <code>if</code> statement, but instead illustrate using a second, nested ternary operator:</p>
<div class="cell" data-execution_count="15">
<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">s</span>(x) <span class="op">=</span> x <span class="op">&lt;</span> <span class="fl">0</span> ? <span class="fl">1</span> <span class="op">:</span></span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a> x <span class="op">&gt;</span> <span class="fl">0</span> ? <span class="fl">1</span> <span class="op">:</span> <span class="fu">error</span>(<span class="st">"0 is not in the domain"</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>s (generic function with 1 method)</code></pre>
</div>
</div>
<p>With nested ternary operators, the advantage over the <code>if</code> condition is not always compelling, but for simple cases the ternary operator is quite useful.</p>
</section>
</section>
</section>
<section id="functions-defined-with-the-function-keyword" class="level2" data-number="7.2">
<h2 data-number="7.2" class="anchored" data-anchor-id="functions-defined-with-the-function-keyword"><span class="header-section-number">7.2</span> Functions defined with the “function” keyword</h2>
<p>For more complicated functions, say one with a few steps to compute, an alternate form for defining a function can be used:</p>
<pre class="{verbatim}"><code>function function_name(function_arguments)
...function_body...
end</code></pre>
<p>The last value computed is returned unless the <code>function_body</code> contains an explicit <code>return</code> statement.</p>
<p>For example, the following is a more verbose way to define <span class="math inline">\(sq(x) = x^2\)</span>:</p>
<div class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">sq</span>(x)</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> x<span class="op">^</span><span class="fl">2</span></span>
<span id="cb25-3"><a href="#cb25-3" 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="15">
<pre><code>sq (generic function with 1 method)</code></pre>
</div>
</div>
<p>The line <code>return x^2</code>, could have just been <code>x^2</code> as it is the last (and) only line evaluated.</p>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The <code>return</code> keyword is not a function, so is not called with parentheses. An emtpy <code>return</code> statement will return a value of <code>nothing</code>.</p>
</div>
</div>
<section id="example-1" class="level5">
<h5 class="anchored" data-anchor-id="example-1">Example</h5>
<p>Imagine we have the following complicated function related to the trajectory of a <a href="http://www.researchgate.net/publication/230963032_On_the_trajectories_of_projectiles_depicted_in_early_ballistic_woodcuts">projectile</a> with wind resistance:</p>
<p><span class="math display">\[
f(x) = \left(\frac{g}{k v_0\cos(\theta)} + \tan(\theta) \right) x + \frac{g}{k^2}\ln\left(1 - \frac{k}{v_0\cos(\theta)} x \right)
\]</span></p>
<p>Here <span class="math inline">\(g\)</span> is the gravitational constant <span class="math inline">\(9.8\)</span> and <span class="math inline">\(v_0\)</span>, <span class="math inline">\(\theta\)</span> and <span class="math inline">\(k\)</span> parameters, which we take to be <span class="math inline">\(200\)</span>, <span class="math inline">\(45\)</span> degrees and <span class="math inline">\(1/2\)</span> respectively. With these values, the above function can be computed when <span class="math inline">\(x=100\)</span> with:</p>
<div class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">trajectory</span>(x)</span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a> g, v0, theta, k <span class="op">=</span> <span class="fl">9.8</span>, <span class="fl">200</span>, <span class="fl">45</span><span class="op">*</span><span class="cn">pi</span><span class="op">/</span><span class="fl">180</span>, <span class="fl">1</span><span class="op">/</span><span class="fl">2</span></span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a> a <span class="op">=</span> v0 <span class="op">*</span> <span class="fu">cos</span>(theta)</span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a> (g<span class="op">/</span>(k<span class="op">*</span>a) <span class="op">+</span> <span class="fu">tan</span>(theta))<span class="op">*</span> x <span class="op">+</span> (g<span class="op">/</span>k<span class="op">^</span><span class="fl">2</span>) <span class="op">*</span> <span class="fu">log</span>(<span class="fl">1</span> <span class="op">-</span> k<span class="op">/</span>a<span class="op">*</span>x)</span>
<span id="cb27-6"><a href="#cb27-6" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="16">
<pre><code>trajectory (generic function with 1 method)</code></pre>
</div>
</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="fu">trajectory</span>(<span class="fl">100</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="17">
<pre><code>96.7577179163216</code></pre>
</div>
</div>
<p>By using a multi-line function our work is much easier to look over for errors.</p>
</section>
<section id="example-the-secant-method-for-finding-a-solution-to-fx-0." class="level5">
<h5 class="anchored" data-anchor-id="example-the-secant-method-for-finding-a-solution-to-fx-0.">Example: the secant method for finding a solution to <span class="math inline">\(f(x) = 0\)</span>.</h5>
<p>This next example, shows how using functions to collect a set of computations for simpler reuse can be very helpful.</p>
<p>An old method for finding a zero of an equation is the <a href="https://en.wikipedia.org/wiki/Secant_method">secant method</a>. We illustrate the method with the function <span class="math inline">\(f(x) = x^2 - 2\)</span>. In an upcoming example we saw how to create a function to evaluate the secant line between <span class="math inline">\((a,f(a))\)</span> and <span class="math inline">\((b, f(b))\)</span> at any point. In this example, we define a function to compute the <span class="math inline">\(x\)</span> coordinate of where the secant line crosses the <span class="math inline">\(x\)</span> axis. This can be defined as follows:</p>
<div class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="kw">function</span> <span class="fu">secant_intersection</span>(f, a, b)</span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># solve 0 = f(b) + m * (x-b) where m is the slope of the secant line</span></span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># x = b - f(b) / m</span></span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a> m <span class="op">=</span> (<span class="fu">f</span>(b) <span class="op">-</span> <span class="fu">f</span>(a)) <span class="op">/</span> (b <span class="op">-</span> a)</span>
<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a> b <span class="op">-</span> <span class="fu">f</span>(b) <span class="op">/</span> m</span>
<span id="cb31-6"><a href="#cb31-6" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="18">
<pre><code>secant_intersection (generic function with 1 method)</code></pre>
</div>
</div>
<p>We utilize this as follows. Suppose we wish to solve <span class="math inline">\(f(x) = 0\)</span> and we have two “rough” guesses for the answer. In our example, we wish to solve <span class="math inline">\(q(x) = x^2 - 2\)</span> and our “rough” guesses are <span class="math inline">\(1\)</span> and <span class="math inline">\(2\)</span>. Call these values <span class="math inline">\(a\)</span> and <span class="math inline">\(b\)</span>. We <em>improve</em> our rough guesses by finding a value <span class="math inline">\(c\)</span> which is the intersection point of the secant line.</p>
<div class="cell" data-execution_count="20">
<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><span class="fu">q</span>(x) <span class="op">=</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> <span class="fl">2</span></span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a>𝒂, 𝒃 <span class="op">=</span> <span class="fl">1</span>, <span class="fl">2</span></span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a>𝒄 <span class="op">=</span> <span class="fu">secant_intersection</span>(q, 𝒂, 𝒃)</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>1.3333333333333335</code></pre>
</div>
</div>
<p>In our example, we see that in trying to find an answer to <span class="math inline">\(f(x) = 0\)</span> ( <span class="math inline">\(\sqrt{2}\approx 1.414\dots\)</span>) our value found from the intersection point is a better guess than either <span class="math inline">\(a=1\)</span> or <span class="math inline">\(b=2\)</span>:</p>
<div class="cell" data-execution_count="21">
<div class="cell-output cell-output-display" data-execution_count="20">
<p><img src="functions_files/figure-html/cell-22-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>Still, <code>q(𝒄)</code> is not really close to <span class="math inline">\(0\)</span>:</p>
<div class="cell" data-execution_count="22">
<div class="sourceCode cell-code" id="cb35"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="fu">q</span>(𝒄)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="21">
<pre><code>-0.22222222222222188</code></pre>
</div>
</div>
<p><em>But</em> it is much closer than either <span class="math inline">\(q(a)\)</span> or <span class="math inline">\(q(b)\)</span>, so it is an improvement. This suggests renaming <span class="math inline">\(a\)</span> and <span class="math inline">\(b\)</span> with the old <span class="math inline">\(b\)</span> and <span class="math inline">\(c\)</span> values and trying again we might do better still:</p>
<div class="cell" data-hold="true" data-execution_count="23">
<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="op">=</span> 𝒃, 𝒄</span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a>𝒄 <span class="op">=</span> <span class="fu">secant_intersection</span>(q, 𝒂, 𝒃)</span>
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a><span class="fu">q</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.03999999999999959</code></pre>
</div>
</div>
<p>Yes, now the function value at this new <span class="math inline">\(c\)</span> is even closer to <span class="math inline">\(0\)</span>. Trying a few more times we see we just get closer and closer. He we start again to see the progress</p>
<div class="cell" data-hold="true" data-execution_count="24">
<div class="sourceCode cell-code" id="cb39"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a>𝒂,𝒃 <span class="op">=</span> <span class="fl">1</span>, <span class="fl">2</span></span>
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> step <span class="kw">in</span> <span class="fl">1</span><span class="op">:</span><span class="fl">6</span></span>
<span id="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></a> 𝒂, 𝒃 <span class="op">=</span> 𝒃, <span class="fu">secant_intersection</span>(q, 𝒂, 𝒃)</span>
<span id="cb39-4"><a href="#cb39-4" aria-hidden="true" tabindex="-1"></a> current <span class="op">=</span> (c<span class="op">=</span>𝒃, qc<span class="op">=</span><span class="fu">q</span>(𝒃))</span>
<span id="cb39-5"><a href="#cb39-5" aria-hidden="true" tabindex="-1"></a> <span class="pp">@show</span> current</span>
<span id="cb39-6"><a href="#cb39-6" 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>current = (c = 1.3333333333333335, qc = -0.22222222222222188)
current = (c = 1.4000000000000001, qc = -0.03999999999999959)
current = (c = 1.4146341463414633, qc = 0.0011897679952408424)
current = (c = 1.41421143847487, qc = -6.007286838860537e-6)
current = (c = 1.4142135620573204, qc = -8.931455575122982e-10)
current = (c = 1.4142135623730954, qc = 8.881784197001252e-16)</code></pre>
</div>
</div>
<p>Now our guess <span class="math inline">\(c\)</span> is basically the same as <code>sqrt(2)</code>. Repeating the above leads to only a slight improvement in the guess, as we are about as close as floating point values will allow.</p>
<p>Here we see a visualization with all these points. As can be seen, it quickly converges at the scale of the visualization, as we cant see much closer than <code>1e-2</code>.</p>
<div class="cell" data-hold="true" data-execution_count="25">
<div class="cell-output cell-output-display" data-execution_count="24">
<p><img src="functions_files/figure-html/cell-26-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>In most cases, this method can fairly quickly find a zero provided two good starting points are used.</p>
</section>
</section>
<section id="parameters-function-context-scope-keyword-arguments" class="level2" data-number="7.3">
<h2 data-number="7.3" class="anchored" data-anchor-id="parameters-function-context-scope-keyword-arguments"><span class="header-section-number">7.3</span> Parameters, function context (scope), keyword arguments</h2>
<p>Consider two functions implementing the slope-intercept form and point-slope form of a line:</p>
<p><span class="math display">\[
f(x) = m \cdot x + b, \quad g(x) = y_0 + m \cdot (x - x_0).
\]</span></p>
<p>Both functions use the variable <span class="math inline">\(x\)</span>, but there is no confusion, as we learn that this is just a dummy variable to be substituted for and so could have any name. Both also share a variable <span class="math inline">\(m\)</span> for a slope. Where does that value come from? In practice, there is a context that gives an answer. Despite the same name, there is no expectation that the slope will be the same for each function if the context is different. So when parameters are involved, a function involves a rule and a context to give specific values to the parameters. Euler had said initially that functions composed of “the variable quantity and numbers or constant quantities.” The term “variable,” we still use, but instead of “constant quantities,” we use the name “parameters.”</p>
<p>Something similar is also true with <code>Julia</code>. Consider the example of writing a function to model a linear equation with slope <span class="math inline">\(m=2\)</span> and <span class="math inline">\(y\)</span>-intercept <span class="math inline">\(3\)</span>. A typical means to do this would be to define constants, and then use the familiar formula:</p>
<div class="cell" data-execution_count="26">
<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>m, b <span class="op">=</span> <span class="fl">2</span>, <span class="fl">3</span></span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a><span class="fu">mxb</span>(x) <span class="op">=</span> m<span class="op">*</span>x <span class="op">+</span> b</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="25">
<pre><code>mxb (generic function with 1 method)</code></pre>
</div>
</div>
<p>This will work as expected. For example, <span class="math inline">\(f(0)\)</span> will be <span class="math inline">\(b\)</span> and <span class="math inline">\(f(2)\)</span> will be <span class="math inline">\(7\)</span>:</p>
<div class="cell" data-execution_count="27">
<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><span class="fu">mxb</span>(<span class="fl">0</span>), <span class="fu">mxb</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="26">
<pre><code>(3, 7)</code></pre>
</div>
</div>
<p>All fine, but what if somewhere later the values for <span class="math inline">\(m\)</span> and <span class="math inline">\(b\)</span> were <em>redefined</em>, say with <span class="math inline">\(m,b = 3,2\)</span>?</p>
<p>Now what happens with <span class="math inline">\(f(0)\)</span>? When <span class="math inline">\(f\)</span> was defined <code>b</code> was <span class="math inline">\(3\)</span>, but now if we were to call <code>f</code>, <code>b</code> is <span class="math inline">\(2\)</span>. Which value will we get? More generally, when <code>f</code> is being evaluated in what context does <code>Julia</code> look up the bindings for the variables it encounters? It could be that the values are assigned when the function is defined, or it could be that the values for the parameters are resolved when the function is called. If the latter, what context will be used?</p>
<p>Before discussing this, lets just see in this case:</p>
<div class="cell" data-hold="true" data-execution_count="28">
<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>m, b <span class="op">=</span> <span class="fl">3</span>, <span class="fl">2</span></span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a><span class="fu">mxb</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="27">
<pre><code>2</code></pre>
</div>
</div>
<p>So the <code>b</code> is found from the currently stored value. This fact can be exploited. we can write template-like functions, such as <code>f(x)=m*x+b</code> and reuse them just by updating the parameters separately.</p>
<p>How <code>Julia</code> resolves what a variable refers to is described in detail in the manual page <a href="http://julia.readthedocs.org/en/latest/manual/variables-and-scoping/">Scope of Variables</a>. In this case, the function definition finds variables in the context of where the function was defined, the main workspace. As seen, this context can be modified after the function definition and prior to the function call. It is only when <code>b</code> is needed, that the context is consulted, so the most recent binding is retrieved. Contexts (more formally known as environments) allow the user to repurpose variable names without there being name collision. For example, we typically use <code>x</code> as a function argument, and different contexts allow this <code>x</code> to refer to different values.</p>
<p>Mostly this works as expected, but at times it can be complicated to reason about. In our example, definitions of the parameters can be forgotten, or the same variable name may have been used for some other purpose. The potential issue is with the parameters, the value for <code>x</code> is straightforward, as it is passed into the function. However, we can also pass the parameters, such as <span class="math inline">\(m\)</span> and <span class="math inline">\(b\)</span>, as arguments. For parameters, we suggest using <a href="http://julia.readthedocs.org/en/latest/manual/functions/#keyword-arguments">keyword</a> arguments. These allow the specification of parameters, but also give a default value. This can make usage explicit, yet still convenient. For example, here is an alternate way of defining a line with parameters <code>m</code> and <code>b</code>:</p>
<div class="cell" data-execution_count="29">
<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><span class="fu">mxplusb</span>(x; m<span class="op">=</span><span class="fl">1</span>, b<span class="op">=</span><span class="fl">0</span>) <span class="op">=</span> m<span class="op">*</span>x <span class="op">+</span> b</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>mxplusb (generic function with 1 method)</code></pre>
</div>
</div>
<p>The right-hand side is identical to before, but the left hand side is different. Arguments defined <em>after</em> a semicolon are keyword arguments. They are specified as <code>var=value</code> (or <code>var::Type=value</code> to restrict the type) where the value is used as the default, should a value not be specified when the function is called.</p>
<p>Calling a function with keyword arguments can be identical to before:</p>
<div class="cell" data-execution_count="30">
<div class="sourceCode cell-code" id="cb49"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="fu">mxplusb</span>(<span class="fl">0</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="29">
<pre><code>0</code></pre>
</div>
</div>
<p>During this call, values for <code>m</code> and <code>b</code> are found from how the function is called, not the main workspace. In this case, nothing is specified so the defaults of <span class="math inline">\(m=1\)</span> and <span class="math inline">\(b=0\)</span> are used. Whereas, this call will use the user-specified values for <code>m</code> and <code>b</code>:</p>
<div class="cell" data-execution_count="31">
<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><span class="fu">mxplusb</span>(<span class="fl">0</span>; m<span class="op">=</span><span class="fl">3</span>, b<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="30">
<pre><code>2</code></pre>
</div>
</div>
<p>Keywords are used to mark the parameters whose values are to be changed from the default. Though one can use <em>positional arguments</em> for parameters - and there are good reasons to do so - using keyword arguments is a good practice if performance isnt paramount, as their usage is more explicit yet the defaults mean that a minimum amount of typing needs to be done.</p>
<section id="example-2" class="level5">
<h5 class="anchored" data-anchor-id="example-2">Example</h5>
<p>In the example for multi-line functions we hard coded many variables inside the body of the function. In practice it can be better to pass these in as parameters along the lines of:</p>
<div class="cell" data-hold="true" data-execution_count="32">
<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><span class="kw">function</span> <span class="fu">trajectory</span>(x; g <span class="op">=</span> <span class="fl">9.8</span>, v0 <span class="op">=</span> <span class="fl">200</span>, theta <span class="op">=</span> <span class="fl">45</span><span class="op">*</span><span class="cn">pi</span><span class="op">/</span><span class="fl">180</span>, k <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fl">2</span>)</span>
<span id="cb53-2"><a href="#cb53-2" aria-hidden="true" tabindex="-1"></a> a <span class="op">=</span> v0 <span class="op">*</span> <span class="fu">cos</span>(theta)</span>
<span id="cb53-3"><a href="#cb53-3" aria-hidden="true" tabindex="-1"></a> (g<span class="op">/</span>(k<span class="op">*</span>a) <span class="op">+</span> <span class="fu">tan</span>(theta))<span class="op">*</span> x <span class="op">+</span> (g<span class="op">/</span>k<span class="op">^</span><span class="fl">2</span>) <span class="op">*</span> <span class="fu">log</span>(<span class="fl">1</span> <span class="op">-</span> k<span class="op">/</span>a<span class="op">*</span>x)</span>
<span id="cb53-4"><a href="#cb53-4" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span>
<span id="cb53-5"><a href="#cb53-5" aria-hidden="true" tabindex="-1"></a><span class="fu">trajectory</span>(<span class="fl">100</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>96.7577179163216</code></pre>
</div>
</div>
</section>
<section id="the-fxp-style-for-parameterization" class="level3" data-number="7.3.1">
<h3 data-number="7.3.1" class="anchored" data-anchor-id="the-fxp-style-for-parameterization"><span class="header-section-number">7.3.1</span> The <code>f(x,p)</code> style for parameterization</h3>
<p>An alternative to keyword arguments is to bundle the parameters into a container and pass them as a single argument to the function. The idiom in <code>Julia</code> is to use the <em>second</em> argument for parameters, or <code>f(x, p)</code> for the function argument specifications. This style is used in the very popular <code>SciML</code> suite of packages.</p>
<p>For example, here we use a <em>named tuple</em> to pass parameters to <code>f</code>:</p>
<div class="cell" data-hold="true" data-execution_count="33">
<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><span class="kw">function</span> <span class="fu">trajectory</span>(x ,p)</span>
<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a> g,v0, theta, k <span class="op">=</span> p.g, p.v0, p.theta, p.k <span class="co"># unpack parameters</span></span>
<span id="cb55-3"><a href="#cb55-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb55-4"><a href="#cb55-4" aria-hidden="true" tabindex="-1"></a> a <span class="op">=</span> v0 <span class="op">*</span> <span class="fu">cos</span>(theta)</span>
<span id="cb55-5"><a href="#cb55-5" aria-hidden="true" tabindex="-1"></a> (g<span class="op">/</span>(k<span class="op">*</span>a) <span class="op">+</span> <span class="fu">tan</span>(theta))<span class="op">*</span> x <span class="op">+</span> (g<span class="op">/</span>k<span class="op">^</span><span class="fl">2</span>) <span class="op">*</span> <span class="fu">log</span>(<span class="fl">1</span> <span class="op">-</span> k<span class="op">/</span>a<span class="op">*</span>x)</span>
<span id="cb55-6"><a href="#cb55-6" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span>
<span id="cb55-7"><a href="#cb55-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb55-8"><a href="#cb55-8" aria-hidden="true" tabindex="-1"></a>p <span class="op">=</span> (g<span class="op">=</span><span class="fl">9.8</span>, v0<span class="op">=</span><span class="fl">200</span>, theta <span class="op">=</span> <span class="fl">45</span><span class="op">*</span><span class="cn">pi</span><span class="op">/</span><span class="fl">180</span>, k<span class="op">=</span><span class="fl">1</span><span class="op">/</span><span class="fl">2</span>)</span>
<span id="cb55-9"><a href="#cb55-9" aria-hidden="true" tabindex="-1"></a><span class="fu">trajectory</span>(<span class="fl">100</span>, p)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="32">
<pre><code>96.7577179163216</code></pre>
</div>
</div>
<p>The style isnt so different from using keyword arguments, save the extra step of unpacking the parameters. The <em>big</em> advantage is consistency the function is always called in an identical manner regardless of the number of parameters (or variables).</p>
</section>
</section>
<section id="multiple-dispatch" class="level2" data-number="7.4">
<h2 data-number="7.4" class="anchored" data-anchor-id="multiple-dispatch"><span class="header-section-number">7.4</span> Multiple dispatch</h2>
<p>The concept of a function is of much more general use than its restriction to mathematical functions of single real variable. A natural application comes from describing basic properties of geometric objects. The following function definitions likely will cause no great concern when skimmed over:</p>
<div class="cell" data-hold="true" data-execution_count="34">
<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="fu">Area</span>(w, h) <span class="op">=</span> w <span class="op">*</span> h <span class="co"># of a rectangle</span></span>
<span id="cb57-2"><a href="#cb57-2" aria-hidden="true" tabindex="-1"></a><span class="fu">Volume</span>(r, h) <span class="op">=</span> <span class="cn">pi</span> <span class="op">*</span> r<span class="op">^</span><span class="fl">2</span> <span class="op">*</span> h <span class="co"># of a cylinder</span></span>
<span id="cb57-3"><a href="#cb57-3" aria-hidden="true" tabindex="-1"></a><span class="fu">SurfaceArea</span>(r, h) <span class="op">=</span> <span class="cn">pi</span> <span class="op">*</span> r <span class="op">*</span> (r <span class="op">+</span> <span class="fu">sqrt</span>(h<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> r<span class="op">^</span><span class="fl">2</span>)) <span class="co"># of a right circular cone, including the base</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>SurfaceArea (generic function with 1 method)</code></pre>
</div>
</div>
<p>The right-hand sides may or may not be familiar, but it should be reasonable to believe that if push came to shove, the formulas could be looked up. However, the left-hand sides are subtly different - they have two arguments, not one. In <code>Julia</code> it is trivial to define functions with multiple arguments - we just did.</p>
<p>Earlier we saw the <code>log</code> function can use a second argument to express the base. This function is basically defined by <code>log(b,x)=log(x)/log(b)</code>. The <code>log(x)</code> value is the natural log, and this definition just uses the change-of-base formula for logarithms.</p>
<p>But not so fast, on the left side is a function with two arguments and on the right side the functions have one argument - yet they share the same name. How does <code>Julia</code> know which to use? <code>Julia</code> uses the number, order, and <em>type</em> of the positional arguments passed to a function to determine which function definition to use. This is technically known as <a href="http://en.wikipedia.org/wiki/Multiple_dispatch">multiple dispatch</a> or <strong>polymorphism</strong>. As a feature of the language, it can be used to greatly simplify the number of functions the user must learn. The basic idea is that many functions are “generic” in that they have methods which will work differently in different scenarios.</p>
<div class="callout-warning 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">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Multiple dispatch is very common in mathematics. For example, we learn different ways to add: integers (fingers, carrying), real numbers (align the decimal points), rational numbers (common denominators), complex numbers (add components), vectors (add components), polynomials (combine like monomials), … yet we just use the same <code>+</code> notation for each operation. The concepts are related, the details different.</p>
</div>
</div>
<p><code>Julia</code> is similarly structured. <code>Julia</code> terminology would be to call the operation “<code>+</code>” a <em>generic function</em> and the different implementations <em>methods</em> of “<code>+</code>”. This allows the user to just need to know a smaller collection of generic concepts yet still have the power of detail-specific implementations. To see how many different methods are defined in the base <code>Julia</code> language for the <code>+</code> operator, we can use the command <code>methods(+)</code>. As there are so many (<span class="math inline">\(\approx 200\)</span>) and that number is growing, we illustrate how many different logarithm methods are implemented for “numbers:”</p>
<div class="cell" data-execution_count="35">
<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="fu">methods</span>(log, (<span class="dt">Number</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="34">
# 11 methods for generic function <b>log</b>:<ul><li> log(::<b>Static.StaticFloat64{M}</b>)<i> where M</i> in Static at <a href="file:///Users/verzani/.julia/packages/Static/sVI3g/src/Static.jl" target="_blank">/Users/verzani/.julia/packages/Static/sVI3g/src/Static.jl:460</a></li> <li> log(d::<b>ForwardDiff.Dual{T}</b>)<i> where T</i> in ForwardDiff at <a href="file:///Users/verzani/.julia/packages/ForwardDiff/Z1voq/src/dual.jl" target="_blank">/Users/verzani/.julia/packages/ForwardDiff/Z1voq/src/dual.jl:238</a></li> <li> log(x::<b>Float32</b>) in Base.Math at <a href="https://github.com/JuliaLang/julia/tree/742b9abb4dd4621b667ec5bb3434b8b3602f96fd/base/special/log.jl#L266" target="_blank">special/log.jl:266</a></li> <li> log(::<b>Irrational{:}</b>) in Base.MathConstants at <a href="https://github.com/JuliaLang/julia/tree/742b9abb4dd4621b667ec5bb3434b8b3602f96fd/base/mathconstants.jl#L123" target="_blank">mathconstants.jl:123</a></li> <li> log(x::<b>SymPy.Sym</b>) in SymPy at <a href="https://github.com/JuliaPy/SymPy.jl/tree/ff52d9b0a65d1dbc652d33bd9e84b7251fc74422//src/mathfuns.jl#L43" target="_blank">/Users/verzani/julia/SymPy/src/mathfuns.jl:43</a></li> <li> log(x::<b>BigFloat</b>) in Base.MPFR at <a href="https://github.com/JuliaLang/julia/tree/742b9abb4dd4621b667ec5bb3434b8b3602f96fd/base/mpfr.jl#L678" target="_blank">mpfr.jl:678</a></li> <li> log(x::<b>Float64</b>) in Base.Math at <a href="https://github.com/JuliaLang/julia/tree/742b9abb4dd4621b667ec5bb3434b8b3602f96fd/base/special/log.jl#L269" target="_blank">special/log.jl:269</a></li> <li> log(a::<b>ComplexF16</b>) in Base.Math at <a href="https://github.com/JuliaLang/julia/tree/742b9abb4dd4621b667ec5bb3434b8b3602f96fd/base/math.jl#L1202" target="_blank">math.jl:1202</a></li> <li> log(z::<b>Complex</b>) in Base at <a href="https://github.com/JuliaLang/julia/tree/742b9abb4dd4621b667ec5bb3434b8b3602f96fd/base/complex.jl#L608" target="_blank">complex.jl:608</a></li> <li> log(a::<b>Float16</b>) in Base.Math at <a href="https://github.com/JuliaLang/julia/tree/742b9abb4dd4621b667ec5bb3434b8b3602f96fd/base/math.jl#L1201" target="_blank">math.jl:1201</a></li> <li> log(x::<b>Real</b>) in Base.Math at <a href="https://github.com/JuliaLang/julia/tree/742b9abb4dd4621b667ec5bb3434b8b3602f96fd/base/math.jl#L1218" target="_blank">math.jl:1218</a></li> </ul>
</div>
</div>
<p>(The arguments have <em>type annotations</em> such as <code>x::Float64</code> or <code>x::BigFloat</code>. <code>Julia</code> uses these to help resolve which method should be called for a given set of arguments. This allows for different operations depending on the variable type. For example, in this case, the <code>log</code> function for <code>Float64</code> values uses a fast algorithm, whereas for <code>BigFloat</code> values an algorithm that can handle multiple precision is used.)</p>
<section id="example-an-application-of-composition-and-multiple-dispatch" class="level5">
<h5 class="anchored" data-anchor-id="example-an-application-of-composition-and-multiple-dispatch">Example: An application of composition and multiple dispatch</h5>
<p>As mentioned <code>Julia</code>s multiple dispatch allows multiple functions with the same name. The function that gets selected depends not just on the type of the arguments, but also on the number of arguments given to the function. We can exploit this to simplify our tasks. For example, consider this optimization problem:</p>
<blockquote class="blockquote">
<p>For all rectangles of perimeter <span class="math inline">\(20\)</span>, what is the one with largest area?</p>
</blockquote>
<p>The start of this problem is to represent the area in terms of one variable. We see next that composition can simplify this task, which when done by hand requires a certain amount of algebra.</p>
<p>Representing the area of a rectangle in terms of two variables is easy, as the familiar formula of width times height applies:</p>
<div class="cell" data-execution_count="36">
<div class="sourceCode cell-code" id="cb60"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a><span class="fu">Area</span>(w, h) <span class="op">=</span> w <span class="op">*</span> h</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="35">
<pre><code>Area (generic function with 1 method)</code></pre>
</div>
</div>
<p>But the other fact about this problem - that the perimeter is <span class="math inline">\(20\)</span> - means that height depends on width. For this question, we can see that <span class="math inline">\(P=2w + 2h\)</span> so that - as a function - <code>height</code> depends on <code>w</code> as follows:</p>
<div class="cell" data-execution_count="37">
<div class="sourceCode cell-code" id="cb62"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a><span class="fu">height</span>(w) <span class="op">=</span> (<span class="fl">20</span> <span class="op">-</span> <span class="fl">2</span><span class="op">*</span>w)<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="36">
<pre><code>height (generic function with 1 method)</code></pre>
</div>
</div>
<p>By hand we would substitute this last expression into that for the area and simplify (to get <span class="math inline">\(A=w\cdot (20-2 \cdot w)/2 = -w^2 + 10\)</span>). However, within <code>Julia</code> we can let <em>composition</em> do the substitution and leave the algebraic simplification for <code>Julia</code> to do:</p>
<div class="cell" data-execution_count="38">
<div class="sourceCode cell-code" id="cb64"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a><span class="fu">Area</span>(w) <span class="op">=</span> <span class="fu">Area</span>(w, <span class="fu">height</span>(w))</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>Area (generic function with 2 methods)</code></pre>
</div>
</div>
<p>This might seem odd, just like with <code>log</code>, we now have two <em>different</em> but related functions named <code>Area</code>. Julia will decide which to use based on the number of arguments when the function is called. This setup allows both to be used on the same line, as above. This usage style is not so common with many computer languages, but is a feature of <code>Julia</code> which is built around the concept of <em>generic</em> functions with multiple dispatch rules to decide which rule to call.</p>
<p>For example, jumping ahead a bit, the <code>plot</code> function of <code>Plots</code> expects functions of a single numeric variable. Behind the scenes, then the function <code>A(w)</code> will be used in this graph:</p>
<div class="cell" data-execution_count="39">
<div class="sourceCode cell-code" id="cb66"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb66-1"><a href="#cb66-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(Area, <span class="fl">0</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="38">
<p><img src="functions_files/figure-html/cell-40-output-1.svg" class="img-fluid"></p>
</div>
</div>
<p>From the graph, we can see that that width for maximum area is <span class="math inline">\(w=5\)</span> and so <span class="math inline">\(h=5\)</span> as well.</p>
</section>
</section>
<section id="function-application" class="level2" data-number="7.5">
<h2 data-number="7.5" class="anchored" data-anchor-id="function-application"><span class="header-section-number">7.5</span> Function application</h2>
<p>The typical calling pattern for a function simply follows <em>mathematical</em> notation, that is <code>f(x)</code> calls the function <code>f</code> with the argument <code>x</code>. There are times especially with function composition that an alternative <em>piping</em> syntax is desirable. <code>Julia</code> provides the <em>infix</em> operation <code>|&gt;</code> for piping, defining it by <code>|&gt;(x, f) = f(x)</code>. This allows composition to work left to right, instead of right to left. For example, these two calls produce the same answer:</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="fu">exp</span>(<span class="fu">sin</span>(<span class="fu">log</span>(<span class="fl">3</span>))), <span class="fl">3</span> <span class="op">|&gt;</span> log <span class="op">|&gt;</span> sin <span class="op">|&gt;</span> exp</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="39">
<pre><code>(2.436535228064216, 2.436535228064216)</code></pre>
</div>
</div>
</section>
<section id="other-types-of-functions" class="level2" data-number="7.6">
<h2 data-number="7.6" class="anchored" data-anchor-id="other-types-of-functions"><span class="header-section-number">7.6</span> Other types of functions</h2>
<p><code>Julia</code> has both <em>generic</em> functions <em>and</em> <em>anonymous</em> functions. Generic functions participate in <em>multiple dispatch</em>, a central feature of <code>Julia</code>. Anonymous functions are very useful with higher-order programming (passing functions as arguments). These notes occasionally take advantage of anonymous functions for convenience.</p>
<section id="anonymous-functions" class="level3" data-number="7.6.1">
<h3 data-number="7.6.1" class="anchored" data-anchor-id="anonymous-functions"><span class="header-section-number">7.6.1</span> Anonymous functions</h3>
<p>Simple mathematical functions have a domain and range which are a subset of the real numbers, and generally have a concrete mathematical rule. However, the definition of a function is much more abstract. Weve seen that functions for computer languages can be more complicated too, with, for example, the possibility of multiple input values. Things can get more abstract still.</p>
<p>Take for example, the idea of the shift of a function. The following mathematical definition of a new function <span class="math inline">\(g\)</span> related to a function <span class="math inline">\(f\)</span>:</p>
<p><span class="math display">\[
g(x) = f(x-c)
\]</span></p>
<p>has an interpretation - the graph of <span class="math inline">\(g\)</span> will be the same as the graph of <span class="math inline">\(f\)</span> shifted to the right by <span class="math inline">\(c\)</span> units. That is <span class="math inline">\(g\)</span> is a transformation of <span class="math inline">\(f\)</span>. From one perspective, the act of replacing <span class="math inline">\(x\)</span> with <span class="math inline">\(x-c\)</span> transforms a function into a new function. Mathematically, when we focus on transforming functions, the word <a href="http://en.wikipedia.org/wiki/Operator_%28mathematics%29">operator</a> is sometimes used. This concept of transforming a function can be viewed as a certain type of function, in an abstract enough way. The relation would be to just pair off the functions <span class="math inline">\((f,g)\)</span> where <span class="math inline">\(g(x) = f(x-c)\)</span>.</p>
<p>With <code>Julia</code> we can represent such operations. The simplest thing would be to do something like:</p>
<div class="cell" data-hold="true" 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><span class="fu">f</span>(x) <span class="op">=</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> <span class="fl">2</span>x</span>
<span id="cb69-2"><a href="#cb69-2" aria-hidden="true" tabindex="-1"></a><span class="fu">g</span>(x) <span class="op">=</span> <span class="fu">f</span>(x <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="40">
<pre><code>g (generic function with 1 method)</code></pre>
</div>
</div>
<p>Then <span class="math inline">\(g\)</span> has the graph of <span class="math inline">\(f\)</span> shifted by 3 units to the right. Now <code>f</code> above refers to something in the main workspace, in this example a specific function. Better would be to allow <code>f</code> to be an argument of a function, like this:</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><span class="kw">function</span> <span class="fu">shift_right</span>(f; c<span class="op">=</span><span class="fl">0</span>)</span>
<span id="cb71-2"><a href="#cb71-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">function</span>(x)</span>
<span id="cb71-3"><a href="#cb71-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">f</span>(x <span class="op">-</span> c)</span>
<span id="cb71-4"><a href="#cb71-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">end</span></span>
<span id="cb71-5"><a href="#cb71-5" aria-hidden="true" tabindex="-1"></a><span class="kw">end</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="41">
<pre><code>shift_right (generic function with 1 method)</code></pre>
</div>
</div>
<p>That takes some parsing. In the body of the <code>shift_right</code> is the definition of a function. But this function has no name it is <em>anonymous</em>. But what it does should be clear - it subtracts <span class="math inline">\(c\)</span> from <span class="math inline">\(x\)</span> and evaluates <span class="math inline">\(f\)</span> at this new value. Since the last expression creates a function, this function is returned by <code>shift_right</code>.</p>
<p>So we could have done something more complicated like:</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">f</span>(x) <span class="op">=</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> <span class="fl">2</span>x</span>
<span id="cb73-2"><a href="#cb73-2" aria-hidden="true" tabindex="-1"></a>l <span class="op">=</span> <span class="fu">shift_right</span>(f, c<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="42">
<pre><code>#15 (generic function with 1 method)</code></pre>
</div>
</div>
<p>Then <code>l</code> is a function that is derived from <code>f</code>.</p>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The value of <code>c</code> used when <code>l</code> is called is the one passed to <code>shift_right</code>. Functions like <code>l</code> that are returned by other functions also are called <em>closures</em>, as the context they are evaluated within includes the context of the function that constructs them.</p>
</div>
</div>
<p>Anonymous functions can be created with the <code>function</code> keyword, but we will use the “arrow” notation, <code>arg-&gt;body</code> to create them, The above, could have been defined as:</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><span class="fu">shift_right_alt</span>(f; c<span class="op">=</span><span class="fl">0</span>) <span class="op">=</span> x <span class="op">-&gt;</span> <span class="fu">f</span>(x<span class="op">-</span>c)</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>shift_right_alt (generic function with 1 method)</code></pre>
</div>
</div>
<p>When the <code>-&gt;</code> is seen a function is being created.</p>
<div class="callout-warning 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">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Generic versus anonymous functions. Julia has two types of functions, generic ones, as defined by <code>f(x)=x^2</code> and anonymous ones, as defined by <code>x -&gt; x^2</code>. One gotcha is that <code>Julia</code> does not like to use the same variable name for the two types. In general, Julia is a dynamic language, meaning variable names can be reused with different types of variables. But generic functions take more care, as when a new method is defined it gets added to a method table. So repurposing the name of a generic function for something else is not allowed. Similarly, repurposing an already defined variable name for a generic function is not allowed. This comes up when we use functions that return functions as we have different styles that can be used: When we defined <code>l = shift_right(f, c=3)</code> the value of <code>l</code> is assigned an anonymous function. This binding can be reused to define other variables. However, we could have defined the function <code>l</code> through <code>l(x) = shift_right(f, c=3)(x)</code>, being explicit about what happens to the variable <code>x</code>. This would add a method to the generic function <code>l</code>. Meaning, we get an error if we tried to assign a variable to <code>l</code>, such as an expression like <code>l=3</code>. We generally employ the latter style, even though it involves a bit more typing, as we tend to stick to methods of generic functions for consistency.</p>
</div>
</div>
<section id="example-the-secant-line" class="level5">
<h5 class="anchored" data-anchor-id="example-the-secant-line">Example: the secant line</h5>
<p>A secant line is a line through two points on the graph of a function. If we have a function <span class="math inline">\(f(x)\)</span>, and two <span class="math inline">\(x\)</span>-values <span class="math inline">\(x=a\)</span> and <span class="math inline">\(x=b\)</span>, then we can find the slope between the points <span class="math inline">\((a,f(a))\)</span> and <span class="math inline">\((b, f(b))\)</span> with:</p>
<p><span class="math display">\[
m = \frac{f(b) - f(a)}{b - a}.
\]</span></p>
<p>The point-slope form of a line then gives the equation of the tangent line as <span class="math inline">\(y = f(a) + m \cdot (x - a)\)</span>.</p>
<p>To model this in <code>Julia</code>, we would want to turn the inputs <code>f</code>,<code>a</code>, <code>b</code> into a function that implements the secant line (functions are much easier to work with than equations). Here is how we can do it:</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="kw">function</span> <span class="fu">secant</span>(f, a, b)</span>
<span id="cb77-2"><a href="#cb77-2" aria-hidden="true" tabindex="-1"></a> m <span class="op">=</span> (<span class="fu">f</span>(b) <span class="op">-</span> <span class="fu">f</span>(a)) <span class="op">/</span> (b<span class="op">-</span>a)</span>
<span id="cb77-3"><a href="#cb77-3" aria-hidden="true" tabindex="-1"></a> x <span class="op">-&gt;</span> <span class="fu">f</span>(a) <span class="op">+</span> m <span class="op">*</span> (x <span class="op">-</span> a)</span>
<span id="cb77-4"><a href="#cb77-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="44">
<pre><code>secant (generic function with 1 method)</code></pre>
</div>
</div>
<p>The body of the function nearly mirrors the mathematical treatment. The main difference is in place of <span class="math inline">\(y = \dots\)</span> we have a <code>x -&gt; ...</code> to create an anonymous function.</p>
<p>To illustrate the use, suppose <span class="math inline">\(f(x) = x^2 - 2\)</span> and we have the secant line between <span class="math inline">\(a=1\)</span> and <span class="math inline">\(b=2\)</span>. The value at <span class="math inline">\(x=3/2\)</span> is given by:</p>
<div class="cell" data-hold="true" 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">f</span>(x) <span class="op">=</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> <span class="fl">2</span></span>
<span id="cb79-2"><a href="#cb79-2" aria-hidden="true" tabindex="-1"></a>a,b <span class="op">=</span> <span class="fl">1</span>, <span class="fl">2</span></span>
<span id="cb79-3"><a href="#cb79-3" aria-hidden="true" tabindex="-1"></a><span class="fu">secant</span>(f,a,b)(<span class="fl">3</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="45">
<pre><code>0.5</code></pre>
</div>
</div>
<p>The last line employs double parentheses. The first pair, <code>secant(f,a,b)</code>, returns a function and the second pair, <code>(3/2)</code>, are used to call the returned function.</p>
</section>
<section id="closures" class="level4">
<h4 class="anchored" data-anchor-id="closures">Closures</h4>
<p>One main use of anonymous functions is to make <a href="https://en.wikipedia.org/wiki/Closure_(computer_programming)">closures</a>. Weve touched on two concepts: functions with parameters <em>and</em> functions as arguments to other functions. The creation of a function for a given set of parameters may be needed. Anonymous functions are used to create <strong>closures</strong> which capture the values of the parameters. For a simple example, <code>mxplusb</code> parameterizes any line, but to use a function to represent a specific line, a new function can be created:</p>
<div class="cell" data-hold="true" data-execution_count="47">
<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="fu">mxplusb</span>(x; m<span class="op">=</span><span class="fl">0</span>, b<span class="op">=</span><span class="fl">0</span>) <span class="op">=</span> m<span class="op">*</span>x <span class="op">+</span> b</span>
<span id="cb81-2"><a href="#cb81-2" aria-hidden="true" tabindex="-1"></a><span class="fu">specific_line</span>(m,b) <span class="op">=</span> x <span class="op">-&gt;</span> <span class="fu">mxplusb</span>(x; m<span class="op">=</span>m, b<span class="op">=</span>b)</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>specific_line (generic function with 1 method)</code></pre>
</div>
</div>
<p>The returned object will have its parameters (<code>m</code> and <code>b</code>) fixed when used.</p>
<p>In <code>Julia</code>, the functions <code>Base.Fix1</code> and <code>Base.Fix2</code> are provided to take functions of two variables and create callable objects of just one variable, with the other argument fixed. This partial function application is provided by a some of the logical comparison operators. which can be useful with filtering, say.</p>
<p>For example, <code>&lt;(2)</code> is a funny looking way of expressing the function <code>x -&gt; x &lt; 2</code>. (Think of <code>x &lt; y</code> as <code>&lt;(x,y)</code> and then “fix” the value of <code>y</code> to be <code>2</code>.) This is useful with filtering by a predicate function, for example:</p>
<div class="cell" data-execution_count="48">
<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">filter</span>(<span class="fu">&lt;</span>(<span class="fl">2</span>), <span class="fl">0</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="47">
<pre><code>2-element Vector{Int64}:
0
1</code></pre>
</div>
</div>
<p>which picks off the values of <code>0</code> and <code>1</code> in a somewhat obscure way but less verbose than <code>filter(x -&gt; x &lt; 2, 0:4)</code>.</p>
<p>The <code>Fix2</code> function is also helpful when using the <code>f(x, p)</code> form for passing parameters to a function. The result of <code>Base.Fix2(f, p)</code> is a function with its parameters fixed that can be passed along for plotting or other uses.</p>
</section>
</section>
<section id="the-do-notation" class="level3" data-number="7.6.2">
<h3 data-number="7.6.2" class="anchored" data-anchor-id="the-do-notation"><span class="header-section-number">7.6.2</span> The <code>do</code> notation</h3>
<p>Many functions in <code>Julia</code> accept a function as the first argument. A common pattern for calling some function is <code>action(f, args...)</code> where <code>action</code> is the function that will act on another function <code>f</code> using the value(s) in <code>args...</code>. There <code>do</code> notation is syntactical sugar for creating an anonymous function which is useful when more complicated function bodies are needed.</p>
<p>Here is an artificial example to illustrate of a task we wont have cause to use in these notes, but is an important skill in some contexts. The <code>do</code> notation can be confusing to read, as it moves the function definition to the end and not the beginning, but is convenient to write and is used very often with the task of this example.</p>
<p>To save some text to a file requires a few steps: opening the file; writing to the file; closing the file. The <code>open</code> function does the first. One method has this signature <code>open(f::Function, args...; kwargs....)</code> and is documented to “Apply the function f to the result of <code>open(args...; kwargs...)</code> and close the resulting file descriptor upon completion.” Which is great, the open and close stages are handled by <code>Julia</code> and only the writing is up to the user.</p>
<p>The writing is done in the function of a body, so the <code>do</code> notation allows the creation of the function to be handled anonymously. In this context, the argument to this function will be an <code>IO</code> handle, which is typically called <code>io</code>.</p>
<p>So the pattern would be</p>
<div class="sourceCode cell-code" id="cb85"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb85-1"><a href="#cb85-1" aria-hidden="true" tabindex="-1"></a><span class="fu">open</span>(<span class="st">"somefile.txt"</span>, <span class="st">"w"</span>) <span class="cf">do</span> io</span>
<span id="cb85-2"><a href="#cb85-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">write</span>(io, <span class="st">"Four score and seven"</span>)</span>
<span id="cb85-3"><a href="#cb85-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">write</span>(io, <span class="st">"years ago..."</span>)</span>
<span id="cb85-4"><a href="#cb85-4" 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>
<p>The name of the file to open appears, how the file is to be opened (<code>w</code> means write, <code>r</code> would mean read), and then a function with argument <code>io</code> which writes two lines to <code>io</code>.</p>
</section>
</section>
<section id="questions" class="level2" data-number="7.7">
<h2 data-number="7.7" class="anchored" data-anchor-id="questions"><span class="header-section-number">7.7</span> Questions</h2>
<section id="question" class="level5">
<h5 class="anchored" data-anchor-id="question">Question</h5>
<p>State the domain and range of <span class="math inline">\(f(x) = |x + 2|\)</span>.</p>
<div class="cell" data-hold="true" data-execution_count="50">
<div class="cell-output cell-output-display" data-execution_count="48">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="16589550744177237334" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_16589550744177237334">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_16589550744177237334_1">
<input class="form-check-input" type="radio" name="radio_16589550744177237334" id="radio_16589550744177237334_1" value="1">
<span class="label-body px-1">
Domain is all real numbers, range is all real numbers
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_16589550744177237334_2">
<input class="form-check-input" type="radio" name="radio_16589550744177237334" id="radio_16589550744177237334_2" value="2">
<span class="label-body px-1">
Domain is all real numbers, range is all non-negative numbers
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_16589550744177237334_3">
<input class="form-check-input" type="radio" name="radio_16589550744177237334" id="radio_16589550744177237334_3" value="3">
<span class="label-body px-1">
Domain is all non-negative numbers, range is all non-negative numbers
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_16589550744177237334_4">
<input class="form-check-input" type="radio" name="radio_16589550744177237334" id="radio_16589550744177237334_4" value="4">
<span class="label-body px-1">
Domain is all non-negative numbers, range is all real numbers
</span>
</label>
</div>
</div>
</div>
<div id="16589550744177237334_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_16589550744177237334"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('16589550744177237334_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_16589550744177237334")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_16589550744177237334")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-1" class="level5">
<h5 class="anchored" data-anchor-id="question-1">Question</h5>
<p>State the domain and range of <span class="math inline">\(f(x) = 1/(x-2)\)</span>.</p>
<div class="cell" data-hold="true" data-execution_count="51">
<div class="cell-output cell-output-display" data-execution_count="49">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="9110885088932349371" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_9110885088932349371">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_9110885088932349371_1">
<input class="form-check-input" type="radio" name="radio_9110885088932349371" id="radio_9110885088932349371_1" value="1">
<span class="label-body px-1">
Domain is all real numbers, range is all real numbers
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_9110885088932349371_2">
<input class="form-check-input" type="radio" name="radio_9110885088932349371" id="radio_9110885088932349371_2" value="2">
<span class="label-body px-1">
Domain is all real numbers except \(2\), range is all real numbers except \(0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_9110885088932349371_3">
<input class="form-check-input" type="radio" name="radio_9110885088932349371" id="radio_9110885088932349371_3" value="3">
<span class="label-body px-1">
Domain is all non-negative numbers except \(-2\), range is all non-negative numbers except \(0\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_9110885088932349371_4">
<input class="form-check-input" type="radio" name="radio_9110885088932349371" id="radio_9110885088932349371_4" value="4">
<span class="label-body px-1">
Domain is all non-negative numbers except \(0\), range is all real numbers except \(2\)
</span>
</label>
</div>
</div>
</div>
<div id="9110885088932349371_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_9110885088932349371"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('9110885088932349371_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_9110885088932349371")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_9110885088932349371")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-2" class="level5">
<h5 class="anchored" data-anchor-id="question-2">Question</h5>
<p>Which of these functions has a domain of all real <span class="math inline">\(x\)</span>, but a range of <span class="math inline">\(x &gt; 0\)</span>?</p>
<div class="cell" data-hold="true" data-execution_count="52">
<div class="cell-output cell-output-display" data-execution_count="50">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="6824808404101322557" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_6824808404101322557">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_6824808404101322557_1">
<input class="form-check-input" type="radio" name="radio_6824808404101322557" id="radio_6824808404101322557_1" value="1">
<span class="label-body px-1">
\(f(x) = \sqrt{x}\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_6824808404101322557_2">
<input class="form-check-input" type="radio" name="radio_6824808404101322557" id="radio_6824808404101322557_2" value="2">
<span class="label-body px-1">
\(f(x) = 1/x^2\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_6824808404101322557_3">
<input class="form-check-input" type="radio" name="radio_6824808404101322557" id="radio_6824808404101322557_3" value="3">
<span class="label-body px-1">
\(f(x) = 2^x\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_6824808404101322557_4">
<input class="form-check-input" type="radio" name="radio_6824808404101322557" id="radio_6824808404101322557_4" value="4">
<span class="label-body px-1">
\(f(x) = |x|\)
</span>
</label>
</div>
</div>
</div>
<div id="6824808404101322557_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_6824808404101322557"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 3;
var msgBox = document.getElementById('6824808404101322557_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_6824808404101322557")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_6824808404101322557")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<section id="question-3" class="level6">
<h6 class="anchored" data-anchor-id="question-3">Question</h6>
<p>Which of these commands will make a function for <span class="math inline">\(f(x) = \sin(x + \pi/3)\)</span>?</p>
<div class="cell" data-hold="true" data-execution_count="53">
<div class="cell-output cell-output-display" data-execution_count="51">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="12656795219860792359" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_12656795219860792359">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_12656795219860792359_1">
<input class="form-check-input" type="radio" name="radio_12656795219860792359" id="radio_12656795219860792359_1" value="1">
<span class="label-body px-1">
<code>f: x -&gt; sin(x + pi/3)</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_12656795219860792359_2">
<input class="form-check-input" type="radio" name="radio_12656795219860792359" id="radio_12656795219860792359_2" value="2">
<span class="label-body px-1">
<code>f x = sin(x + pi/3)</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_12656795219860792359_3">
<input class="form-check-input" type="radio" name="radio_12656795219860792359" id="radio_12656795219860792359_3" value="3">
<span class="label-body px-1">
<code>f = sin(x + pi/3)</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_12656795219860792359_4">
<input class="form-check-input" type="radio" name="radio_12656795219860792359" id="radio_12656795219860792359_4" value="4">
<span class="label-body px-1">
<code>f(x) = sin(x + pi/3)</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_12656795219860792359_5">
<input class="form-check-input" type="radio" name="radio_12656795219860792359" id="radio_12656795219860792359_5" value="5">
<span class="label-body px-1">
<code>function f(x) = sin(x + pi/3)</code>
</span>
</label>
</div>
</div>
</div>
<div id="12656795219860792359_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_12656795219860792359"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 4;
var msgBox = document.getElementById('12656795219860792359_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_12656795219860792359")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_12656795219860792359")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-4" class="level6">
<h6 class="anchored" data-anchor-id="question-4">Question</h6>
<p>Which of these commands will create a function for <span class="math inline">\(f(x) = (1 + x^2)^{-1}\)</span>?</p>
<div class="cell" data-hold="true" data-execution_count="54">
<div class="cell-output cell-output-display" data-execution_count="52">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="4535795334832120710" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_4535795334832120710">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_4535795334832120710_1">
<input class="form-check-input" type="radio" name="radio_4535795334832120710" id="radio_4535795334832120710_1" value="1">
<span class="label-body px-1">
<code>f[x] = (1 + x^2)^(-1)</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_4535795334832120710_2">
<input class="form-check-input" type="radio" name="radio_4535795334832120710" id="radio_4535795334832120710_2" value="2">
<span class="label-body px-1">
<code>f(x) = (1 + x^2)^(-1)</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_4535795334832120710_3">
<input class="form-check-input" type="radio" name="radio_4535795334832120710" id="radio_4535795334832120710_3" value="3">
<span class="label-body px-1">
<code>function f(x) = (1 + x^2)^(-1)</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_4535795334832120710_4">
<input class="form-check-input" type="radio" name="radio_4535795334832120710" id="radio_4535795334832120710_4" value="4">
<span class="label-body px-1">
<code>def f(x): (1 + x^2)^(-1)</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_4535795334832120710_5">
<input class="form-check-input" type="radio" name="radio_4535795334832120710" id="radio_4535795334832120710_5" value="5">
<span class="label-body px-1">
<code>f(x) := (1 + x^2)^(-1)</code>
</span>
</label>
</div>
</div>
</div>
<div id="4535795334832120710_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_4535795334832120710"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('4535795334832120710_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_4535795334832120710")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_4535795334832120710")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-5" class="level6">
<h6 class="anchored" data-anchor-id="question-5">Question</h6>
<p>Will the following <code>Julia</code> commands create a function for</p>
<p><span class="math display">\[
f(x) = \begin{cases}
30 &amp; x &lt; 500\\
30 + 0.10 \cdot (x-500) &amp; \text{otherwise.}
\end{cases}
\]</span></p>
<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="fu">phone_plan</span>(x) <span class="op">=</span> x <span class="op">&lt;</span> <span class="fl">500</span> ? <span class="fl">30.0</span> <span class="op">:</span> <span class="fl">30</span> <span class="op">+</span> <span class="fl">0.10</span> <span class="op">*</span> (x<span class="op">-</span><span class="fl">500</span>);</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell" data-hold="true" data-execution_count="56">
<div class="cell-output cell-output-display" data-execution_count="54">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="17719860021514437618" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_17719860021514437618">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_17719860021514437618_1">
<input class="form-check-input" type="radio" name="radio_17719860021514437618" id="radio_17719860021514437618_1" value="1">
<span class="label-body px-1">
Yes
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_17719860021514437618_2">
<input class="form-check-input" type="radio" name="radio_17719860021514437618" id="radio_17719860021514437618_2" value="2">
<span class="label-body px-1">
No
</span>
</label>
</div>
</div>
</div>
<div id="17719860021514437618_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_17719860021514437618"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 1;
var msgBox = document.getElementById('17719860021514437618_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_17719860021514437618")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_17719860021514437618")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-6" class="level6">
<h6 class="anchored" data-anchor-id="question-6">Question</h6>
<p>The expression <code>max(0, x)</code> will be <code>0</code> if <code>x</code> is negative, but otherwise will take the value of <code>x</code>. Is this the same?</p>
<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">a_max</span>(x) <span class="op">=</span> x <span class="op">&lt;</span> <span class="fl">0</span> ? x <span class="op">:</span> <span class="fl">0.0</span>;</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell" data-hold="true" data-execution_count="58">
<div class="cell-output cell-output-display" data-execution_count="56">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="16209034506866409559" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_16209034506866409559">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_16209034506866409559_1">
<input class="form-check-input" type="radio" name="radio_16209034506866409559" id="radio_16209034506866409559_1" value="1">
<span class="label-body px-1">
Yes
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_16209034506866409559_2">
<input class="form-check-input" type="radio" name="radio_16209034506866409559" id="radio_16209034506866409559_2" value="2">
<span class="label-body px-1">
No
</span>
</label>
</div>
</div>
</div>
<div id="16209034506866409559_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_16209034506866409559"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('16209034506866409559_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_16209034506866409559")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_16209034506866409559")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-7" class="level6">
<h6 class="anchored" data-anchor-id="question-7">Question</h6>
<p>In statistics, the normal distribution has two parameters <span class="math inline">\(\mu\)</span> and <span class="math inline">\(\sigma\)</span> appearing as:</p>
<p><span class="math display">\[
f(x; \mu, \sigma) = \frac{1}{\sqrt{2\pi\sigma}} e^{-\frac{1}{2}\frac{(x-\mu)^2}{\sigma}}.
\]</span></p>
<p>Does this function implement this with the default values of <span class="math inline">\(\mu=0\)</span> and <span class="math inline">\(\sigma=1\)</span>?</p>
<div class="cell" data-execution_count="59">
<div class="sourceCode cell-code" id="cb88"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb88-1"><a href="#cb88-1" aria-hidden="true" tabindex="-1"></a><span class="fu">a_normal</span>(x; mu<span class="op">=</span><span class="fl">0</span>, sigma<span class="op">=</span><span class="fl">1</span>) <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fu">sqrt</span>(<span class="fl">2</span>pi<span class="op">*</span>sigma) <span class="op">*</span> <span class="fu">exp</span>(<span class="fu">-</span>(<span class="fl">1</span><span class="op">/</span><span class="fl">2</span>)<span class="fu">*</span>(x<span class="op">-</span>mu)<span class="op">^</span><span class="fl">2</span><span class="op">/</span>sigma)</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>a_normal (generic function with 1 method)</code></pre>
</div>
</div>
<div class="cell" data-hold="true" data-execution_count="60">
<div class="cell-output cell-output-display" data-execution_count="58">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="14845651241173061627" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_14845651241173061627">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_14845651241173061627_1">
<input class="form-check-input" type="radio" name="radio_14845651241173061627" id="radio_14845651241173061627_1" value="1">
<span class="label-body px-1">
Yes
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_14845651241173061627_2">
<input class="form-check-input" type="radio" name="radio_14845651241173061627" id="radio_14845651241173061627_2" value="2">
<span class="label-body px-1">
No
</span>
</label>
</div>
</div>
</div>
<div id="14845651241173061627_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_14845651241173061627"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 1;
var msgBox = document.getElementById('14845651241173061627_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_14845651241173061627")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_14845651241173061627")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>What value of <span class="math inline">\(\mu\)</span> is used if the function is called as <code>f(x, sigma=2.7)</code>?</p>
<div class="cell" data-hold="true" data-execution_count="61">
<div class="cell-output cell-output-display" data-execution_count="59">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="6213269073487099842" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_6213269073487099842">
<div style="padding-top: 5px">
<br>
<div class="input-group">
<input id="6213269073487099842" type="number" class="form-control" placeholder="Numeric answer">
</div>
</div>
</div>
<div id="6213269073487099842_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.getElementById("6213269073487099842").addEventListener("change", function() {
var correct = (Math.abs(this.value - 0) <= 0);
var msgBox = document.getElementById('6213269073487099842_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_6213269073487099842")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_6213269073487099842")
if (explanation != null) {
explanation.style.display = "block";
}
}
});
</script>
</div>
</div>
<p>What value of <span class="math inline">\(\mu\)</span> is used if the function is called as <code>f(x, mu=70)</code>?</p>
<div class="cell" data-hold="true" data-execution_count="62">
<div class="cell-output cell-output-display" data-execution_count="60">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="4237430369337414381" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_4237430369337414381">
<div style="padding-top: 5px">
<br>
<div class="input-group">
<input id="4237430369337414381" type="number" class="form-control" placeholder="Numeric answer">
</div>
</div>
</div>
<div id="4237430369337414381_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.getElementById("4237430369337414381").addEventListener("change", function() {
var correct = (Math.abs(this.value - 70) <= 0);
var msgBox = document.getElementById('4237430369337414381_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_4237430369337414381")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_4237430369337414381")
if (explanation != null) {
explanation.style.display = "block";
}
}
});
</script>
</div>
</div>
<p>What value of <span class="math inline">\(\mu\)</span> is used if the function is called as <code>f(x, mu=70, sigma=2.7)</code>?</p>
<div class="cell" data-hold="true" data-execution_count="63">
<div class="cell-output cell-output-display" data-execution_count="61">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="15259131602833898254" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_15259131602833898254">
<div style="padding-top: 5px">
<br>
<div class="input-group">
<input id="15259131602833898254" type="number" class="form-control" placeholder="Numeric answer">
</div>
</div>
</div>
<div id="15259131602833898254_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.getElementById("15259131602833898254").addEventListener("change", function() {
var correct = (Math.abs(this.value - 70) <= 0);
var msgBox = document.getElementById('15259131602833898254_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_15259131602833898254")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_15259131602833898254")
if (explanation != null) {
explanation.style.display = "block";
}
}
});
</script>
</div>
</div>
</section>
<section id="question-8" class="level6">
<h6 class="anchored" data-anchor-id="question-8">Question</h6>
<p><code>Julia</code> has keyword arguments (as just illustrated) but also positional arguments. These are matched by how the function is called. For example,</p>
<div class="cell" data-execution_count="64">
<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">A</span>(w, h) <span class="op">=</span> w <span class="op">*</span> h</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="62">
<pre><code>A (generic function with 1 method)</code></pre>
</div>
</div>
<p>when called as <code>A(10, 5)</code> will use 10 for <code>w</code> and <code>5</code> for <code>h</code>, as the order of <code>w</code> and <code>h</code> matches that of <code>10</code> and <code>5</code> in the call.</p>
<p>This is clear enough, but in fact positional arguments can have default values (then called <a href="http://julia.readthedocs.org/en/latest/manual/functions/#optional-arguments">optional</a>) arguments). For example,</p>
<div class="cell" data-execution_count="65">
<div class="sourceCode cell-code" id="cb92"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb92-1"><a href="#cb92-1" aria-hidden="true" tabindex="-1"></a><span class="fu">B</span>(w, h<span class="op">=</span><span class="fl">5</span>) <span class="op">=</span> w <span class="op">*</span> h</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-display" data-execution_count="63">
<pre><code>B (generic function with 2 methods)</code></pre>
</div>
</div>
<p>Actually creates two functions: <code>B(w,h)</code> for when the call is, say, <code>B(10,5)</code> and <code>B(w)</code> when the call is <code>B(10)</code>.</p>
<p>Suppose a function <code>C</code> is defined by</p>
<div class="cell" data-execution_count="66">
<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><span class="fu">C</span>(x, mu<span class="op">=</span><span class="fl">0</span>, sigma<span class="op">=</span><span class="fl">1</span>) <span class="op">=</span> <span class="fl">1</span><span class="op">/</span><span class="fu">sqrt</span>(<span class="fl">2</span>pi<span class="op">*</span>sigma) <span class="op">*</span> <span class="fu">exp</span>(<span class="fu">-</span>(<span class="fl">1</span><span class="op">/</span><span class="fl">2</span>)<span class="fu">*</span>(x<span class="op">-</span>mu)<span class="op">^</span><span class="fl">2</span><span class="op">/</span>sigma)</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">
<pre><code>C (generic function with 3 methods)</code></pre>
</div>
</div>
<p>This is <em>nearly</em> identical to the last question, save for a comma instead of a semicolon after the <code>x</code>.</p>
<p>What value of <code>mu</code> is used by the call <code>C(1, 70, 2.7)</code>?</p>
<div class="cell" data-hold="true" data-execution_count="67">
<div class="cell-output cell-output-display" data-execution_count="65">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="4154676016774552055" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_4154676016774552055">
<div style="padding-top: 5px">
<br>
<div class="input-group">
<input id="4154676016774552055" type="number" class="form-control" placeholder="Numeric answer">
</div>
</div>
</div>
<div id="4154676016774552055_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.getElementById("4154676016774552055").addEventListener("change", function() {
var correct = (Math.abs(this.value - 70) <= 0);
var msgBox = document.getElementById('4154676016774552055_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_4154676016774552055")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_4154676016774552055")
if (explanation != null) {
explanation.style.display = "block";
}
}
});
</script>
</div>
</div>
<p>What value of <code>mu</code> is used by the call <code>C(1, 70)</code>?</p>
<div class="cell" data-hold="true" data-execution_count="68">
<div class="cell-output cell-output-display" data-execution_count="66">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="16718476918490185105" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_16718476918490185105">
<div style="padding-top: 5px">
<br>
<div class="input-group">
<input id="16718476918490185105" type="number" class="form-control" placeholder="Numeric answer">
</div>
</div>
</div>
<div id="16718476918490185105_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.getElementById("16718476918490185105").addEventListener("change", function() {
var correct = (Math.abs(this.value - 70) <= 0);
var msgBox = document.getElementById('16718476918490185105_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_16718476918490185105")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_16718476918490185105")
if (explanation != null) {
explanation.style.display = "block";
}
}
});
</script>
</div>
</div>
<p>What value of <code>mu</code> is used by the call <code>C(1)</code>?</p>
<div class="cell" data-hold="true" data-execution_count="69">
<div class="cell-output cell-output-display" data-execution_count="67">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="12674417217987490480" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_12674417217987490480">
<div style="padding-top: 5px">
<br>
<div class="input-group">
<input id="12674417217987490480" type="number" class="form-control" placeholder="Numeric answer">
</div>
</div>
</div>
<div id="12674417217987490480_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.getElementById("12674417217987490480").addEventListener("change", function() {
var correct = (Math.abs(this.value - 0) <= 0);
var msgBox = document.getElementById('12674417217987490480_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_12674417217987490480")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_12674417217987490480")
if (explanation != null) {
explanation.style.display = "block";
}
}
});
</script>
</div>
</div>
<p>Will the call <code>C(1, mu=70)</code> use a value of <code>70</code> for <code>mu</code>?</p>
<div class="cell" data-hold="true" data-execution_count="70">
<div class="cell-output cell-output-display" data-execution_count="68">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="6946957002682382430" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_6946957002682382430">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_6946957002682382430_1">
<input class="form-check-input" type="radio" name="radio_6946957002682382430" id="radio_6946957002682382430_1" value="1">
<span class="label-body px-1">
Yes, this will work just as it does for keyword arguments
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_6946957002682382430_2">
<input class="form-check-input" type="radio" name="radio_6946957002682382430" id="radio_6946957002682382430_2" value="2">
<span class="label-body px-1">
No, there will be an error that the function does not accept keyword arguments
</span>
</label>
</div>
</div>
</div>
<div id="6946957002682382430_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_6946957002682382430"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('6946957002682382430_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_6946957002682382430")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_6946957002682382430")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-9" class="level6">
<h6 class="anchored" data-anchor-id="question-9">Question</h6>
<p>This function mirrors that of the built-in <code>clamp</code> function:</p>
<div class="cell" data-execution_count="71">
<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">klamp</span>(x, a, b) <span class="op">=</span> x <span class="op">&lt;</span> a ? a <span class="op">:</span> (x <span class="op">&gt;</span> b ? b <span class="op">:</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="69">
<pre><code>klamp (generic function with 1 method)</code></pre>
</div>
</div>
<p>Can you tell what it does?</p>
<div class="cell" data-hold="true" data-execution_count="72">
<div class="cell-output cell-output-display" data-execution_count="70">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="8568579145043460272" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_8568579145043460272">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_8568579145043460272_1">
<input class="form-check-input" type="radio" name="radio_8568579145043460272" id="radio_8568579145043460272_1" value="1">
<span class="label-body px-1">
<code>x</code> is the larger of the minimum of <code>x</code> and <code>a</code> and the value of <code>b</code>, aka <code>max(min(x,a),b)</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_8568579145043460272_2">
<input class="form-check-input" type="radio" name="radio_8568579145043460272" id="radio_8568579145043460272_2" value="2">
<span class="label-body px-1">
If <code>x</code> is in <code>[a,b]</code> it returns <code>x</code>, otherwise it returns <code>a</code> when <code>x</code> is less than <code>a</code> and <code>b</code> when <code>x</code> is greater than <code>b</code>.
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_8568579145043460272_3">
<input class="form-check-input" type="radio" name="radio_8568579145043460272" id="radio_8568579145043460272_3" value="3">
<span class="label-body px-1">
If <code>x</code> is in <code>[a,b]</code> it returns <code>x</code>, otherwise it returns <code>NaN</code>
</span>
</label>
</div>
</div>
</div>
<div id="8568579145043460272_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_8568579145043460272"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('8568579145043460272_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_8568579145043460272")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_8568579145043460272")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-10" class="level6">
<h6 class="anchored" data-anchor-id="question-10">Question</h6>
<p><code>Julia</code> has syntax for the composition of functions <span class="math inline">\(f\)</span> and <span class="math inline">\(g\)</span> using the Unicode operator <code></code> entered as <code>\circ[tab]</code>.</p>
<p>The notation to call a composition follows the math notation, where parentheses are necessary to separate the act of composition from the act of calling the function:</p>
<p><span class="math display">\[
(f \circ g)(x)
\]</span></p>
<p>For example</p>
<div class="cell" data-execution_count="73">
<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>(sin <span class="op"></span> cos)(<span class="cn">pi</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="71">
<pre><code>0.6496369390800625</code></pre>
</div>
</div>
<p>What happens if you forget the extra parentheses and were to call <code>sin ∘ cos(pi/4)</code>?</p>
<div class="cell" data-hold="true" data-execution_count="74">
<div class="cell-output cell-output-display" data-execution_count="72">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="9774197711472373225" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_9774197711472373225">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_9774197711472373225_1">
<input class="form-check-input" type="radio" name="radio_9774197711472373225" id="radio_9774197711472373225_1" value="1">
<span class="label-body px-1">
You still get \(0.649...\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_9774197711472373225_2">
<input class="form-check-input" type="radio" name="radio_9774197711472373225" id="radio_9774197711472373225_2" value="2">
<span class="label-body px-1">
You get a <code>MethodError</code>, as <code>cos(pi/4)</code> is evaluated as a number and <code></code> is not defined for functions and numbers
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_9774197711472373225_3">
<input class="form-check-input" type="radio" name="radio_9774197711472373225" id="radio_9774197711472373225_3" value="3">
<span class="label-body px-1">
You get a <code>generic</code> function, but this won't be callable. If tried, it will give an method error.
</span>
</label>
</div>
</div>
</div>
<div id="9774197711472373225_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_9774197711472373225"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('9774197711472373225_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_9774197711472373225")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_9774197711472373225")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-11" class="level6">
<h6 class="anchored" data-anchor-id="question-11">Question</h6>
<p>The <a href="http://julia.readthedocs.org/en/latest/stdlib/base/#Base.%7C%3E">pipe</a> notation <code>ex |&gt; f</code> takes the output of <code>ex</code> and uses it as the input to the function <code>f</code>. That is composition. What is the value of this expression <code>1 |&gt; sin |&gt; cos</code>?</p>
<div class="cell" data-hold="true" data-execution_count="75">
<div class="cell-output cell-output-display" data-execution_count="73">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="940000051033181720" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_940000051033181720">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_940000051033181720_1">
<input class="form-check-input" type="radio" name="radio_940000051033181720" id="radio_940000051033181720_1" value="1">
<span class="label-body px-1">
It is <code>0.6663667453928805</code>, the same as <code>cos(sin(1))</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_940000051033181720_2">
<input class="form-check-input" type="radio" name="radio_940000051033181720" id="radio_940000051033181720_2" value="2">
<span class="label-body px-1">
It is <code>0.5143952585235492</code>, the same as <code>sin(cos(1))</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_940000051033181720_3">
<input class="form-check-input" type="radio" name="radio_940000051033181720" id="radio_940000051033181720_3" value="3">
<span class="label-body px-1">
It gives an error
</span>
</label>
</div>
</div>
</div>
<div id="940000051033181720_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_940000051033181720"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 1;
var msgBox = document.getElementById('940000051033181720_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_940000051033181720")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_940000051033181720")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-12" class="level6">
<h6 class="anchored" data-anchor-id="question-12">Question</h6>
<p><code>Julia</code> has implemented this <em>limited</em> set of algebraic operations on functions: <code></code> for <em>composition</em> and <code>!</code> for <em>negation</em>. (Read <code>!</code> as “not.”) The latter is useful for “predicate” functions (ones that return either <code>true</code> or <code>false</code>. What is output by this command?</p>
<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>fn <span class="op">=</span> !iseven</span>
<span id="cb100-2"><a href="#cb100-2" aria-hidden="true" tabindex="-1"></a><span class="fu">fn</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" data-hold="true" data-execution_count="77">
<div class="cell-output cell-output-display" data-execution_count="74">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="13081717500371517683" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_13081717500371517683">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_13081717500371517683_1">
<input class="form-check-input" type="radio" name="radio_13081717500371517683" id="radio_13081717500371517683_1" value="1">
<span class="label-body px-1">
<code>false</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_13081717500371517683_2">
<input class="form-check-input" type="radio" name="radio_13081717500371517683" id="radio_13081717500371517683_2" value="2">
<span class="label-body px-1">
<code>true</code>
</span>
</label>
</div>
</div>
</div>
<div id="13081717500371517683_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_13081717500371517683"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('13081717500371517683_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_13081717500371517683")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_13081717500371517683")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-13" class="level6">
<h6 class="anchored" data-anchor-id="question-13">Question</h6>
<p>Generic functions in <code>Julia</code> allow many algorithms to work without change for different number types. For example, <a href="https://pdfs.semanticscholar.org/1ef4/ee58a159dc7e437e190ec2839fb9a654596c.pdf">3000</a> years ago, floating point numbers wouldnt have been used to carry out the secant method computations, rather rational numbers would have been. We can see the results of using rational numbers with no change to our key function, just by starting with rational numbers for <code>a</code> and <code>b</code>:</p>
<div class="cell" data-hold="true" data-execution_count="78">
<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">secant_intersection</span>(f, a, b) <span class="op">=</span> b <span class="op">-</span> <span class="fu">f</span>(b) <span class="op">*</span> (b <span class="op">-</span> a) <span class="op">/</span> (<span class="fu">f</span>(b) <span class="op">-</span> <span class="fu">f</span>(a)) <span class="co"># rewritten</span></span>
<span id="cb101-2"><a href="#cb101-2" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(x) <span class="op">=</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">-</span> <span class="fl">2</span></span>
<span id="cb101-3"><a href="#cb101-3" aria-hidden="true" tabindex="-1"></a>a, b <span class="op">=</span> <span class="fl">1</span><span class="op">//</span><span class="fl">1</span>, <span class="fl">2</span><span class="op">//</span><span class="fl">1</span></span>
<span id="cb101-4"><a href="#cb101-4" aria-hidden="true" tabindex="-1"></a>c <span class="op">=</span> <span class="fu">secant_intersection</span>(f, a, b)</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">
<pre><code>4//3</code></pre>
</div>
</div>
<p>Now <code>c</code> is <code>4//3</code> and not <code>1.333...</code>. This works as the key operations used: division, squaring, subtraction all have different implementations for rational numbers that preserve this type.</p>
<p>Repeat the secant method two more times to find a better approximation for <span class="math inline">\(\sqrt{2}\)</span>. What is the value of <code>c</code> found?</p>
<div class="cell" data-hold="true" data-execution_count="79">
<div class="cell-output cell-output-display" data-execution_count="76">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="1985568101556090445" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_1985568101556090445">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_1985568101556090445_1">
<input class="form-check-input" type="radio" name="radio_1985568101556090445" id="radio_1985568101556090445_1" value="1">
<span class="label-body px-1">
<code>4//3</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_1985568101556090445_2">
<input class="form-check-input" type="radio" name="radio_1985568101556090445" id="radio_1985568101556090445_2" value="2">
<span class="label-body px-1">
<code>7//5</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_1985568101556090445_3">
<input class="form-check-input" type="radio" name="radio_1985568101556090445" id="radio_1985568101556090445_3" value="3">
<span class="label-body px-1">
<code>58//41</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_1985568101556090445_4">
<input class="form-check-input" type="radio" name="radio_1985568101556090445" id="radio_1985568101556090445_4" value="4">
<span class="label-body px-1">
<code>816//577</code>
</span>
</label>
</div>
</div>
</div>
<div id="1985568101556090445_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_1985568101556090445"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 3;
var msgBox = document.getElementById('1985568101556090445_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_1985568101556090445")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_1985568101556090445")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>How small is the value of <span class="math inline">\(f(c)\)</span> for this value?</p>
<div class="cell" data-hold="true" data-execution_count="80">
<div class="cell-output cell-output-display" data-execution_count="77">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="3932419343644644953" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_3932419343644644953">
<div style="padding-top: 5px">
<br>
<div class="input-group">
<input id="3932419343644644953" type="number" class="form-control" placeholder="Numeric answer">
</div>
</div>
</div>
<div id="3932419343644644953_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.getElementById("3932419343644644953").addEventListener("change", function() {
var correct = (Math.abs(this.value - 0.0011897679952408424) <= 0.001);
var msgBox = document.getElementById('3932419343644644953_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_3932419343644644953")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_3932419343644644953")
if (explanation != null) {
explanation.style.display = "block";
}
}
});
</script>
</div>
</div>
<p>How close is this answer to the true value of <span class="math inline">\(\sqrt{2}\)</span>?</p>
<div class="cell" data-hold="true" data-execution_count="81">
<div class="cell-output cell-output-display" data-execution_count="78">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="329948002471761362" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_329948002471761362">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_329948002471761362_1">
<input class="form-check-input" type="radio" name="radio_329948002471761362" id="radio_329948002471761362_1" value="1">
<span class="label-body px-1">
about \(8\) parts in \(100\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_329948002471761362_2">
<input class="form-check-input" type="radio" name="radio_329948002471761362" id="radio_329948002471761362_2" value="2">
<span class="label-body px-1">
about \(1\) parts in \(100\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_329948002471761362_3">
<input class="form-check-input" type="radio" name="radio_329948002471761362" id="radio_329948002471761362_3" value="3">
<span class="label-body px-1">
about \(4\) parts in \(10,000\)
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_329948002471761362_4">
<input class="form-check-input" type="radio" name="radio_329948002471761362" id="radio_329948002471761362_4" value="4">
<span class="label-body px-1">
about \(2\) parts in \(1,000,000\)
</span>
</label>
</div>
</div>
</div>
<div id="329948002471761362_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_329948002471761362"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 3;
var msgBox = document.getElementById('329948002471761362_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_329948002471761362")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_329948002471761362")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>(Finding a good approximation to <span class="math inline">\(\sqrt{2}\)</span> would be helpful to builders, for example, as it could be used to verify the trueness of a square room, say.)</p>
</section>
<section id="question-14" class="level6">
<h6 class="anchored" data-anchor-id="question-14">Question</h6>
<p><code>Julia</code> does not have surface syntax for the <em>difference</em> of functions. This is a common thing to want when solving equations. The tools available solve <span class="math inline">\(f(x)=0\)</span>, but problems may present as solving for <span class="math inline">\(h(x) = g(x)\)</span> or even <span class="math inline">\(h(x) = c\)</span>, for some constant. Which of these solutions is <strong>not</strong> helpful if <span class="math inline">\(h\)</span> and <span class="math inline">\(g\)</span> are already defined?</p>
<div class="cell" data-hold="true" data-execution_count="82">
<div class="cell-output cell-output-display" data-execution_count="79">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="12458588860790437036" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_12458588860790437036">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_12458588860790437036_1">
<input class="form-check-input" type="radio" name="radio_12458588860790437036" id="radio_12458588860790437036_1" value="1">
<span class="label-body px-1">
Just use <code>f = h - g</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_12458588860790437036_2">
<input class="form-check-input" type="radio" name="radio_12458588860790437036" id="radio_12458588860790437036_2" value="2">
<span class="label-body px-1">
Define <code>f(x) = h(x) - g(x)</code>
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_12458588860790437036_3">
<input class="form-check-input" type="radio" name="radio_12458588860790437036" id="radio_12458588860790437036_3" value="3">
<span class="label-body px-1">
Use <code>x -&gt; h(x) - g(x)</code> when the difference is needed
</span>
</label>
</div>
</div>
</div>
<div id="12458588860790437036_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_12458588860790437036"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 1;
var msgBox = document.getElementById('12458588860790437036_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_12458588860790437036")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_12458588860790437036")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
<section id="question-15" class="level6">
<h6 class="anchored" data-anchor-id="question-15">Question</h6>
<p>Identifying the range of a function can be a difficult task. We see in this question that in some cases, a package can be of assistance.</p>
<p>A mathematical interval is a set of values of the form</p>
<ul>
<li>an open interval: <span class="math inline">\(a &lt; x &lt; b\)</span>, or <span class="math inline">\((a,b)\)</span>;</li>
<li>a closed interval: <span class="math inline">\(a \leq x \leq b\)</span>, or <span class="math inline">\([a,b]\)</span>;</li>
<li>or a half-open interval: <span class="math inline">\(a &lt; x \leq b\)</span> or <span class="math inline">\(a \leq x &lt; b\)</span>, repectively <span class="math inline">\((a,b]\)</span> or <span class="math inline">\([a,b)\)</span>.</li>
</ul>
<p>They all contain all real numbers between the endpoints, the distinction is whether the endpoints are included or not.</p>
<p>A domain is some set, but typically that set is an interval such as <em>all real numbers</em> (<span class="math inline">\((-\infty,\infty)\)</span>), <em>all non-negative numbers</em> (<span class="math inline">\([0,\infty)\)</span>), or, say, <em>all positive numbers</em> (<span class="math inline">\((0,\infty)\)</span>).</p>
<p>The <code>IntervalArithmetic</code> package provides an easy means to define closed intervals using the symbol <code>..</code>, but this is also used by the already loaded <code>CalculusWithJulia</code> package in different manner, so we use the fully qualified named constructor in the following to construct intervals:</p>
<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><span class="im">import</span> <span class="bu">IntervalArithmetic</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="84">
<div class="sourceCode cell-code" id="cb104"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb104-1"><a href="#cb104-1" aria-hidden="true" tabindex="-1"></a>I1 <span class="op">=</span> IntervalArithmetic.<span class="fu">Interval</span>(<span class="op">-</span><span class="cn">Inf</span>, <span class="cn">Inf</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>[-∞, ∞]</code></pre>
</div>
</div>
<div class="cell" data-execution_count="85">
<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>I2 <span class="op">=</span> IntervalArithmetic.<span class="fu">Interval</span>(<span class="fl">0</span>, <span class="cn">Inf</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>[0, ∞]</code></pre>
</div>
</div>
<p>The main feature of the package is not to construct intervals, but rather to <em>rigorously</em> bound with an interval the output of the image of a closed interval under a function. That is, for a function <span class="math inline">\(f\)</span> and <em>closed</em> interval <span class="math inline">\([a,b]\)</span>, a bound for the set <span class="math inline">\(\{f(x) \text{ for } x \text{ in } [a,b]\}\)</span>. When <code>[a,b]</code> is the domain of <span class="math inline">\(f\)</span>, then this is a bound for the range of <span class="math inline">\(f\)</span>.</p>
<p>For example the function <span class="math inline">\(f(x) = x^2 + 2\)</span> had a domain of all real <span class="math inline">\(x\)</span>, the range can be found with:</p>
<div class="cell" data-execution_count="86">
<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>ab <span class="op">=</span> IntervalArithmetic.<span class="fu">Interval</span>(<span class="op">-</span><span class="cn">Inf</span>, <span class="cn">Inf</span>)</span>
<span id="cb108-2"><a href="#cb108-2" aria-hidden="true" tabindex="-1"></a><span class="fu">u</span>(x) <span class="op">=</span> x<span class="op">^</span><span class="fl">2</span> <span class="op">+</span> <span class="fl">2</span></span>
<span id="cb108-3"><a href="#cb108-3" aria-hidden="true" tabindex="-1"></a><span class="fu">u</span>(ab)</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>[2, ∞]</code></pre>
</div>
</div>
<p>For this problem, the actual range can easily be identified. Does the bound computed match exactly?</p>
<div class="cell" data-hold="true" data-execution_count="87">
<div class="cell-output cell-output-display" data-execution_count="84">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="13737765055052446379" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_13737765055052446379">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_13737765055052446379_1">
<input class="form-check-input" type="radio" name="radio_13737765055052446379" id="radio_13737765055052446379_1" value="1">
<span class="label-body px-1">
Yes
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_13737765055052446379_2">
<input class="form-check-input" type="radio" name="radio_13737765055052446379" id="radio_13737765055052446379_2" value="2">
<span class="label-body px-1">
No
</span>
</label>
</div>
</div>
</div>
<div id="13737765055052446379_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_13737765055052446379"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 1;
var msgBox = document.getElementById('13737765055052446379_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_13737765055052446379")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_13737765055052446379")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>Does <code>sin(0..pi)</code> <strong>exactly</strong> match the interval of <span class="math inline">\([-1,1]\)</span>?</p>
<div class="cell" data-hold="true" data-execution_count="88">
<div class="cell-output cell-output-display" data-execution_count="85">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="7675591842803118913" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_7675591842803118913">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_7675591842803118913_1">
<input class="form-check-input" type="radio" name="radio_7675591842803118913" id="radio_7675591842803118913_1" value="1">
<span class="label-body px-1">
Yes
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_7675591842803118913_2">
<input class="form-check-input" type="radio" name="radio_7675591842803118913" id="radio_7675591842803118913_2" value="2">
<span class="label-body px-1">
No
</span>
</label>
</div>
</div>
</div>
<div id="7675591842803118913_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_7675591842803118913"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('7675591842803118913_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_7675591842803118913")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_7675591842803118913")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>Guess why or why not?</p>
<div class="cell" data-hold="true" data-execution_count="89">
<div class="cell-output cell-output-display" data-execution_count="86">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="3417474810259603461" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_3417474810259603461">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_3417474810259603461_1">
<input class="form-check-input" type="radio" name="radio_3417474810259603461" id="radio_3417474810259603461_1" value="1">
<span class="label-body px-1">
Well it does, because \([-1,1]\) is the range
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_3417474810259603461_2">
<input class="form-check-input" type="radio" name="radio_3417474810259603461" id="radio_3417474810259603461_2" value="2">
<span class="label-body px-1">
It does not. The bound found is a provably known bound. The small deviation is due to the possible errors in evalution of the <code>sin</code> function near the floating point approximation of <code>pi</code>,
</span>
</label>
</div>
</div>
</div>
<div id="3417474810259603461_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_3417474810259603461"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('3417474810259603461_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_3417474810259603461")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_3417474810259603461")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>Now consider the evaluation</p>
<div class="cell" data-hold="true" data-execution_count="90">
<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> x<span class="op">^</span>x</span>
<span id="cb110-2"><a href="#cb110-2" aria-hidden="true" tabindex="-1"></a>I <span class="op">=</span> IntervalArithmetic.<span class="fu">Interval</span>(<span class="fl">0</span>, <span class="cn">Inf</span>)</span>
<span id="cb110-3"><a href="#cb110-3" aria-hidden="true" tabindex="-1"></a><span class="fu">f</span>(I)</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>[0, ∞]</code></pre>
</div>
</div>
<p>Make a graph of <code>f</code>. Does the interval found above provide a nearly exact estimate of the true range (as the previous two questions have)?</p>
<div class="cell" data-hold="true" data-execution_count="91">
<div class="cell-output cell-output-display" data-execution_count="88">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="7088153130871855834" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_7088153130871855834">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_7088153130871855834_1">
<input class="form-check-input" type="radio" name="radio_7088153130871855834" id="radio_7088153130871855834_1" value="1">
<span class="label-body px-1">
Yes
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_7088153130871855834_2">
<input class="form-check-input" type="radio" name="radio_7088153130871855834" id="radio_7088153130871855834_2" value="2">
<span class="label-body px-1">
No
</span>
</label>
</div>
</div>
</div>
<div id="7088153130871855834_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_7088153130871855834"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('7088153130871855834_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_7088153130871855834")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_7088153130871855834")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
<p>Any thoughts on why?</p>
<div class="cell" data-hold="true" data-execution_count="92">
<div class="cell-output cell-output-display" data-execution_count="89">
<form class="mx-2 my-3 mw-100" name="WeaveQuestion" data-id="6365832580988751896" data-controltype="">
<div class="form-group ">
<div class="controls">
<div class="form" id="controls_6365832580988751896">
<div style="padding-top: 5px">
<div class="form-check">
<label class="form-check-label" for="radio_6365832580988751896_1">
<input class="form-check-input" type="radio" name="radio_6365832580988751896" id="radio_6365832580988751896_1" value="1">
<span class="label-body px-1">
The interval is a nearly exact estimate, as guaranteed by <code>IntervalArithmetic</code>.
</span>
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="radio_6365832580988751896_2">
<input class="form-check-input" type="radio" name="radio_6365832580988751896" id="radio_6365832580988751896_2" value="2">
<span class="label-body px-1">
The guarantee of <code>IntervalArithmetic</code> is a <em>bound</em> on the interval, not the <em>exact</em> interval. In the case where the variable <code>x</code> appears more than once, it is treated formulaically as an <em>independent</em> quantity (meaning it has it full set of values considered in each instance) which is not the actual case mathematically. This is the "dependence problem" in interval arithmetic.
</span>
</label>
</div>
</div>
</div>
<div id="6365832580988751896_message" style="padding-bottom: 15px"></div>
</div>
</div>
</form>
<script text="text/javascript">
document.querySelectorAll('input[name="radio_6365832580988751896"]').forEach(function(rb) {
rb.addEventListener("change", function() {
var correct = rb.value == 2;
var msgBox = document.getElementById('6365832580988751896_message');
if(correct) {
msgBox.innerHTML = "<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>";
var explanation = document.getElementById("explanation_6365832580988751896")
if (explanation != null) {
explanation.style.display = "none";
}
} else {
msgBox.innerHTML = "<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>";
var explanation = document.getElementById("explanation_6365832580988751896")
if (explanation != null) {
explanation.style.display = "block";
}
}
})});
</script>
</div>
</div>
</section>
</section>
</section>
</main> <!-- /main -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const clipboard = new window.ClipboardJS('.code-copy-button', {
target: function(trigger) {
return trigger.previousElementSibling;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
setTimeout(function() {
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
let href = ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const cites = ref.parentNode.getAttribute('data-cites').split(' ');
tippyHover(ref, function() {
var popup = window.document.createElement('div');
cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
var filterRegex = new RegExp('/' + window.location.host + '/');
var isInternal = (href) => {
return filterRegex.test(href) || localhostRegex.test(href);
}
// Inspect non-navigation links and adorn them if external
var links = window.document.querySelectorAll('a:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external)');
for (var i=0; i<links.length; i++) {
const link = links[i];
if (!isInternal(link.href)) {
// target, if specified
link.setAttribute("target", "_blank");
}
}
});
</script>
<nav class="page-navigation">
<div class="nav-page nav-page-previous">
<a href="../precalc/ranges.html" class="pagination-link">
<i class="bi bi-arrow-left-short"></i> <span class="nav-page-text"><span class="chapter-number">6</span>&nbsp; <span class="chapter-title">Ranges and Sets</span></span>
</a>
</div>
<div class="nav-page nav-page-next">
<a href="../precalc/plotting.html" class="pagination-link">
<span class="nav-page-text"><span class="chapter-number">8</span>&nbsp; <span class="chapter-title">The Graph of a Function</span></span> <i class="bi bi-arrow-right-short"></i>
</a>
</div>
</nav>
</div> <!-- /content -->
<footer class="footer">
<div class="nav-footer">
<div class="nav-footer-center">Copyright 2022, John Verzani</div>
</div>
</footer>
</body></html>