{ "cells": [ { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "

Lab 08 - Optimization

\n", " \n", "## Overview\n", "\n", "The methods learned in Chapter 4 of the text for finding extreme values have practical applications in many areas of life. In this lab, we will use SageMath to help with solving several optimization problems.\n", "\n", "### Important SageMath Commands Introduced in this Lab\n", "\n", "\\begin{array}{|l|l|l|}\n", "\\hline \n", "\\hfill \\textbf{Command} \\hfill & \\hfill \\textbf{Description} \\hfill & \\hfill \\textbf{Example} \\hfill \\\\\n", "\\hline\n", "\\textbf{reset}() & \\text{Resets all variables back to their default state} & \\textbf{reset}() \\\\\n", "\\hline\n", "\\textbf{print}(\\textit{text}) & \\text{Outputs $\\textit{text}$} & \\textbf{print}(\\text{\"Hello\"}), \\textbf{print}(1 + 1) \\\\\n", "\\hline\n", "\\end{array}\n", "\n", "### Related Course Material\n", " 4.6\n", " \n", "### General Steps for Solving Optimization Problems\n", "The following strategy for solving optimization problems is outlined on Page 264 of the text.\n", "1. Read and understand the problem. What is the unknown? What are the given quantities and conditions?\n", "2. Draw a picture. In most problems it is useful to draw a picture and identify the given and required quantities in the picture.\n", "3. Introduce variables. Asign a symbol for the quantity, let us call it $Q$, that is to be maximized or minimized. Also, select symbols for other unknown quantities. Use suggestive notation whenever possible: $A$ for area, $h$ for height, $r$ for radius, etc.\n", "4. Write an equation. Express $Q$ in terms of some of the other unknown variables. If $Q$ has been expressed as a function of more than one variable, use the given information to find relationships (in the form of equations) among these variables. Then use the equations to eliminate all but one of the variables in the expression for $Q$.\n", "5. Test the critical points and endpoints in the domain of the variable. Use the methods of Section 4.1 to find the $\\textit{absolute}$ maximum or minimum value." ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "## Example 1\n", "\n", "A farmer has 2400 ft of fencing and wants to fence off a rectangular field that borders a straight river. He does not need to fence along the river. What are the dimensions of the field that has the largest area? \n", "\n", "Optimization problems often use the same variables, so you should start every new problem with $\\textbf{reset}()$ to clear out variables defined during previous examples." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "reset()" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "We will now assign our given information about the perimeter. We will use $l$ to represent the length of the fence and $w$ to represent the width of the fence. Note: we can create multiple variables at the same time by separating the assignment with commas.\n", "\n", "Since we do not need to fence the side bordering the river, our equation for the perimeter is $2l + w$. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l, w = var('l', 'w')\n", "perimeter = 2*l + w == 2400" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "We are trying to maximize the area of the field, which is given by $l \\times w$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def A(l,w):\n", " return l*w" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "We can now use the perimeter equation to solve either of the variables in terms of the other and substitute this value into our area function to get area as a function of one variable." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "solve(perimeter, l)" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "We now set $l = -\\frac{1}{2}w + 1200$ and redefine $A$ as function which only depends on $w$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l = -1/2 * w + 1200\n", "A(l,w)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def A(w):\n", " return -1/2 *(w-2400)*w\n", "A(w)" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "Now, we find the critical points of $A(w).$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "solve(diff(A(w),w) == 0, w)" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "We need to check the area of the field at the critical point $w = 1200$ and at the endpoints. Since both $l$ and $w$ represent lengths, they both must be nonnegative. Therefore, one endpoint is $w = 0$. To find the other, we solve the inequality $l > 0$ for $w$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "solve(l > 0, w)" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "Hence, our other endpoint is $w = 2400.$ We now find the area of the field at each of these values for $w$. We can use the $\\textbf{print}(\\dots)$ command to output each line of code and not just the last." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(A(0))\n", "print(A(1200))\n", "print(A(2400))" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "Therefore, the area of the field is maximized when $w = 1200$. To find the corresponding value of $l$, we can substitute $w = 1200$ into the expression for $l$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l(w = 1200)" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "Hence, the dimensions which maximize the area of the field are a length of 600 ft and a width of 1200 ft." ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "## Example 2\n", "\n", "Find the point on the line $\\frac{x}{3} + \\frac{y}{5} = 1$ that is closest to the origin." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "## Example 3\n", "\n", "A rectangular storage container with an open top is to have a volume of 10 m$^3$. The length of its base is twice the width. Material for the base costs $\\$$10 per square meter. Material for the sides costs $\\$$6 per square meter. Find the cost of materials for the cheapest such container." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false }, "source": [ "## Example 4\n", "\n", "A man launches his boat from a point $A$ on the bank of a straight river, 3 km wide, and wants to reach point $B$, 8 km downstream on the opposite bank, as quickly as possible. He could row his boat directly across the river to point $C$ and then run to $B$, or he could row directly to $B$, or he could row to some point $D$ between $C$ and $B$ and then run to $B$. If he can row 6 km/hr and run 8 km/hr, where should he land to reach $B$ as soon as possible?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "SageMath", "language": "sagemath", "name": "sagemath" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 2 }