{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exercises in the IPy Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the following exercises, the \"examples\" are completed for you and the \"problems\" are for you to try. Each problem can be solved by a method very similar to the associated example. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## example 1 (simultaneous equations)\n",
    "\n",
    "Solve the following equations for $x$ and $y$:\n",
    "\n",
    "$5x+4y=-1$\n",
    "\n",
    "$-7x-2y=-13$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### solution:\n",
    "\n",
    "There are several ways to solve simultaneous equatiions. Here we use the matrix method for solving linear systems of equations. It amounts to writing the two equations as a $2\\times 2$ matrix of coefficients times a $2\\times1$ vector of variables.\n",
    "\n",
    "So\n",
    "\n",
    "$$Ax + By = E\\\\\n",
    "  Cx + Dy = F $$\n",
    "  \n",
    "is 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",
    "To get our problem in this form, write\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",
    "This is solved this with the `linalg` (i.e., [linear algebra](http://docs.scipy.org/doc/numpy/reference/routines.linalg.html)) routines as follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3. -4.]\n"
     ]
    }
   ],
   "source": [
    "CC = array(((  5.,  4. ),\n",
    "            ( -7., -2. ) ))\n",
    "SS = array(( -1., -13. ))\n",
    "solution = linalg.solve(CC,SS)\n",
    "print(solution)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That is, $x=3$ and $y=-4$ solve our system of equations."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## problem 1 (simultaneous equations)\n",
    "\n",
    "Solve the following equations for $x$, $y$ and $z$:\n",
    "\n",
    "$2x+-y+3z=37$\n",
    "\n",
    "$x+y-3z=-16$\n",
    "\n",
    "$-3x+5z=24$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(solution: $x=7$, $y=4$, $z=9$)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## example 2 (vector addition)\n",
    "\n",
    "Add the two vectors shown.\n",
    "\n",
    "<img src=\"images/prob-2-29.png\" />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a. vector sum [205.96448785 149.9048241 ] mm\n"
     ]
    }
   ],
   "source": [
    "dtr = pi/180.  # a factor to convert degrees to radians\n",
    "# part a\n",
    "R1 = 183.*array(( cos(55*dtr), sin(55*dtr) ))  # mm\n",
    "R2 = array(( 101., 0. ))                       # mm\n",
    "R = R1+R2\n",
    "print ('a. vector sum', R, 'mm')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b. vector sum [-0.86974134 -0.68025866] kip\n"
     ]
    }
   ],
   "source": [
    "# part b\n",
    "R1 = 1.23*array(( -cos(45*dtr), sin(45*dtr) ))   # kip\n",
    "R2 = array(( 0., -1.55 ))                        # kip\n",
    "R = R1+R2\n",
    "print ('b. vector sum', R, 'kip')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## problem 2  (vector addition)\n",
    "\n",
    "\n",
    "Add the two vectors shown.\n",
    "\n",
    "<img src=\"images/prob-2-30.png\" />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## example 3  (vector algebra)\n",
    "\n",
    "\n",
    "<img src=\"images/prob-2-37.png\" />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a. R=A+B= [350. 280.]\n",
      "   magnitude of R= 448.2186966202994 lb\n"
     ]
    }
   ],
   "source": [
    "A = array(( 150., -200. ))   # lb\n",
    "B = array(( 200.,  480. ))   # lb\n",
    "\n",
    "# part a\n",
    "R = A+B\n",
    "R_mag = sqrt( sum(R**2) )\n",
    "print ('a. R=A+B=',R)\n",
    "print ('   magnitude of R=',R_mag,'lb')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b. R=2A-(1/2)B= [ 200. -640.]\n",
      "   magnitude of R= 670.5221845696084 lb\n"
     ]
    }
   ],
   "source": [
    "# part b\n",
    "R = 2*A - (1/2)*B\n",
    "R_mag = sqrt( sum(R**2) )\n",
    "print ('b. R=2A-(1/2)B=',R)\n",
    "print ('   magnitude of R=',R_mag,'lb')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To do part (c), solve for $s$ when the $y$ component of $\\vec{R}$ is 0.\n",
    "\n",
    "$R_y = s A_y +  B_y = 0$\n",
    "\n",
    "so \n",
    "\n",
    "$s = -\\frac{B_y}{A_y}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c. s= 2.4\n"
     ]
    }
   ],
   "source": [
    "# part c\n",
    "s = - B[1]/A[1]\n",
    "print ('c. s=',s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For part (d), first compute $\\vec{C} = \\vec{B}-\\vec{A}$, then get its magnitude $\\left | \\vec{C} \\right |$.\n",
    "\n",
    "Finally, find the unit vector with $\\hat{u} = \\frac{\\vec{C}}{\\left | \\vec{C} \\right |}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d. unit vector= [0.07333144 0.99730763]\n"
     ]
    }
   ],
   "source": [
    "# part d\n",
    "C = B-A\n",
    "C_mag = sqrt(sum(C**2))\n",
    "u = C / C_mag\n",
    "print ('d. unit vector=',u)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## problem 3   (vector algebra)\n",
    "\n",
    "\n",
    "<img src=\"images/prob-2-38.png\" />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## example 4 (unit vectors)\n",
    "\n",
    "\n",
    "<img src=\"images/prob-2-41.png\" />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a. rAB= [3.46410162 2.        ] ft\n",
      "b. rBA= [-3.46410162 -2.        ] ft\n",
      "c. uAB= [0.8660254 0.5      ]\n",
      "d. uBA= [-0.8660254 -0.5      ]\n",
      "e. FAB= [6.92820323 4.        ] lb\n",
      "f. FBA= [-6.92820323 -4.        ] lb\n"
     ]
    }
   ],
   "source": [
    "rAB = 4.*array(( cos(30*dtr), sin(30*dtr) ))   # ft\n",
    "rBA = -rAB\n",
    "uAB = rAB / sqrt(sum(rAB**2))\n",
    "uBA = -uAB\n",
    "FAB = 8.*uAB  # lb\n",
    "FBA = 8.*uBA  # lb\n",
    "print ('a. rAB=',rAB,'ft')\n",
    "print ('b. rBA=',rBA,'ft')\n",
    "print ('c. uAB=',uAB)\n",
    "print ('d. uBA=',uBA)\n",
    "print ('e. FAB=',FAB,'lb')\n",
    "print ('f. FBA=',FBA,'lb')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## problem 4 (unit vectors)\n",
    "\n",
    "<img src=\"images/prob-2-40.png\" />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## example 5 (vector rotation)\n",
    "\n",
    "\n",
    "<img src=\"images/prob-2-55.png\" />\n",
    "\n",
    "Vector rotations are effected by multiplying the vector by the matrix of trig functions described above.  For coordinate rotation through angle $\\phi$:\n",
    "\n",
    "$$\n",
    "\\begin{bmatrix}v_t\\\\ v_n\\end{bmatrix} = \n",
    "\\begin{bmatrix}\n",
    "\\cos \\phi & \\sin \\phi \\\\ \n",
    "-\\sin \\phi & \\cos \\phi \n",
    "\\end{bmatrix} \n",
    "\\begin{bmatrix}v_x\\\\ v_y\\end{bmatrix}\n",
    "$$\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "original vector R= [-809.86806494  373.86858304] lb\n",
      "rotation matrix=\n",
      " [[ 0.76604444  0.64278761]\n",
      " [-0.64278761  0.76604444]]\n"
     ]
    }
   ],
   "source": [
    "R = 892.0 * array(( -cos(24.78*dtr), sin(24.78*dtr) ))\n",
    "phi = 40. * dtr            # coordinate rotation angle, radians\n",
    "rot = matrix(((cos(phi),sin(phi)),(-sin(phi),cos(phi))))\n",
    "print ('original vector R=',R,'lb')\n",
    "print ('rotation matrix=\\n',rot)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Multiplication of a matrix and a vector is done with the `dot` function. The solution agrees with the result of example 2.7 in the text."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rotated vector= [[-380.07683798  806.97310812]] lb\n"
     ]
    }
   ],
   "source": [
    "print ('rotated vector=', dot(rot,R), 'lb')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## problem 5 (vector rotation)\n",
    "\n",
    "\n",
    "<img src=\"images/prob-2-56.png\" />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## example 6 (unit vectors)\n",
    "\n",
    "\n",
    "<img src=\"images/prob-2-88.png\" />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "force the cable exerts on B is [-42.85714286  85.71428571 -28.57142857] lb\n",
      "force the cable exerts on E is [ 42.85714286 -85.71428571  28.57142857] lb\n"
     ]
    }
   ],
   "source": [
    "x = 6.  # in\n",
    "y = 12. # in\n",
    "rOB = array(( x, 0, 4. ))\n",
    "rOE = array(( 0, y, 0. ))\n",
    "rBE = rOE - rOB\n",
    "uBE = rBE / sqrt(sum(rBE**2))  # unit vector pointing from B to E\n",
    "FBE = 100.*uBE\n",
    "FEB = -FBE\n",
    "print ('force the cable exerts on B is',FBE,'lb')\n",
    "print ('force the cable exerts on E is',FEB,'lb')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## problem 6 (unit vectors)\n",
    "\n",
    "\n",
    "See problem 9, above. Use the parameters for 2.88."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## example 7 (dot product)\n",
    "\n",
    "\n",
    "<img src=\"images/prob-2-103.png\" />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a. angle between vectors= 45.90050773774112 degrees\n",
      "b. component of A parallel to B= 6.2631578947368425 N\n",
      "   component of A perpendicular to B= 6.4631921823194745 N\n",
      "c. vector component of A parallel to B= [ 0.32963989  5.93351801 -1.97783934] N\n",
      "   vector component of A perpendicular to B= [-1.32963989  2.06648199  5.97783934] N\n"
     ]
    }
   ],
   "source": [
    "A = array(( -1.,  8., 4. ))  # N\n",
    "B = array((  1., 18.,-6. ))  # mm\n",
    "AdotB = dot(A,B)\n",
    "A_mag = sqrt(sum(A**2))\n",
    "B_mag = sqrt(sum(B**2))\n",
    "uA = A / A_mag   # unit vector\n",
    "uB = B / B_mag   # unit vector\n",
    "cos_AB = arccos( dot(uA,uB) ) / dtr  # angle in degrees\n",
    "A_para_mag = dot(A,uB)\n",
    "A_para = A_para_mag*uB\n",
    "A_perp = A - A_para\n",
    "A_perp_mag = sqrt(sum(A_perp**2))\n",
    "print ('a. angle between vectors=',cos_AB,'degrees')\n",
    "print ('b. component of A parallel to B=',A_para_mag,'N')\n",
    "print ('   component of A perpendicular to B=',A_perp_mag,'N')\n",
    "print ('c. vector component of A parallel to B=',A_para,'N')\n",
    "print ('   vector component of A perpendicular to B=',A_perp,'N')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## problem 7 (dot product)\n",
    "\n",
    "\n",
    "See example 7 above. Use figure P2.104."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## example 8 (cross product)\n",
    "\n",
    "\n",
    "<img src=\"images/prob-2-146.png\" />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a. AxB= [ -4. -72. -40.] in^2\n",
      "b. BxA= [ 4. 72. 40.] in^2\n",
      "c. the cross product appears to be anti-commutative...!\n",
      "d. dot products: 0.0 and 0.0\n"
     ]
    }
   ],
   "source": [
    "A = array((  6., -2., 3. ))  # in\n",
    "B = array((-14., -2., 5. ))  # in\n",
    "C = cross(A,B)\n",
    "print ('a. AxB=',C,'in^2')\n",
    "print ('b. BxA=',cross(B,A),'in^2')\n",
    "print ('c. the cross product appears to be anti-commutative...!')\n",
    "print ('d. dot products:',dot(A,C),'and',dot(B,C))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## problem 8 (cross product)\n",
    "\n",
    "(book problem 2.146)\n",
    "\n",
    "See example 8 above. Use figure P2.146."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
