EGR 103/Fall 2014/Reviews

From PrattWiki
Jump to navigation Jump to search


For Skills Quiz

  1. Understand how to write valid MATLAB variable names
    • Must start with a letter
    • Can contain numbers, but not as the first character
    • May be up to 31 characters long
    • Case sensitive
    • May not contain spaces or punctuation except ‘_’
  2. Know the meaning of the following built-in variables
    • ans – value of an expression not assigned to a variable
    • eps – floating point precision – ratio of a number to the largest number that can still “see” it
    • i, j - unit imaginary numbers sqrt(-1)
    • pi – 3.141592 …
    • realmax – largest positive floating point number
    • realmin – smallest positive floating point number with full precision
  3. Understand the following features of assignment (the =)
    • There can only be a single variable on the left hand side of an assignment statement unless the assignation comes from a function returning multiple matrices
    • Assignment is not a question about equality
    • If an entire variable is assigned a new value, the former value is lost
      • x = [1 2 3] wipes out any old “x” values
    • If only a part of a variable is assigned a new value, everything else is retained
      • x(3) = 3 keeps all other values of x (expands if necessary)
    • If a location outside of the current size of a variable is assigned, the matrix will stretch to fit, putting zeros to retain its shape; Note: this always works for the x(row,col)= version and for vectors with the x(element)= version
  4. Understand the math operators
    • Know how to use the following scalar operators: + - .* ./ .^
    • Understand the order of precedence of the operators
      1. Parentheses () innermost first
      2. Exponentiation operator .^
      3. Unary (one variable) operator + and – (right to left) – says a number is positive or negative:
        • +MyVal
      4. Binary (two variable) operators .* ./ (left to right):
        • MyVal .* YourVal
      5. Binary operators (two variable) + - (left to right)
        • MyVal + YourVal
    • Be able to write the MATLAB command to calculate a complicated equation using scalar operators
    • Be able to evaluate a complicated MATLAB equation and give the resulting answer
  5. Know how to use the following math functions:
    • exp(x) - ex (note that just “e” is NOT defined in MATLAB)
    • log(x) – natural log
    • log10(x) – base 10 log
    • sqrt(x) – square root
    • abs(x) – absolute value
    • round(x), floor(x), ceil(x), fix(x) – ways to round to an integer
  6. Know how to use all the trig functions from help elfun – especially the difference between using radians (sin, tan, etc) or degrees (sind, tand, etc); know how to convert between degrees and radians (1 deg = (pi/180) rad)
  7. Know how min, max, mean, and sum work
    • Remember that for 1xN or Nx1 matrices, they produce a single value, while for NxM matrices, where N and M are greater than 1, they produce a 1xM row of answers based on applying the function to each column of the matrix.
  8. Know how find works and how to use it to determine where the nonzero entries are in a matrix. Also know how that can be used to figure out corresponding values in different matrices.
  9. Know how to create and use anonymous functions
    1. Anonymous functions provide a way to create a 1 line, 1 output function that does not have to be stored in a separate m-file
    • Example: c = @(a,b) sqrt(a.^2 + b.^2)
    1. Know how to make a logical mask in an anonymous function – must be one line of code!
  10. Know how to create and use .m file functions
    1. Function file name must start with a valid name in MATLAB and end in “.m”
    2. Function code must start with a function paradigm:
      • Typically:
        function [out1, out2, …]=FunName(in1, in2, …)
      • If no outputs, just:
        function FunName(in1, in2, …)
        If no inputs, just:
        function [out1, out2, …]=FunName
    3. When called, function input arguments must be enclosed in parentheses
    4. Arguments to functions may be constants, variables, expressions, other functions
    5. If a function gives multiple outputs, the variables that receive them are listed in square brackets; i.e. “[rows, cols] = size(rand(3,4))”
    6. A function can be called in the command window, in a script file, or in a function
    7. Variables declared in a function are local to that function – that is, nothing outside the function can “see” them and the function cannot “see” variables outside the function
    8. nargin and nargout can be used to determine the number of input arguments called and number of output arguments expected – these can be used with if trees to set default values for unspecified inputs
    9. If a function runs the command return, the function stops right then and returns the values stored in the output argument names to the function call
  11. Know how to use the following MATLAB commands
    1. % - comment
    2. who – lists variables currently in memory
    3. whos – same as who but also shows size, memory, and type
    4. clear var1 var2 - removes var1 and var2 from memory
    5. clear – removes all variables from memory (versus clc – clears the screen)
    6.  ; - suppresses screen printing at end of a line
    7. … ellipsis; continues a line
    8. ↑ - relists prior commands
  12. Know how to use strings
    1. Be able to assign a string to a variable name
    2. Be able to access part of a string
  13. Know how to use the input function – and be able to input numbers or strings
  14. Know how to use the fprintf function, including format codes (%X.Yf, %X.Ye, %c, and %s) and control codes (\n, \\, %%)
  15. Know how to use the error and warning functions
  16. Understand the save and load commands
    1. Be able to save all or select variables to a specified file in both MATLAB language and ASCII (text)
    2. Be able to load all or select variables from a MATLAB language .mat file – not on test / good to know
  17. Be able to load data from an ascii file. Understand that data loaded from an ascii file is loaded in a single matrix.
    1. load BLAH.txt will load data from BLAH.txt into a matrix called BLAH
    2. MyData=load(‘BLAH.txt’) will load from BLAH.txt into a matrix called MyData
  18. Know how to use matrices
    1. Be able to create a matrix using square brackets, colon notation, built-in commands, and extracting parts of other matrices
    2. Know how to transpose a vector or matrix using a single quote
    3. Be able to use an element or elements of a matrix in an equation
    4. Know how to extract an entire row or column of a matrix using the :
    5. Be able to do scalar addition, subtraction, multiplication, division, and exponentiation with matrices with MATLAB and by hand
    6. Know how to generate a vector with a : (ex. x = 1:5)
    7. Know how to generate a vector with an increment other than one (ex. x=2:2:8 or y=5:-3:-5)
    8. Know how to generate vectors using the linspace and logspace functions – note especially that the arguments of logspace are the powers of 10 – that is, to get [1 10 100] use logspace(0, 2, 3)
    9. Know how to access a subgroup of elements within a matrix
    10. Be able to use the following functions
      • eye(n) – creates an nxn identity matrix
      • eye(r,c) – creates an rxc matrix of zeros with 1’s on the main diagonal
      • ones(n) – creates a nxn matrix of ones
      • ones(r,c) – creates an rxc array of ones
      • zeros(n) – creates an nxn matrix of zeroes
      • zeros(r,c) – creates an rxc array of zeros
      • length(vec) - returns the length (largest dimension) of a matrix
      • size(mat) returns the number of rows and columns in a matrix or in individual matrices if two outputs are requested
      • size(mat, N) returns the N’th dimension (i.e. size(mat,1) is row count)
      • rand(n) – creates an nxn matrix of random numbers between (0,1)
      • rand(r,c) – creates an rxc array of random numbers between (0,1)
      • Use randi to create a range of random numbers – for example, integers between 1 and 6:
        x= randi([1 6], r, c)
    11. Understand how to use the end keyword with matrices and vectors
    12. Never use an element that does not exist in your matrix or vector on the right part of an assignment or in an equation!!!!!
    13. Know what happens if you assign a value to a matrix element outside the bounds of your original matrix
    14. Understand how sum, mean, min, and max work with matrices and vectors
  19. Logic
    1. Understand how to use the relational operators: (< , >, <=, >=, ==, ~=) and how to detect and correct the most common mistakes (1<x<3 is wrong; using = instead of == as a relational operator is wrong)
    2. Understand the difference between a single = (the assignment statement) and a double == (asking the question, “are these two things equal?”)
    3. Be able to use logical masks to generate a piecewise function.
    4. Know how to use the logical operators (~, &, |) and what order they process in (parentheses first, then nots, then ands, then ors last)
    5. Know how to use the find command to determine where nonzero elements are in a matrix and also figure out the value of other matrices at the same location (i.e. determining the largest negative deflection of a beam as well as where along the beam that deflection is located)
    6. Know how to use the find command to determine values for which logical expressions are true
    7. Know how any() and all() commands work
  20. If trees
    1. Know the format of an if tree (including elseif and else and end) – and recognize that “else if” is very different from elseif.
    2. Know that a statement that evaluates to 0 or any matrix with a 0 in it is considered false by an if tree – that is, the if and elseif statements basically put an all() on your logic for you
    3. Know that an if or elseif looks at all the logic at once and does not go element by element
    4. Know that else does not take a logical expression
  21. Plotting
    1. Be able to plot a single curve or multiple curves on the same graph – both by using hold on/hold off and by putting multiple plot triplets in the same plot command
    2. Know the
      • color codes: b g r c m y k
      • point style codes: . o x + * s d (there are others; just know these)
      • line style codes: - : -. --
    3. Note that a single plot style can have up to one color, point, and/or line style. To plot using a green line with red squares at the data points requires two plots; either
      • plot(x, y, 'g-', x, y, 'rs')
        or
      • plot(x, y, 'g-')
        hold on
        plot(x, y, 'rs')
        hold off
    4. Know the commands to add a title, axis labels, and a well-placed legend
    5. Know the command and adverb to save a plot into a PostScript file
  22. For loops
    1. Know the format of a for loop
    2. Know how the loop control expression can be written as a row vector using colon notation, as an explicitly written vector, and as a matrix
    3. Know the difference between a for loop and a while loop
    4. Be able to use a for loop to go column-by-column through a matrix – for one thing, use a for loop when you know before you start the loop how many times the loop will be executed.
    5. Be able to tell how many times a loop will execute.
    6. Be able to predict the output of a set of nested for loops (remember that the internal loop completely executes before the outer loop increments and repeats its set of commands).
    7. Be able to use a counter variable in conjunction with a for loop
  23. While loops
    1. Know the format of a while loop
    2. Know how to use a while loop to check input conditions
    3. Use a while loop when you will repeat commands until a certain condition is met.
    4. Be able to tell how many times a loop will execute (remember that some while loops will never execute and some will execute infinitely).
    5. Know that an infinite loop can occur
    6. Know how to use the break command to get out of a while loop
    7. Be able to use a counter variable in conjunction with a while loop

Good To Know; Not Explicitly On Quiz

  1. Strings
    1. Be able to compare strings using strcmp
    2. Be familiar with the functions lower, upper, num2str and str2num
  2. Switch trees
    1. Know the format of a switch tree (including case and otherwise and end)
    2. Know when you can use a switch tree
    3. Know how to have multiple triggers from a case, as in {‘blue’, ‘Blue’}
    4. Know that otherwise does not take a logical expression

Test I

  1. Know everything from the Quiz. Especially matrix manipulation, logic, loops, decision structures, input, fprintf’ing, plotting, loading, and functions.
  2. Matrices and Parametric Functions
    1. Know how to set up the independent variables for a 2-variable function if you want to generate a surface – both by hand and by using meshgrid
    2. Know how to use meshc and surfc to generate plots of surfaces
    3. Know how to use functions such as find, max, and min to determine significant values on a surface and where they occur
    4. Know how to use plot3 to generate a curve in space
    5. Know how to label and title 3-D Plots
    6. Know how to change color maps and add color bars
    7. Be able to use the contour and clabel functions to generate and graph contours.
    8. Be able generate a particular number of contours or contours at particular values
  3. Series Solutions
    1. Be able to write a program that calculates a series solution by computing single terms and accumulating them (SinSeries, plate temperature, etc)
    2. Seriously.
  4. Subplots
    1. Know how to use and set up subplots
  5. Iterative methods
    1. Understand how all of Chapra Figure 4.2 works and be able to reproduce it given some kind of series expansion or other mapping function.
    2. Seriously.
  6. Roots in General (note: some parts specifically for continuous functions)
    1. Roots are values of input variables that cause a function to equal zero.
    2. Graphically, real roots can be found on plots as the x values where y is equal to zero.
    3. For a continuous function, a valid bracket on the root would span the root and the sign of the function would be different at the lower and upper sides of the bracket – brackets may contain multiple roots, and for continuous functions, they will contain at least one root.
    4. Imaginary and complex roots cannot be seen on a graph of real numbers but can be found with methods such as Newton’s Method.
    5. Repeated roots (in even numbers) will cause the function to reach zero, but not cross the zero axis. To find repeated roots, methods depending on bracketing (including fzero) cannot be used since the function does not change signs at the location of the root.
  7. MATLAB’s built-in root finding functions
    1. In general, fzero can find a single root in a single dimension; for a multi-dimensional function, all other variable values must be known in advance.
      1. Be able to set up and solve for a single root of a single-input function using fzero.
      2. Be able to set up and solve for a single root of a multiple-input function using fzero.
      3. Be able to use a loop to set up and solve for multiple roots of a single- or multiple-input function using fzero.
      4. You can set up all of these using the general form (i.e. using an anonymous function as the first input to fzero) and will not be required to program or interpret any of the shortcuts.
  8. MATLAB’s built-in min finding functions
    1. In general, fminbnd can find a single local minimum in a single dimension; for a multi-dimensional function, all other variable values must be known in advance if you are using fminbnd. fminsearch can be used to find the minimum of a multivariable function if used correctly.
    2. Be able to set up and solve for a single min or max of a single-input function using fminbnd or fminsearch.
    3. Be able to set up and solve for a single min or max of a multiple-input function using fminbnd (if all other variables are known) or fminsearch (if there are multiple variables).
    4. Be able to use a loop to set up and solve for multiple minima or maxima of a single- or multiple-input function using fminbnd or fminsearch.
    5. You can set up all of these using the general form (i.e. using an anonymous function as the first input to fminbnd or fminsearch) and will not be required to program or interpret any of the shortcuts.

Test 2

  1. Know everything from the Quiz and from Test I.
  2. Statistics
    1. Know how to calculate the mean and the sum of the squares of the data residuals for a vector by hand and using MATLAB.
    2. Know how to define and calculate the sum of the squares of the estimate residuals and the coefficient of determination by hand and using MATLAB.
    3. Know the meaning of the coefficient of determination; that is, given a value for it, state whether a fit is mathematically good.
    4. Understand that mathematically good fits are not necessarily good from an engineering perspective.
  3. Linear Algebra
    1. Know how to write systems of linear equations in matrix form Ax=b.
    2. Be able to calculate the determinant of square matrices up to 3x3 and the inverses of square matrices up to 2x2 by hand.
    3. Know the MATLAB code to set up and solve for the determinant and inverse of an nxn matrix
    4. Be able to set up and solve 2-variable, 2-equation system by hand clearly demonstrating the use of the matrix inverse and matrix multiplication.
    5. Know the MATLAB code to set up and solve for the unknowns of an n-variable, n-equation system – including solutions 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 them.
        • Possibly using different line styles.
          • In color.
    6. Know the symbols for the different norms for vectors and matrices.
    7. Be able to determine the 1-, 2-, Euclidian-, and infinity-norm of a vector by hand and using MATLAB.
    8. Be able to determine the 1-, Frobenius-, and infinity-norm of a matrix by hand and using MATLAB.
    9. Be able to determine the 2-norm of a matrix using MATLAB.
    10. Be able to determine the condition number of a matrix based on the 1-, Frobenius-, and infinity-norm of a matrix by hand and using MATLAB. Note – since this requires an inverse, the largest system required by hand would necessarily be 2x2.
    11. Be able to determine the condition number of a matrix based on the 2-norm of a matrix using MATLAB.
    12. Be able to describe the meaning of the condition number with respect to the precision of a solution of a linear system relative to the precision of the system itself. Don’t forget the log10 part…
  4. Linear Fits
    1. Know how to set up a linear system to determine the least-squares fit coefficients for data given an arbitrary linear model.
    2. Be able to solve a polynomial fit of any order efficiently using MATLAB.
    3. Be able to solve a least-squares-fit of any number of terms for a general (linear) model using MATLAB. Linear in this case means that the model is made up of a sum of constants multiplied by functions of data. Note: this may involve multiple dimensions.
  5. Nonlinear Fits
    1. Be able to set up and solve a system for the least-squares-fit of a non-linear system using linearization. Note: this will be limited to the exponential, power law, and saturation growth models in the Chapra book. You should be able to solve these 2 parameter systems using MATLAB.
    2. For linearized methods, be prepared to perform a statistical analysis of how good the fit is using MATLAB. Note that the coefficient of determination is based on the untransformed variables even though the coefficients are found using transformed variables.
    3. Be able to set up and solve a system for the least-squares fit of a non-linear system using fminsearch and the sum of squares of estimate residuals
  6. Interpolation
    1. Note that interpolation is not the same as fitting. It does not find the best coefficients of a model to fit all the data. It forces a line or curve through all the points in the data set.
    2. Nearest-neighbor interpolation utilizes only one point – specifically the one with the independent value nearest the value for which you want to calculate an estimate. Be able to find values by hand.
    3. Piecewise-linear interpolation utilizes only two points. If given a vector of data to linearly interpolate by hand, only use the two data points that bracket your interpolation point.
    4. To interpolate over a large number of data points, piecewise polynomial interpolation uses localized nth-order fits whose coefficients are chosen to satisfy certain matching conditions. To avoid sharp transitions in the interpolated curve, requirements for continuity of slope and curvature may be imposed (cubic spline, or ‘spline’) or the curvature requirement may be tossed in favor of shape preservation (piecewise cubic Hermite interpolation, or ‘pchip’).
    5. Know how to use the MATLAB built-in interpolation functions interp1 and interp2. The syntax for interp1 is as follows:
      yinterp = interp1(x, y, xinterp,method)
      
      where:
      • yinterp is the interpolated value(or vector of values) of the function at the point(s) in xinterp
      • x and y are data vectors
      • xinterp is the point (or vector of points) where the interpolated value(s) of the function is needed
      • ‘method’ – is the interpolating method. Valid methods are
        • ‘nearest’ – nearest neighbor interpolation (sets yint equal to value of the nearest y data point)
        • ‘linear’ – linear interpolation
        • ‘spline’ – cubic spline interpolation
        • ‘cubic’ or ‘pchip’– piecewise cubic hermite interpolation (requires continuity of the 1st derivative of the function, but not the 2nd derivative)
    6. Note: The independent data presented to interp1 must be monotonic.
    7. Know how to use the MATLAB built-in spline interpolation function, spline. The syntax depends on whether you are using not-a-knot conditions at the end points or using clamped derivatives at the end points. For the former, which is the default case for interp1 with the ‘spline’ argument as well, use:
      yinterp = spline(x, y, xinterp)
      
      where:
      • yinterp is the interpolated value(or vector of values) of the function at the point(s) in xinterp
      • x and y are data vectors
      • xinterp is the point (or vector of points) where the interpolated value(s) of the function is needed
    8. For the clamped conditions, you must put the derivatives at the first and last points as the first and last entries of the y matrix:
      yinterpclamped = spline(x, [yp1 y ypN], xinterp)
      
      where yp1 and ypN are the first derivatives at the first and last points, respectively. These derivatives will be obtained either from an analytical expression or through some scientific process. The format above assumes y is a row vector; if a column use [yp1; y; ypN].
    9. You will not be required to find splines with “natural” end conditions.
  7. Numerical Derivatives (by hand) - see Fall 2010 Test III Problem I(1): link
    1. Know the equations for 2 and 3 pt backwards first derivatives, 2 and 3 pt forwards first derivatives, and 3 point central first and second derivatives.
    2. Know the exceptional cases:
      1. For two-point first derivatives, you must use forward for the first point and backward for the last point regardless of which method you are using for the rest of the points.
      2. For three-point first derivatives, there are special formulae at the beginning and end points that you must know.
      3. For second derivatives, at the ends, you simply use the value of the 2nd derivative at the second point for the first point, and the value for the N-1th point for the Nth point.
  8. Numerical Integrals (by hand) - see Fall 2010 Test III Problem I(1): link
    1. Know the equations for integration using the Trapezoidal Rule
    2. Know the equations for integration using the Trapezoidal Rule, Simpson's 1/3rd Rule and Simpson's 3/8ths Rule for 2, 3, and 4 points, respectively, assuming even spacing
    3. Know how to use the "hybrid" integration scheme depending on the number of points (or intervals)
      1. 0 for the integral at the first point (no intervals)
      2. Trapezoidal rule for the integral between the first and second points (one interval)
      3. Simpson's 1/3rd rule for the integral between the first and third points (two intervals)
      4. Simpson's 3/8ths rule for the integral between the first and fourth points (three intervals)
      5. Appropriate combinations of Simpson's Rules for integrals between the first and the fifth and later points.
        • For an odd number of points, use Simpson’s 1/3rd for all the intervals (the 1-4-(2-4)-1 pattern)
        • For an even number of points, use Simpson’s 1/3rd for all but the last three intervals (stop the 1-4-(2-4)-1 pattern at the N—3rd point) and use Simpson’s 3/8ths rule for the last interval.
    4. Know how to take an integral from an arbitrary starting point to an ending point. In other words, do not just know how to integrate from the first point to the Nth point, know what it takes to do an integral from, for example, the third point through the sixth point.