Difference between revisions of "EGR 103/Spring 2019/Test 2"

From PrattWiki
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 9: Line 9:
 
import matplotlib.pyplot as plt
 
import matplotlib.pyplot as plt
 
from mpl_toolkits.mplot3d import axes3d
 
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm
 
 
import scipy.optimize as opt
 
import scipy.optimize as opt
 
</syntaxhighlight>
 
</syntaxhighlight>
 
  
 
== CS 101 Practice Tests ==
 
== CS 101 Practice Tests ==
Line 67: Line 65:
  
 
== curve fitting ==
 
== curve fitting ==
* Write code that finds coefficients for polynomial, general linear, and nonlinear fits
+
* Write code that finds coefficients for polynomial, general linear, and nonlinear fits; you will be given the following:
 +
** For polynomial fits, the order of the fit
 +
** For general linear or nonlinear, the model equation
 
* 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
 
* 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
 +
* Be able to describe if a fit is mathematically good based on the <math>r^2</math> value
 +
** <math>r^2>0.95</math> is a "good fit"
 +
** <math>r^2<0</math> means the fit is worse than merely picking the average value of the data
  
 
== surfaces ==
 
== surfaces ==

Latest revision as of 18:48, 2 April 2019

The test is necessarily cumulative, so while the focus will be on material learned since Test 1, the items at EGR_103/Spring_2019/Test_1 is still in play.

Memorizing Chapra 4.2 is not required.

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
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

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)
      • Type for us can be any integer, np.inf, or 'fro' (where the last one has to be a string)
  • Condition numbers
    • Be able to calculate the condition numbers for up to 2x2 matrices using 1, Frobenius, or infinity norms by hand
    • Be able to calculate the condition numbers for any square matrix using any norm using efficient Python code
    • Understand that "efficient Python code" means using np.linalg.cond(ARRAY, TYPE)
      • Type for us can be 1, 2, np.inf, or 'fro' (where the last one has to be a string)
    • 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; you will be given the following:
    • For polynomial fits, the order of the fit
    • For general linear or nonlinear, the model equation
  • 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
  • Be able to describe if a fit is mathematically good based on the \(r^2\) value
    • \(r^2>0.95\) is a "good fit"
    • \(r^2<0\) means the fit is worse than merely picking the average value of the data

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
  • 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.
  • Neither colormaps (cmap) nor facecolors nor colorbars will be tested

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