{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Quick background\n", "\n", "# Maximization and minimization with Julia\n", "\n", "To get started, we load our `MTH229` package for plotting and other\n", "features:" ], "id": "712f653f-6f20-4f89-aa30-05814b232df2" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "using MTH229\n", "using Plots\n", "plotly()" ], "id": "407f2fd5" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read about this material here: [Maximization and minimization with\n", "julia](http://mth229.github.io/extrema.html).\n", "\n", "*Extrema* is nothing more than a fancy word for describing either a\n", "maximum *or* a minimum. In calculus, we have **two** concepts of these:\n", "*relative* extrema and *absolute* extrema. Let’s focus for a second on\n", "*absolute* extrema which are defined through:\n", "\n", "> A value $y=f(x)$ is an *absolute maximum* over an interval $[a,b]$ if\n", "> $y \\geq f(x)$ for all $x$ in $[a,b]$. (An absolute minimum has\n", "> $y \\leq f(x)$ instead.)\n", "\n", "Of special note is that an absolute extrema involves *both* a function\n", "**and** an interval.\n", "\n", "There are two theorems which help identify extrema here. The first, due\n", "to Bolzano, says that any continuous function on a *closed* interval\n", "will *necessarily* have an absolute maximum and minimum on that\n", "interval. The second, due to Fermat, tells us where to look: these\n", "absolute maximums and minimums can only occur at the end points of the\n", "interval or critical points of the function inside the interval.\n", "\n", "Bolzano and Fermat are historic figures. For us, we can plot a function\n", "to visually identify extrema. The value of Bolzano is the knowledge that\n", "yes, plotting isn’t a waste of time, as we are *guaranteed* to see what\n", "we look for. The value of Fermat is that if we want to get *precise*\n", "numeric answers, we have a means: identify the end points and the\n", "critical points then compare the function at *just* these values.\n", "\n", "------------------------------------------------------------------------\n", "\n", "## An example\n", "\n", "Among all rectangles with perimeter $25$ find the one with maximum area.\n", "\n", "This problem leads to two equations:\n", "\n", "- a constraint based on a fixed perimeter: $25 = 2b + 2h$.\n", "- an expression for the area: $A=h \\cdot b$.\n", "\n", "We translate these into `Julia` functions. First, using the constraint,\n", "we solve for one variable and then substitute this in:" ], "id": "e7b08125-34ec-4168-b5cf-a33d5d85e3e5" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "A (generic function with 1 method)" ] } } ], "source": [ "h(b) = (25 - 2b) / 2\n", "A(b) = h(b) * b # A = h * b translates to this" ], "id": "ab4085f5" }, { "cell_type": "markdown", "metadata": {}, "source": [ "So we have area as a function of a single variable. Here `b` ranges from\n", "$0$ to no more than $12.5$, as both `b` and `h` need be non-negative. A\n", "plot shows the function to maximize:" ], "id": "914a7b55-3378-42c6-9349-65ec9e1f0afe" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/html": [ "" ] } } ], "source": [ "plot(A, 0, 12.5)" ], "id": "f432a7a6" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Not only do we see a maximum value, we also can tell more:\n", "\n", "- the maximum happens at a critical point - not an end point\n", "- there is a unique critical point on this interval $[0,12.5]$.\n", "\n", "So, we can use `fzero` to find the critical point. The graph shows it is\n", "near $6$:" ], "id": "50bb9691-b192-4d51-9f5d-bc6e4cfda344" }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "6.25" ] } } ], "source": [ "x = fzero(A', 6)" ], "id": "1dc96d94" }, { "cell_type": "markdown", "metadata": {}, "source": [ "We store the value as `x`. Is this the answer? Not quite, the question\n", "asks for the rectangle that gives the maximum area, so we should also\n", "find the height. But this is just" ], "id": "fd80973b-4b55-48ba-98f5-bff6b6cdad46" }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "6.25" ] } } ], "source": [ "h(x)" ], "id": "5c773663" }, { "cell_type": "markdown", "metadata": {}, "source": [ "In fact, for the problems encountered within, the critical point, the\n", "constraint at the critical point, or the function evaluated at the\n", "critical point are used to answer the questions:" ], "id": "e5ba8a34-be6b-499c-a88a-dc1a5c11019e" }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "output_type": "display_data", "metadata": {}, "data": { "text/plain": [ "(6.25, 6.25, 39.0625)" ] } } ], "source": [ "x, h(x), A(x)" ], "id": "1b8eef4f" }, { "cell_type": "markdown", "metadata": {}, "source": [ "Play this video to see this problem again and a related problem:\n", "\n", "
\n", "\n", "
asciicast
\n", "
\n", "\n", "(This uses the Julia REPL, not a notebook.)\n", "\n", "------------------------------------------------------------------------" ], "id": "1d657ddf-2855-4f7a-979f-9ee89e7d8c51" }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Your commands go here" ], "id": "63d50ada" } ], "nbformat": 4, "nbformat_minor": 5, "metadata": { "kernelspec": { "name": "julia-1.11", "display_name": "Julia 1.11.2", "language": "julia", "path": "/Users/jverzani/Library/Jupyter/kernels/julia-1.11" }, "language_info": { "name": "julia", "file_extension": ".jl", "mimetype": "application/julia", "version": "1.11.2" } } }