{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "3ssNNiKewpZs" }, "source": [ "# EE16A Discussion 0D" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "BD2cWcfpiPmd" }, "source": [ "Welcome to Google Colab! We are trying this tool for IPython notebooks during discussion sections. It's new to us too -- please be patient with any technical issues that may come up. Let us know how it works for you! The objective is to have you working **together** to edit these notebooks. \n", "\n", "1. When you have some time (but not necessarily right now during discussion), visit https://colab.research.google.com/ where you can learn more about Colab and poke around a bit to learn about some of its features.\n", "\n", "2. For now, just edit this notebook together with your other group members. **You each have your own runtime, but you're editing the same underlying IPython notebook**. In order for your peers to see your edits, you need to SAVE (CTRL/CMD + s) and ask your peers to reload their version. (You have your own copies of each python variable, etc. but the underlying IPython file is shared)\n", "\n", "3. To avoid having to reload the runtime so much, you can try this (let us know if this works better for you): have the group pick someone to \"drive\" and share their screen with everyone else so everyone can follow along with what their runtime is showing. " ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "VfIaE1pIwpZx" }, "source": [ "### Graphing System of Two Equations\n", "\n", "For the first half of Problem 1a, you were asked to solve systems of two equations. Now we will develop some graphical intuition for these problems. Each equation can be rewritten as $ax+by=c$ and thus describes a line in $\\mathbb{R}^2$. Therefore, the solution to the system of equations must lie on the lines described by the system of equations (convince yourselves why this is the case).\n", "\n", "In the next few blocks, there is code to graph two lines. Play around with the parameters $a1,b1,\\ldots,c2$ to create different lines. Change the parameters to describe the system of equations from parts i-iii. For each part use the graphs to explain the results you found in Problem 1a parts i-iii." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "XSTeXN0syDyd" }, "outputs": [], "source": [ "# imports\n", "import numpy as np\n", "from matplotlib import pyplot as plt\n", "# if you're in your own Jupyter notebook, you can use this for interactive plotting\n", "# %matplotlib notebook" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "Id0Ql2NJ2N1I" }, "outputs": [], "source": [ "def line_xy(a,b,c):\n", " '''For the parameters a,b,c this function returns (x, y) pairs\n", " that are on the line ax+by = c'''\n", " b = b+.00001 # just avoiding divide-by-zero in line below\n", " x_range = [-100, 100]\n", " x = np.linspace(x_range[0],x_range[1],100)\n", " y = (c - a*x) / b \n", " return x, y" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "WKlCxZQ42V1y" }, "outputs": [], "source": [ "# use for parts i-iii\n", "# Parameters for the first line\n", "a1 = \n", "b1 = \n", "c1 = \n", "\n", "# Parameters for the second line\n", "a2 = \n", "b2 = \n", "c2 = \n", "\n", "x1, y1 = line_xy(a1,b1,c1)\n", "x2, y2 = line_xy(a2,b2,c2)\n", "\n", "fig = plt.figure()\n", "\n", "# # let matplotlib set the axis limits\n", "ax = fig.add_subplot(111)\n", "\n", "# or you can change these manually to make the plot look better\n", "# plot_xlimits = [-100, 100] \n", "# plot_ylimits = [-1000, 1000] \n", "# ax = fig.add_subplot(111, xlim=plot_xlimits, ylim=plot_ylimits)\n", "\n", "ax.plot(x1,y1,'-.b')\n", "ax.plot(x2,y2,'r', alpha=0.5)\n", "ax.grid(True)\n", "ax.legend(['Line 1','Line 2'])" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "wYC12pBdpQLG" }, "source": [ "You may be used to interactive plotting with matplotlib, but alas, it appears that because of the way Google hosts these notebooks for you, we can only do inline plots \"out of the box\" (see https://github.com/googlecolab/colabtools/issues/616)\n", "\n", "We'll have to get a little more fancy...read on\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "xMsBh2z42_u6" }, "source": [ "### Graphing systems of three equations " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "3OUrrj5C6BgK" }, "outputs": [], "source": [ "# while there are several ways to generate cool interactive plots in IPython notebooks, \n", "# for this we are going to use a package called Plotly which seems to mostly work in Colab. \n", "# You can read more about it here: https://plotly.com/python/\n", "from plotly import graph_objects\n", "from plotly import offline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "9LS0eE063giR" }, "outputs": [], "source": [ "def plane_xyz(a,b,c,d):\n", " '''For the parameters a,b,c,d this function returns matrices x_mat, y_mat, and z_mat\n", " that we can use to visualize a plane with the form ax+by+cz = d'''\n", " \n", " c = c+.00001 # again, avoid divide by zero\n", " x_range = [-10, 10]\n", " x = np.linspace(x_range[0],x_range[1],100)\n", " y = np.linspace(x_range[0],x_range[1],100)\n", " yt = y[:,np.newaxis]\n", " \n", " x_mat, y_mat = np.meshgrid(x, y, indexing='xy')\n", " z_mat = (d - a*x_mat - b*y_mat) / c\n", " return x_mat, y_mat, z_mat" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "SYlotR1yzRIe" }, "outputs": [], "source": [ "# just defining to objects we'll use to format how the plot looks\n", "axis_obj = dict(\n", " gridcolor=\"rgb(255, 255, 255)\", # set grid line color\n", " zerolinecolor=\"rgb(255, 255, 255)\", # set zero grid line color\n", " range=[-10, 10]\n", ")\n", "layout_obj = graph_objects.Layout(\n", " title='Planes in 3D', # set plot title\n", " scene=graph_objects.layout.Scene( # (!) axes are part of a 'scene' in 3d plotly plots\n", " xaxis=graph_objects.layout.scene.XAxis(axis_obj), # set x-axis style\n", " yaxis=graph_objects.layout.scene.YAxis(axis_obj), # set y-axis style\n", " zaxis=graph_objects.layout.scene.ZAxis(axis_obj)), # set z-axis style\n", " showlegend=True\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": {}, "colab_type": "code", "id": "7g8w-bRL3X57" }, "outputs": [], "source": [ "# Use for parts iv-vi\n", "\n", "# Parameters for the first plane\n", "a1 = \n", "b1 = \n", "c1 = \n", "d1 = \n", "\n", "# Parameters for the second plane\n", "a2 = \n", "b2 = \n", "c2 = \n", "d2 = \n", "\n", "# Parameters for the third plane\n", "a3 = \n", "b3 = \n", "c3 = \n", "d3 = \n", "\n", "# Plotting \n", "x1, y1, z1 = plane_xyz(a1,b1,c1,d1)\n", "x2, y2, z2 = plane_xyz(a2,b2,c2,d2)\n", "x3, y3, z3 = plane_xyz(a3,b3,c3,d3)\n", "\n", "blue=[[0, 'rgb(0,0,255)'], [1, 'rgb(0,0,255)']]\n", "green=[[0, 'rgb(0,255,0)'], [1, 'rgb(0,255,0)']]\n", "red=[[0, 'rgb(255,0,0)'], [1, 'rgb(255,0,0)']]\n", "\n", "\n", "trace1 = graph_objects.Surface(x=x1, y=y1, z=z1, colorscale=blue, name='First Plane')\n", "trace2 = graph_objects.Surface(x=x2, y=y2, z=z2, colorscale=green, name='Second Plane')\n", "trace3 = graph_objects.Surface(x=x3, y=y3, z=z3, colorscale=red, name='Third Plane')\n", "fig = graph_objects.Figure(data=[trace1, trace2, trace3], layout=layout_obj)\n", "offline.iplot(fig)" ] } ], "metadata": { "anaconda-cloud": {}, "colab": { "collapsed_sections": [], "name": "dis0D_breakout1.ipynb", "provenance": [] }, "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.7.7" } }, "nbformat": 4, "nbformat_minor": 1 }