{ "cells": [ { "cell_type": "markdown", "metadata": { "internals": { "slide_helper": "subslide_end", "slide_type": "subslide" }, "slide_helper": "slide_end", "slideshow": { "slide_type": "slide" } }, "source": [ "# Maximization and minimization with Julia\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get started, we load our `MTH229` package for plotting and other features:\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "┌ Info: For saving to png with the Plotly backend PlotlyBase has to be installed.\n", "└ @ Plots /Users/verzani/.julia/packages/Plots/1KWPG/src/backends.jl:318\n" ] }, { "data": { "text/plain": [ "Plots.PlotlyBackend()" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using MTH229\n", "using Plots\n", "plotly()" ] }, { "cell_type": "markdown", "metadata": { "internals": { "slide_type": "subslide" }, "slide_helper": "slide_end", "slideshow": { "slide_type": "subslide" } }, "source": [ "### Quick background\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read about this material here: [Maximization and minimization with julia](http://mth229.github.io/extrema.html).\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the impatient, *extrema* is nothing more than a fancy word for describing either a maximum *or* a minimum. In calculus, we have **two** concepts of these: *relative* extrema and *absolute* extrema. Let's focus for a second on *absolute* extrema which are defined through:\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> A value $y=f(x)$ is an *absolute maximum* over an interval $[a,b]$ if $y \\geq f(x)$ for all $x$ in $[a,b]$. (An absolute minimum has $y \\leq f(x)$ instead.)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of special note is that an absolute extrema involves *both* a function **and** an interval.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are two theorems which help identify extrema here. The first, due to Bolzano, says that any continuous function on a *closed* interval will *necessarily* have an absolute maximum and minimum on that interval. The second, due to Fermat, tells us where to look: these absolute maximums and minimums can only occur at end points or critical points.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bolzano and Fermat are historic figures. For us, we can plot a function to visually see extrema. The value of Bolzano is the knowledge that yes, plotting isn't a waste of time, as we are *guaranteed* to see what we look for. The value of Fermat is that if we want to get *precise* numeric answers, we have a means: identify the end points and the critical points then compare the function at *just* these values.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n" ] }, { "cell_type": "markdown", "metadata": { "internals": { "slide_helper": "subslide_end", "slide_type": "subslide" }, "slide_helper": "slide_end", "slideshow": { "slide_type": "slide" } }, "source": [ "## An example\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Among all rectangles with perimeter 24 find the one with maximum area.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This problem leads to two equations:\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " * a constraint based on a fixed perimeter: $25 = 2b + 2h$.\n", " * an expression for the area: $A=h \\cdot b$.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We translate these into `Julia` functions. First, using the constraint, we solve for one variable and then substitute this in:\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "A (generic function with 1 method)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h(b) = (25 - 2b) / 2\n", "A(b) = h(b) * b # A = h * b translates to this" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So we have area as a function of a single variable. Here `b` ranges from $0$ to no more than 10, as both `b` and `h` need be non-negative. A plot shows the function to maximize:\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "data": [ { "colorbar": { "title": "" }, "legendgroup": "y1", "line": { "color": "rgba(0, 154, 250, 1.000)", "dash": "solid", "shape": "linear", "width": 1 }, "mode": "lines", "name": "y1", "showlegend": true, "type": "scatter", "x": [ 0, 0.04882909509894066, 0.09765819019788131, 0.5491536809408047, 1.0006491716837282, 1.2719791423699836, 1.5433091130562389, 1.772715150106948, 2.002121187157657, 2.500519700330514, 2.9599990453462817, 3.20630425470559, 3.4526094640648983, 3.7379782037251106, 4.023346943385323, 4.530551061110215, 4.9753233654022, 5.460244566553195, 5.948047787528121, 6.217712206886082, 6.487376626244043, 6.743955516846238, 7.000534407448435, 7.5340994351135055, 7.983247536668451, 8.257426998266046, 8.53160645986364, 8.789343240575363, 9.047080021287085, 9.459994330723841, 9.872908640160599, 9.9364543200803, 10 ], "xaxis": "x", "y": [ 0, 0.6079794082085769, 1.211190255360791, 6.562851246469224, 11.506815881255273, 14.281808341000515, 16.909560894760553, 19.01642037291815, 21.018025591405127, 25.00389748239042, 28.238393718377623, 29.79841621007671, 31.23710618946072, 32.752246495039884, 34.10451616546851, 36.10599534655079, 37.43769947721043, 38.43878635534125, 38.97132486138334, 39.061457498415834, 39.006152337313, 38.818507947377164, 38.499198103236026, 37.413588640741175, 36.05835297663275, 35.032736846632574, 33.85677196230851, 32.61423590654422, 31.23884335451664, 28.7584363967208, 25.93703298504968, 25.472554545961298, 25 ], "yaxis": "y", "zmax": null, "zmin": null } ], "layout": { "annotations": [], "height": 400, "legend_position": { "bgcolor": "rgba(255, 255, 255, 1.000)", "bordercolor": "rgba(0, 0, 0, 1.000)", "borderwidth": 1, "font": { "color": "rgba(0, 0, 0, 1.000)", "family": "sans-serif", "size": 11 }, "title": { "font": { "color": "rgba(0, 0, 0, 1.000)", "family": "sans-serif", "size": 15 }, "text": "" }, "tracegroupgap": 0, "traceorder": "normal", "x": 1, "xanchor": "auto", "y": 1, "yanchor": "auto" }, "margin": { "b": 20, "l": 0, "r": 0, "t": 20 }, "paper_bgcolor": "rgba(255, 255, 255, 1.000)", "plot_bgcolor": "rgba(255, 255, 255, 1.000)", "showlegend": true, "width": 600, "xaxis": { "anchor": "y", "domain": [ 0.03619130941965587, 0.9934383202099738 ], "gridcolor": "rgba(0, 0, 0, 0.100)", "gridwidth": 0.5, "linecolor": "rgba(0, 0, 0, 1.000)", "mirror": false, "range": [ -0.3, 10.3 ], "showgrid": true, "showline": true, "showticklabels": true, "tickangle": 0, "tickcolor": "rgb(0, 0, 0)", "tickfont": { "color": "rgba(0, 0, 0, 1.000)", "family": "sans-serif", "size": 11 }, "tickmode": "array", "ticks": "inside", "ticktext": [ "0.0", "2.5", "5.0", "7.5", "10.0" ], "tickvals": [ 0, 2.5, 5, 7.5, 10 ], "title": "", "titlefont": { "color": "rgba(0, 0, 0, 1.000)", "family": "sans-serif", "size": 15 }, "type": "-", "visible": true, "zeroline": false, "zerolinecolor": "rgba(0, 0, 0, 1.000)" }, "yaxis": { "anchor": "x", "domain": [ 0.03762029746281716, 0.9901574803149606 ], "gridcolor": "rgba(0, 0, 0, 0.100)", "gridwidth": 0.5, "linecolor": "rgba(0, 0, 0, 1.000)", "mirror": false, "range": [ -1.171843724952475, 40.23330122336831 ], "showgrid": true, "showline": true, "showticklabels": true, "tickangle": 0, "tickcolor": "rgb(0, 0, 0)", "tickfont": { "color": "rgba(0, 0, 0, 1.000)", "family": "sans-serif", "size": 11 }, "tickmode": "array", "ticks": "inside", "ticktext": [ "0", "10", "20", "30", "40" ], "tickvals": [ 0, 10, 20, 30, 40 ], "title": "", "titlefont": { "color": "rgba(0, 0, 0, 1.000)", "family": "sans-serif", "size": 15 }, "type": "-", "visible": true, "zeroline": false, "zerolinecolor": "rgba(0, 0, 0, 1.000)" } } }, "text/html": [ "
\n", " \n" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot(A, 0, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Not only do we see a maximum value, we also can tell more:\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " * the maximum happens at a critical point - not an end point\n", " * there is a unique critical point on this interval $[0,10]$.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So, we can use `fzero` to find the critical point:\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6.25" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = fzero(A', 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We store the value as `x`. Is this the answer? Not quite, the question asks for the rectangle that gives the maximum area, so we should also find the height. But this is just\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6.25" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In fact, for the problems encountered within, the critical point, the constraint at the critical point, or the function evaluated at the critical point are used to answer the questions:\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6.25, 6.25, 39.0625)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x, h(x), A(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Play this video to see this problem again and a related problem:\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[![asciicast](https://asciinema.org/a/4tdlumFtE2mBswLZJSw6q9K9J.svg)](https://asciinema.org/a/4tdlumFtE2mBswLZJSw6q9K9J)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(This uses the Julia REPL, not a notebook; the begin/end block just combines commands like a cell.)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.7.2", "language": "julia", "name": "julia-1.7" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }