Difference between revisions of "EGR 103/Fall 2014/Reviews"

From PrattWiki
Jump to navigation Jump to search
Line 1: Line 1:
DRAFT - posted 9/17/2014 - will be ready in a few days.
+
 
  
 
== For Skills Quiz ==
 
== For Skills Quiz ==

Revision as of 14:01, 29 September 2014


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)
      - suppresses screen printing at end of a line
    6. … ellipsis; continues a line
    7. ↑ - 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