{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"#**Lab 1**\n",
"\n",
"**Name: (your name)**\n",
"\n"
],
"metadata": {
"id": "RQDJ-AcIRSh3"
}
},
{
"cell_type": "markdown",
"source": [
"Some remarks before you start:\n",
"* You are currently working on a Google Colab notebook, a convenient environment that allows you to write texts, Python codes, and execute the codes. This notebook runs in a temporary virtual machine, which gets reset each time you close the session. You will need to import the below Python packages again each time you start a session.\n",
"* The programming language used in this lab is Python, which serves as a tool to implement algorithms rather than be a primary focus by itself. Therefore, technical details of Python that are not crucial to know for problem-solving will be skipped.\n",
"* You can double click on any cell to edit it or to learn how to type math objects (matrix, equations,...) or format text (bolden, underline,...) Once inside a cell, you can execute it by pressing **Shift+Enter**.\n",
"* Before submitting your report, remove all ouputs by clicking on **Edit🠦Clear all ouputs**. Then download your report by clicking on **File🠦Download🠦.ipynb**. This is the file you will submit on Canvas.\n",
"\n",
"In this lab, you will practice:\n",
"* perform row operations on a matrix\n",
"* solve a linear system of equations\n",
"* solve a linear programming problem\n",
"\n",
"\\begin{array}{|c| c|}\n",
" \\hline\n",
" \\text{Problems} & \\text{Points}\\\\\n",
" \\hline\\hline\n",
" 1, 4, 6, 9 & 2 \\\\\n",
" \\hline\n",
" 2, 3, 5 & 3 \\\\\n",
" \\hline\n",
" 7, 8, 10 & 4 \\\\\n",
" \\hline\n",
" \\text{Readability of your report} & 3 \\\\\n",
" \\hline\n",
" \\text{Total: 10} & \\text{Total: 32} \\\\\n",
" \\hline\n",
"\\end{array}\n",
"\n"
],
"metadata": {
"id": "zsnOeJdB7N2X"
}
},
{
"cell_type": "markdown",
"source": [
"##**I. Import necessary Python packages**\n",
"Execute the following code each time you start a session."
],
"metadata": {
"id": "IiFvVFDVZVn0"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "07yNk1raROZf"
},
"outputs": [],
"source": [
"from numpy import* # import everything from the standard Python numeric package (NumPy)\n",
"from sympy import* # import everything from the standard Python symbolic-computing package (SymPy)\n",
"from scipy import* # import everything from the standard Python scientific-computing package (SciPy)"
]
},
{
"cell_type": "markdown",
"source": [
"## **II. Matrix and row operations**"
],
"metadata": {
"id": "QMu59W8_b0n3"
}
},
{
"cell_type": "markdown",
"source": [
"A *vector* is an array of numbers. You enter an array using the command **array**. Entries are separated from each other by commas.\n",
"\n",
"Enter the vector $a=[1,2,4]$ as follows:"
],
"metadata": {
"id": "zQbd1Ur_eG6l"
}
},
{
"cell_type": "code",
"source": [
"a=array([1,2,4])\n",
"print(a)"
],
"metadata": {
"id": "4MS4DSoSgOYt"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"A *matrix* is a two-dimensional array. You can view a matrix as an array of its rows, each of which is an array itself. For example, you can view the matrix\n",
"\n",
"$$\n",
"A=\\left[\n",
" \\begin{matrix}\n",
" 1&2&-1\\\\\n",
" 3&2&0\\\\\n",
" 1&0&2\n",
" \\end{matrix}\n",
"\\right]\n",
"$$\n",
"\n",
"as an array of three entries: $[1,2,-1]$, $[3,2,0]$, $[1,0,2]$. Therefore, you can enter this matrix as follows."
],
"metadata": {
"id": "AM2vW5a3otkS"
}
},
{
"cell_type": "code",
"source": [
"A=array([[1,2,-1],[3,4,0],[1,0,2]],dtype=float64)\n",
"print(A)"
],
"metadata": {
"id": "aJ_Thk6KZy8f"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### **Exercise 1**\n",
"Each entry of an array can be accessed by its index. In Python, the starting index is 0 (not 1). Run the code and write your observation:"
],
"metadata": {
"id": "SGNzq9myf6v8"
}
},
{
"cell_type": "code",
"source": [
"print(a[0])\n",
"print(a[1])\n",
"print(a[2])\n",
"print(a[3])"
],
"metadata": {
"id": "GyH_JeOBnUlN"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### **Exercise 2**\n",
"You can multiply a vector by a number or add two vectors of the same length. Explain what each of the following commands does."
],
"metadata": {
"id": "_lMCkCncn5vX"
}
},
{
"cell_type": "code",
"source": [
"print(3*a)\n",
"b=array([3,5,2])\n",
"print(b)\n",
"print(a+b)\n",
"c=a+2*b\n",
"print(c)"
],
"metadata": {
"id": "zIXLz7ZUg3R4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### **Exercise 3**\n"
],
"metadata": {
"id": "ey7i58OMhR0w"
}
},
{
"cell_type": "markdown",
"source": [
"You can perform row operations on a matrix, such as subtracting three times of the first row from the second row and subtracting the first row from the third row:"
],
"metadata": {
"id": "llYNDpaMqXmH"
}
},
{
"cell_type": "code",
"source": [
"A[1] = A[1] - 3*A[0]\n",
"A[2] = A[2] - A[0]\n",
"print(A)"
],
"metadata": {
"id": "U3oPnAdEZ9TO"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Continue to do more row operations to turn matrix $A$ into a reduced row echelon form (RREF)."
],
"metadata": {
"id": "VY9jPG9erDPi"
}
},
{
"cell_type": "markdown",
"source": [
"### **Exercise 4**\n",
"You can use the built-in command **rref** to find the RREF of a matrix. Try the following:"
],
"metadata": {
"id": "XAGE1-Hetpya"
}
},
{
"cell_type": "code",
"source": [
"A=Matrix([[1,2,-1],[3,4,0],[1,0,2]])\n",
"A.rref()[0]"
],
"metadata": {
"id": "hMBizbOIacTA"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Note:\n",
"\n",
"* The reason why we write **A.rref()[0]** instead of just **A.rref()** is because the former command gives us exactly what we need (the RREF of matrix $A$), whereas the latter command gives some additional information that we are not interested in.\n",
"* In order to use the built-in command **rref**, you need to enter the matrix using **Matrix** (as above) instead of **array**. The reason is a little bit technical: **rref** is built in the SymPy package, not NumPy package. **Matrix** command generates an object in the SymPy package, whereas **array** command generates an object in the NumPy package."
],
"metadata": {
"id": "IV5aaVFzvNKY"
}
},
{
"cell_type": "markdown",
"source": [
"Use the built-in **rref** command to find the RREF of the following matrix:\n",
"\n",
"$$\n",
"B=\\left[\n",
" \\begin{matrix}\n",
" 2&-4&2\\\\\n",
" 3&-4&5\\\\\n",
" 0&1&1\\\\\n",
" -3&5&4\n",
" \\end{matrix}\n",
"\\right]\n",
"$$"
],
"metadata": {
"id": "N9fI8pIrBs64"
}
},
{
"cell_type": "markdown",
"source": [
"## **III. Linear system of equations**"
],
"metadata": {
"id": "wi-tre7KC8Pg"
}
},
{
"cell_type": "markdown",
"source": [
"###**Exercise 5**\n",
"Consider the following linear system of equations:\n",
"$$x+y+z=4$$\n",
"$$x+2y+4z=12$$\n",
"$$2x-3y-z=4$$\n",
"\n",
"* Write the matrix associated with this system.\n",
"* Use the command **rref** to find the RREF of this matrix.\n",
"* Solve the system based on the RREF you just found."
],
"metadata": {
"id": "B8Zk0OomDVle"
}
},
{
"cell_type": "markdown",
"source": [
"###**Exercise 6**\n",
"You can use the built-in command **linalg.solve** to solve a linear system. For example, consider the system\n",
"\n",
"$$x+2y-z=4$$\n",
"$$3x+4y=5$$\n",
"$$2x+z=3$$\n",
"\n",
"This system has a *coefficient* matrix:\n",
"\n",
"$$\n",
"A=\\left[\n",
" \\begin{matrix}\n",
" 1&2&-1\\\\\n",
" 3&4&0\\\\\n",
" 2&0&1\n",
" \\end{matrix}\n",
"\\right]\n",
"$$\n",
"\n",
"and a *right-hand-side* vector:\n",
"\n",
"$$\n",
"b=\\left[\n",
"\\begin{matrix}\n",
"4\\\\\n",
"5\\\\\n",
"3\n",
"\\end{matrix}\n",
"\\right]\n",
"$$\n",
"\n",
"You will enter maxtrix $A$, array $b$, and use the command **linalg.solve** as follows."
],
"metadata": {
"id": "hqpoWhjkGGbX"
}
},
{
"cell_type": "code",
"source": [
"A=array([[1,2,-1],[3,4,0],[2,0,1]])\n",
"b=array([4,5,3])\n",
"linalg.solve(A,b)"
],
"metadata": {
"id": "fiQ8HFhruFbU"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Note: the command **linalg.solve** can successfully solve a system only if it has a unique solution (like in the example above). If the system does not have a solution (inconsistent system) or has infinitely many solutions, **linalg.solve** will throw an error. In that case, you need to find the RREF of the associated matrix and use it to solve the system (as in the [Exercise 5](#linsys))."
],
"metadata": {
"id": "AN4yqw3OJdPD"
}
},
{
"cell_type": "markdown",
"source": [
"Solve the linear system in [Exercise 5](#linsys) using **linalg.solve**."
],
"metadata": {
"id": "dNAo0fw6MqaO"
}
},
{
"cell_type": "markdown",
"source": [
"###**Exercise 7**\n",
"Balance the following chemical equation\n",
"$$\n",
"{\\rm KMnO_4 + HCl \\to KCl + MnCl_2 + H_2O + Cl_2}\n",
"$$\n",
"In other words, find positive intergers $x_1, x_2,...,x_6$ such that\n",
"$$\n",
"x_1{\\rm KMnO_4} + x_2{\\rm HCl} = x_3{\\rm KCl} + x_4{\\rm MnCl_2} + x_5{\\rm H_2O} + x_6{\\rm Cl_2}\n",
"$$\n",
"\n",
"*Hint:* You will need to write down the system of linear equations, its associated matrix, and find its RREF. You will notice that this system has infinitely many solutions. Just choose one solution in which $x_1,x_2,...,x_6$ are all integers."
],
"metadata": {
"id": "kz_AhwIANo3R"
}
},
{
"cell_type": "markdown",
"source": [
"##**IV. Linear programming**"
],
"metadata": {
"id": "PwQqD9MUQeL6"
}
},
{
"cell_type": "markdown",
"source": [
"Consider a linear programming problem of minimizing $C=-2x+y$ subject to\n",
"\\begin{align}\n",
" x-y\\le& 3\\\\\n",
" 2x+3y\\le& 12\\\\\n",
" x+y\\ge& 2\\\\\n",
" x,y\\ge& 0\n",
"\\end{align}\n",
"\n",
"To solve this problem using the **simplex method**, you will first convert it into a standard form: maximizing $P=2x-y$ subject to\n",
"\\begin{align}\n",
" x-y\\le& 3\\\\\n",
" 2x+3y\\le& 12\\\\\n",
" -x-y\\le& -2\\\\\n",
" x,y\\ge& 0\n",
"\\end{align}\n",
"\n",
"Next, you introduce slack variables to turn all inequalities to equalities: maximizing $P=2x-y$ subject to\n",
"\\begin{align}\n",
" x-y+s_1=& 3\\\\\n",
" 2x+3y+s_2=& 12\\\\\n",
" x+y-s_3=& 2\\\\\n",
" x,y,s_1,s_2,s_3\\ge& 0\n",
"\\end{align}\n",
"Notice that the third equation has been multiplied by $(-1)$ to ensure that the right hand side is nonnegative. You rewrite the equation $P=2x-y$ as\n",
"$$-2x+y+P=0$$\n",
"making sure that the coefficient of $P$ is $1$ (not $-1$).\n",
"\n",
"Next, you construct the matrix associated with the system of the above four equations:\n",
"$$\n",
"A=\\left[\n",
" \\begin{matrix}\n",
" 1&-1&1&0&0&0&3\\\\\n",
" 2&3&0&1&0&0&12\\\\\n",
" 1&1&0&0&-1&0&2\\\\\n",
" -2&1&0&0&0&1&0\\\\\n",
" \\end{matrix}\n",
"\\right]\n",
"$$"
],
"metadata": {
"id": "3GOxNl1SlO1f"
}
},
{
"cell_type": "code",
"source": [
"A=array([[1,-1,1,0,0,0,3],[2,3,0,1,0,0,12],[1,1,0,0,-1,0,2],[-2,1,0,0,0,1,0]],dtype=float64)\n",
"print(A)"
],
"metadata": {
"id": "UWMOfAyqMhTB"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Your next goal is to use row operations to turn the matrix into the following form:\n",
""
],
"metadata": {
"id": "TH8DpROWKdx0"
}
},
{
"cell_type": "markdown",
"source": [
"The key column is the first column and the pivot element is $1$ on the third row. You then use elementary row operations to turn the key column into a column that has $1$ at the pivot element and $0$'s elsewhere."
],
"metadata": {
"id": "RfObVOp8JQOQ"
}
},
{
"cell_type": "code",
"source": [
"A[0] = A[0] - A[2]\n",
"A[1] = A[1] - 2*A[2]\n",
"A[3] = A[3] + 2*A[2]\n",
"print(A)"
],
"metadata": {
"id": "3O7KWJfctAsr"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"The key column is now the fifth column and the pivot element is $1$ on the first row. You then use elementary row operations to turn the key column into a column that has $1$ at the pivot element and $0$'s elsewhere. Note that the operation $R_2=R_2+R_4$ is not acceptable because it would change the sixth column, which you do not want to change (see the figure above)."
],
"metadata": {
"id": "A73W8dyWRYkW"
}
},
{
"cell_type": "code",
"source": [
"A[1] = A[1] - 2*A[0]\n",
"A[2] = A[2] + A[0]\n",
"A[3] = A[3] + 2*A[0]\n",
"print(A)"
],
"metadata": {
"id": "xHEiBR0jQ1uJ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"The key column is now the second column and the pivot element is $5$ on the second row. You then use elementary row operations to turn the key column into a column that has $1$ at the pivot element and $0$'s elsewhere."
],
"metadata": {
"id": "al00bvajSieQ"
}
},
{
"cell_type": "code",
"source": [
"A[1] = 1/5*A[1]\n",
"A[0] = A[0] + 2*A[1]\n",
"A[2] = A[2] + A[1]\n",
"A[3] = A[3] + A[1]\n",
"print(A)"
],
"metadata": {
"id": "lVt90Jo0Seu4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"This matrix is already in the desirable form. It corresponds to the linear system\n",
"$$0.2s_1+0.4s_2+s_3=3.4$$\n",
"$$y-0.4s_1+0.2s_2=1.2$$\n",
"$$x+0.6s_1+0.2s_2=4.2$$\n",
"$$1.6s_1+0.2s_2+P=7.2$$\n",
"The maximum value of $P$ is $7.2$ and it is attained when $s_1=s_2=0$. Substituting $s_1=s_2=0$ into the system, you can easily see that $x=4.2$ and $y=1.2$. We conclude that the minimum value of $C$ is $-7.2$ and it is attained when $x=4.2$ and $y=1.2$."
],
"metadata": {
"id": "gLasE-30VO3n"
}
},
{
"cell_type": "markdown",
"source": [
"###**Exercise 8**"
],
"metadata": {
"id": "O77FRRnhXF9F"
}
},
{
"cell_type": "markdown",
"source": [
"Use the simplex method (as illustrated above) to solve the following linear programming problem:\n",
"\n",
"Maximize $P=x+4y-z$ subject to\n",
"$$x-y\\ge -4$$\n",
"$$3x-y+z\\ge 0$$\n",
"$$2x+2y+z\\le 2$$\n",
"$$x,y,z\\ge 0$$"
],
"metadata": {
"id": "TcSP7lI57tUf"
}
},
{
"cell_type": "markdown",
"source": [
"###**Exercise 9**\n",
"You can use the built-in command **optimize.linprog** to solve a linear programming problem. To use this command, your problem must be\n",
"* a minimizing problem,\n",
"* all constraints are of the form $\\le$,\n",
"* all variables are nonnegative.\n",
"\n",
"For example, consider a linear programming problem discussed earlier: minimize $C=-2x+y$ subject to\n",
"\\begin{align}\n",
" x-y\\le& 3\\\\\n",
" 2x+3y\\le& 12\\\\\n",
" x+y\\ge& 2\\\\\n",
" x,y\\ge& 0\n",
"\\end{align}\n",
"This problem is already a minimizing problem. Only the third inequality needs to be reversed. You rewrite the problem as: minimize $C=-2x+y$ subject to\n",
"\\begin{align}\n",
" x-y\\le& 3\\\\\n",
" 2x+3y\\le& 12\\\\\n",
" -x-y\\le& -2\\\\\n",
" x,y\\ge& 0\n",
"\\end{align}\n",
"The array of coefficients in the objective function $-2x+y$ is\n",
"$$c=[-2,1]$$\n",
"The matrix of coefficients in the constraints is\n",
"$$\n",
"A=\\left[\n",
" \\begin{matrix}\n",
" 1&-1\\\\\n",
" 2&3\\\\\n",
" -1&-1\n",
" \\end{matrix}\n",
"\\right]\n",
"$$\n",
"The array of the right-hand-side of the constraints is\n",
"$$b=[3,12,-2]$$\n",
"Now you can use **optimize.linprog** to solve the problem as follows."
],
"metadata": {
"id": "_m2vetNfx9zv"
}
},
{
"cell_type": "code",
"source": [
"c = array([-2,1])\n",
"A = array([[1,-1],[2,3],[-1,-1]])\n",
"b = array([3,12,-2])\n",
"opt = optimize.linprog(c,A,b)\n",
"print(opt)"
],
"metadata": {
"id": "SUsFl1Uo8TY4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"The output says that the minimum value of the objective function is $-7.2$ and it is attained at $x=4.2$ and $y=1.2$.\n",
"\n",
"Use **optimize.linprog** to solve [Exercise 8](#linprog)."
],
"metadata": {
"id": "jpt5nfQX2zth"
}
},
{
"cell_type": "markdown",
"source": [
"###**Exercise 10**\n",
"A candy company makes three types of candy (solid, fruit, and cream-filled) and packages these candies in three different assortments. A box of assortment I contains 4 solid, 4 fruit, 12 cream and sells for \\$9.40. A box of assortment II contains 12 solid, 4 fruit, 4 cream and sells for \\$7.60. A box of assortment III contains 8 solid, 8 fruit, 8 cream and sells for \\$11.00. The manufacturing costs per candy are \\$0.20 for solid, \\$0.25 for fruit, and \\$0.30 for cream. The company can manufacture up to 4800 solid, 4000 fruit, and 5600 cream candies weekly. How many boxes of each type should the company produce in order to maximize profit? What is their maximum profit?\n",
"\n",
"Setup the linear programming model. Then use **optimize.linprog** to solve it.\n",
"\n"
],
"metadata": {
"id": "w8iRlgRI7sQw"
}
}
]
}