{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Solving Equations\n",
"\n",
"For problems that arise in first and second year Physics, it is usually\n",
"advisable to solve the problem symbolically and then use the computer to\n",
"compute the result based on your solution. Here we look at a couple exceptions\n",
"to this rule.\n"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"from numpy import *"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Systems of Linear Equations\n",
"\n",
"When you have two or more equations in the form\n",
"\n",
"$$ \\begin{matrix}\n",
" C_{11} x_1 + C_{12} x_2 + ... & = & S_1 \\\\\n",
" C_{21} x_1 + C_{22} x_2 + ... & = & S_2 \\\\ \n",
" ... & & \n",
"\\end{matrix}$$\n",
"\n",
"where the $C_{ij}$ are constant coefficients and the $S_i$ are also constants,\n",
"solution by hand can be tedious and error-prone.\n",
"\n",
"The computer can solve this by matrix methods. (You learn about this in linear algebra, but if you haven't yet studied that don't worry.) The trick is to write the coefficients as a matrix that is multiplied to a column of variables.\n",
"\n",
"For example, the two equations \n",
"\n",
"$$Ax + By = E\\\\\n",
" Cx + Dy = F $$\n",
"\n",
"are written as\n",
"\n",
"$$\\begin{bmatrix}A & B \\\\ C & D \\end{bmatrix}\n",
"\\begin{bmatrix} x\\\\ y\\end{bmatrix} = \n",
"\\begin{bmatrix} E\\\\ F\\end{bmatrix} $$\n",
"\n",
"Now Python can solve this with its \n",
"[linear algebra](http://docs.scipy.org/doc/numpy/reference/routines.linalg.html)\n",
"routines as follows.\n",
"\n",
"### Two Linear Equations\n",
"\n",
"Solve the following equations for $x$ and $y$:\n",
"\n",
"$$ 5x+4y=-1 \\\\\n",
" -7x-2y=-13 $$\n",
"\n",
"Get the problem into matrix form:\n",
"\n",
"$$\\begin{bmatrix}5 & 4 \\\\ -7 & -2 \\end{bmatrix}\n",
"\\begin{bmatrix} x\\\\ y\\end{bmatrix} = \n",
"\\begin{bmatrix} -1\\\\ -13\\end{bmatrix} $$\n",
"\n",
"And let `numpy.linalg` do the rest:\n"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"CC = array((( 5., 4. ),\n",
" ( -7., -2. )))\n",
"SS = array(( -1., -13. ))\n",
"solution = linalg.solve(CC,SS)\n",
"print (solution)"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"That is, $x=3$ and $y=-4$ solve our system of equations.\n",
"\n",
"### Three Linear Equations\n",
"\n",
"Solve the following equations for $x$, $y$ and $z$:\n",
"\n",
"$$ 2x+-y+3z=37 \\\\\n",
" x+y-3z=-16 \\\\\n",
" -3x+5z=24 $$\n",
"\n",
"Generalizing to three or more equations is straightforward.\n"
],
"metadata": {}
},
{
"cell_type": "code",
"source": [
"CC = array((( 2, -1, 3 ),\n",
" ( 1, 1, -3 ),\n",
" ( -3, 0, 5 )))\n",
"SS = array(( 37,-16, 24 ))\n",
"solution = linalg.solve(CC,SS)\n",
"print (solution)"
],
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Solution: $x=7$, $y=4$, $z=9$\n"
],
"metadata": {}
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}