Difference between revisions of "EGR 103/Fall 2018/Test 2"
Jump to navigation
Jump to search
(14 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
Memorizing Chapra 4.2 is '''not''' required. | Memorizing Chapra 4.2 is '''not''' required. | ||
+ | == Givens == | ||
+ | You can assume all the following have already run: | ||
+ | <syntaxhighlight lang=python> | ||
+ | import math as m | ||
+ | import numpy as np | ||
+ | import matplotlib.pyplot as plt | ||
+ | from mpl_toolkits.mplot3d import axes3d | ||
+ | from matplotlib import cm | ||
+ | import scipy.optimize as opt | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | == CS 101 Practice Tests == | ||
+ | The following parts of [https://www2.cs.duke.edu/courses/common/cps06tests.html CS 101 practice tests] have material relevant to our Test II; please note that before Spring 2018, CS 101 used Python 2 and so those tests are not references. | ||
+ | * First Exams | ||
+ | ** Fall 2018: 3B, 5 | ||
+ | ** Spring 2018: 2A, 2B, 5 | ||
+ | * Second Exams | ||
+ | ** Spring 2018: 1A (sets not on test but good to know), 1B, 2A, 2C, 3, 4A, | ||
+ | ** Spring 2018 Booster: | ||
+ | |||
+ | == logical masks == | ||
+ | * Parentheses are critical! | ||
+ | * & and | are critical! | ||
+ | * See [[Python:Logical_Masks]] and be able to write code that does things like [[Python:Logical_Masks#Simplification_2]] or [[Python:Logical_Masks#Using_Logical_Masks_in_Functions]] for a given piecewise function. | ||
== dictionaries == | == dictionaries == | ||
+ | * Be able to start a dictionary from scratch with {} | ||
+ | * Be able to add new key/value pairs to dictionaries | ||
+ | * Be able to change values in dictionaries | ||
+ | * Be able to check if a key exists without breaking Python if it doesn't | ||
+ | * Be able to have a dictionary return a default value if a key doesn't exist | ||
+ | * Be able to iterate over the keys, the values, or both at once | ||
+ | |||
+ | == strings == | ||
+ | * Know how to use ord() and chr() and remember they can only convert one item at a time | ||
+ | * If needed, the relevant parts of the ASCII table (all or some of the printable characters - [https://en.wikipedia.org/wiki/ASCII#Printable_characters]) will be given to you | ||
+ | * Know how to use split() and join() | ||
+ | |||
+ | == files == | ||
+ | * Be able to open a file for writing, write formatted text to it, and close it (i.e. use open, write, and close) | ||
+ | * Be able to load rectangular arrays of data from a text file | ||
+ | |||
== linear algebra == | == linear algebra == | ||
Line 9: | Line 50: | ||
* Be able to calculate by hand the determinant of matrices up to 3x3 and the inverses of matrices up to 2x2. | * Be able to calculate by hand the determinant of matrices up to 3x3 and the inverses of matrices up to 2x2. | ||
* Be able to write Python code to take determinants and inverses of matrices. | * Be able to write Python code to take determinants and inverses of matrices. | ||
− | * Be able to set up and solve a systems of 2 equations with 2 unknowns by hand. | + | * Be able to set up and solve a systems of 2 equations with 2 unknowns by hand and without a calculator. |
* Be able to write Python code to set up and solve systems of N equations with N unknowns. | * Be able to write Python code to set up and solve systems of N equations with N unknowns. | ||
* '''Be able to write Python code to set up and solve for the unknowns of an N-variable, N-equation system and sweep through when some part of the A or b matrix changes due to some parameter. Be able to extract the individual components of the solution vectors and store them in arrays. And plot one or more of them. Possibly using different line styles. In color.''' | * '''Be able to write Python code to set up and solve for the unknowns of an N-variable, N-equation system and sweep through when some part of the A or b matrix changes due to some parameter. Be able to extract the individual components of the solution vectors and store them in arrays. And plot one or more of them. Possibly using different line styles. In color.''' | ||
− | |||
* Norms | * Norms | ||
− | ** Be able to calculate the 1, 2 / Euclidean, and infinity norm of a 1D array by hand | + | ** Be able to calculate the 1, 2 / Euclidean, and infinity norm of a 1D array by hand and by using efficient Python code |
− | ** Be able to calculate the 1, Frobenius, and infinity norm of a 2D array by hand | + | ** Be able to calculate the 1, Frobenius, and infinity norm of a 2D array by hand and using efficient Python code |
** Be able to calculate the 2 norm of a 2D array using efficient Python code | ** Be able to calculate the 2 norm of a 2D array using efficient Python code | ||
− | ** Understand that "efficient Python code" means using np.norm(ARRAY, TYPE) | + | ** Understand that "efficient Python code" means using np.linalg.norm(ARRAY, TYPE) |
− | |||
* Condition numbers | * Condition numbers | ||
− | ** Be able to calculate the condition numbers for up to 2x2 matrices using 1, 2, or Frobenius norms | + | ** Be able to calculate the condition numbers for up to 2x2 matrices using 1, 2, or Frobenius norms by hand or for any size matrix using any type of norm using Python code. |
** Be able to explain what a condition number means for a system - specifically, that the base-10 logarithm of the condition number gives an upper bound on how many digits of precision are lost because of the system. | ** Be able to explain what a condition number means for a system - specifically, that the base-10 logarithm of the condition number gives an upper bound on how many digits of precision are lost because of the system. | ||
+ | |||
+ | == curve fitting == | ||
+ | * Write code that finds coefficients for polynomial, general linear, and nonlinear fits | ||
+ | * Write code that determines the sum of the squares of data residuals for a data set as well as both the sum of the squares of the estimate residuals and the coefficient of determination for a given fit | ||
+ | |||
+ | == surfaces == | ||
+ | * The imports will be given to you | ||
+ | * You need to know how to properly create a figure and an axis for doing 3D projections | ||
+ | * Know how np.meshgrid() works, what the arguments are, and what it returns | ||
+ | * Know how ax.plot_wireframe() and ax.plot_surface() work, including the cmap and facecolors keyword arguments for plot_surface() | ||
+ | * Know that the default with a cmap is that the first color in the map goes to the bottom of the surface and the last color in the map goes to the top | ||
+ | * Remember that when using facecolors the argument of the colormap should have a domain of 0 to 1; anything less than 0 maps to 0 and anything greater than 1 maps to 1. | ||
+ | * Know how to add a colorbar if a cmap is in place. | ||
+ | * You will be given a list of colormaps. | ||
+ | * Know how to use ax.set() for setting axis labels, axis limits, and axis tick locations. | ||
+ | * For the test, the surfaces will be functions of x and y and will be plotted using Cartesian coordinates. | ||
== finding roots == | == finding roots == | ||
+ | * You will only be required to find roots on continuous functions for which you will be given sufficient information to find a bracket. | ||
+ | * You will only be required to understand how opt.brentq() works to find roots using a closed method. | ||
+ | * Remember that a valid bracket consists of two independent values for which a function has opposite signs. | ||
+ | * opt.brentq() can only find one root at a time; if you are finding multiple roots, you either need multiple commands or a loop | ||
+ | * If you are trying to determine where a function is equal to a value other than 0, you can create a lambda function for that. For instance, to find where some function f is equal to 8 between x values of 1 and 3, you could write: | ||
+ | <syntaxhighlight lang=python> | ||
+ | rootloc = opt.brentq(lambda thing: f(thing)-8, 1, 3) | ||
+ | </syntaxhighlight> | ||
== finding extrema == | == finding extrema == | ||
+ | * For 1D functions, you will only be required to find extrema on continuous functions for which you will be given sufficient information to decide boundaries - this means understanding how opt.fminbound() works to find bounded minima and what slight changes to make to find maxima. | ||
+ | * For 2D functions, you will only be required to find extrema on continuous functions for which you will be given sufficient information to decide initial guesses - this means understanding how opt.fmin() works to find unbounded minima on surfaces, what slight changes to make to find maxima, and how to use * to have a function call split a list into component parts. | ||
+ | * opt.fminbound() and opt.fmin() can only find one extremum at a time; if you are finding multiple extrema, you either need multiple commands or a loop | ||
+ | * If you are finding a maximum, remember that you will really be minimizing the negative version of a function. And you cannot simply put - in front of the function handle; instead, create a lambda function in the argument -- for example: | ||
+ | <syntaxhighlight lang=python> | ||
+ | maxloc = opt.fminbound(lambda blah: -f(blah), -1, 2) | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == other == | ||
+ | * Know how to use np.random.shuffle() on lists - remember that it changes the list! | ||
+ | |||
+ | == not on test == | ||
+ | * Coordinate systems other than Cartesian | ||
+ | * Psychedelic donuts |
Latest revision as of 20:31, 15 November 2018
The test is necessarily cumulative, so while the focus will be on material learned since Test 1, the items at EGR_103/Fall_2018/Test_1 is still in play.
Memorizing Chapra 4.2 is not required.
Contents
Givens
You can assume all the following have already run:
import math as m
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm
import scipy.optimize as opt
CS 101 Practice Tests
The following parts of CS 101 practice tests have material relevant to our Test II; please note that before Spring 2018, CS 101 used Python 2 and so those tests are not references.
- First Exams
- Fall 2018: 3B, 5
- Spring 2018: 2A, 2B, 5
- Second Exams
- Spring 2018: 1A (sets not on test but good to know), 1B, 2A, 2C, 3, 4A,
- Spring 2018 Booster:
logical masks
- Parentheses are critical!
- & and | are critical!
- See Python:Logical_Masks and be able to write code that does things like Python:Logical_Masks#Simplification_2 or Python:Logical_Masks#Using_Logical_Masks_in_Functions for a given piecewise function.
dictionaries
- Be able to start a dictionary from scratch with {}
- Be able to add new key/value pairs to dictionaries
- Be able to change values in dictionaries
- Be able to check if a key exists without breaking Python if it doesn't
- Be able to have a dictionary return a default value if a key doesn't exist
- Be able to iterate over the keys, the values, or both at once
strings
- Know how to use ord() and chr() and remember they can only convert one item at a time
- If needed, the relevant parts of the ASCII table (all or some of the printable characters - [1]) will be given to you
- Know how to use split() and join()
files
- Be able to open a file for writing, write formatted text to it, and close it (i.e. use open, write, and close)
- Be able to load rectangular arrays of data from a text file
linear algebra
- Given a set of linear algebra equations, be able to rearrange and write in matrix form.
- Be able to calculate by hand the determinant of matrices up to 3x3 and the inverses of matrices up to 2x2.
- Be able to write Python code to take determinants and inverses of matrices.
- Be able to set up and solve a systems of 2 equations with 2 unknowns by hand and without a calculator.
- Be able to write Python code to set up and solve systems of N equations with N unknowns.
- Be able to write Python code to set up and solve for the unknowns of an N-variable, N-equation system and sweep through when some part of the A or b matrix changes due to some parameter. Be able to extract the individual components of the solution vectors and store them in arrays. And plot one or more of them. Possibly using different line styles. In color.
- Norms
- Be able to calculate the 1, 2 / Euclidean, and infinity norm of a 1D array by hand and by using efficient Python code
- Be able to calculate the 1, Frobenius, and infinity norm of a 2D array by hand and using efficient Python code
- Be able to calculate the 2 norm of a 2D array using efficient Python code
- Understand that "efficient Python code" means using np.linalg.norm(ARRAY, TYPE)
- Condition numbers
- Be able to calculate the condition numbers for up to 2x2 matrices using 1, 2, or Frobenius norms by hand or for any size matrix using any type of norm using Python code.
- Be able to explain what a condition number means for a system - specifically, that the base-10 logarithm of the condition number gives an upper bound on how many digits of precision are lost because of the system.
curve fitting
- Write code that finds coefficients for polynomial, general linear, and nonlinear fits
- Write code that determines the sum of the squares of data residuals for a data set as well as both the sum of the squares of the estimate residuals and the coefficient of determination for a given fit
surfaces
- The imports will be given to you
- You need to know how to properly create a figure and an axis for doing 3D projections
- Know how np.meshgrid() works, what the arguments are, and what it returns
- Know how ax.plot_wireframe() and ax.plot_surface() work, including the cmap and facecolors keyword arguments for plot_surface()
- Know that the default with a cmap is that the first color in the map goes to the bottom of the surface and the last color in the map goes to the top
- Remember that when using facecolors the argument of the colormap should have a domain of 0 to 1; anything less than 0 maps to 0 and anything greater than 1 maps to 1.
- Know how to add a colorbar if a cmap is in place.
- You will be given a list of colormaps.
- Know how to use ax.set() for setting axis labels, axis limits, and axis tick locations.
- For the test, the surfaces will be functions of x and y and will be plotted using Cartesian coordinates.
finding roots
- You will only be required to find roots on continuous functions for which you will be given sufficient information to find a bracket.
- You will only be required to understand how opt.brentq() works to find roots using a closed method.
- Remember that a valid bracket consists of two independent values for which a function has opposite signs.
- opt.brentq() can only find one root at a time; if you are finding multiple roots, you either need multiple commands or a loop
- If you are trying to determine where a function is equal to a value other than 0, you can create a lambda function for that. For instance, to find where some function f is equal to 8 between x values of 1 and 3, you could write:
rootloc = opt.brentq(lambda thing: f(thing)-8, 1, 3)
finding extrema
- For 1D functions, you will only be required to find extrema on continuous functions for which you will be given sufficient information to decide boundaries - this means understanding how opt.fminbound() works to find bounded minima and what slight changes to make to find maxima.
- For 2D functions, you will only be required to find extrema on continuous functions for which you will be given sufficient information to decide initial guesses - this means understanding how opt.fmin() works to find unbounded minima on surfaces, what slight changes to make to find maxima, and how to use * to have a function call split a list into component parts.
- opt.fminbound() and opt.fmin() can only find one extremum at a time; if you are finding multiple extrema, you either need multiple commands or a loop
- If you are finding a maximum, remember that you will really be minimizing the negative version of a function. And you cannot simply put - in front of the function handle; instead, create a lambda function in the argument -- for example:
maxloc = opt.fminbound(lambda blah: -f(blah), -1, 2)
other
- Know how to use np.random.shuffle() on lists - remember that it changes the list!
not on test
- Coordinate systems other than Cartesian
- Psychedelic donuts