EGR 103/Fall 2014/Reviews
Jump to navigation
Jump to search
For Skills Quiz
- 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 ‘_’
- 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
- 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
- Understand the math operators
- Know how to use the following scalar operators: + - .* ./ .^
- Understand the order of precedence of the operators
- Parentheses () innermost first
- Exponentiation operator .^
- Unary (one variable) operator + and – (right to left) – says a number is positive or negative:
- +MyVal
- Binary (two variable) operators .* ./ (left to right):
- MyVal .* YourVal
- 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
- 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
- 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)
- 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.
- 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.
- Know how to create and use anonymous functions
- 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)
- Know how to make a logical mask in an anonymous function – must be one line of code!
- Know how to create and use .m file functions
- Function file name must start with a valid name in MATLAB and end in “.m”
- 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
- Typically:
- When called, function input arguments must be enclosed in parentheses
- Arguments to functions may be constants, variables, expressions, other functions
- If a function gives multiple outputs, the variables that receive them are listed in square brackets; i.e. “[rows, cols] = size(rand(3,4))”
- A function can be called in the command window, in a script file, or in a function
- 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
- 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
- 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
- Know how to use the following MATLAB commands
- % - comment
- who – lists variables currently in memory
- whos – same as who but also shows size, memory, and type
- clear var1 var2 - removes var1 and var2 from memory
- clear – removes all variables from memory (versus clc – clears the screen)
- ; - suppresses screen printing at end of a line
- … ellipsis; continues a line
- ↑ - relists prior commands
- Know how to use strings
- Be able to assign a string to a variable name
- Be able to access part of a string
- Know how to use the input function – and be able to input numbers or strings
- Know how to use the fprintf function, including format codes (%X.Yf, %X.Ye, %c, and %s) and control codes (\n, \\, %%)
- Know how to use the error and warning functions
- Understand the save and load commands
- Be able to save all or select variables to a specified file in both MATLAB language and ASCII (text)
- Be able to load all or select variables from a MATLAB language .mat file – not on test / good to know
- Be able to load data from an ascii file. Understand that data loaded from an ascii file is loaded in a single matrix.
- load BLAH.txt will load data from BLAH.txt into a matrix called BLAH
- MyData=load(‘BLAH.txt’) will load from BLAH.txt into a matrix called MyData
- Know how to use matrices
- Be able to create a matrix using square brackets, colon notation, built-in commands, and extracting parts of other matrices
- Know how to transpose a vector or matrix using a single quote
- Be able to use an element or elements of a matrix in an equation
- Know how to extract an entire row or column of a matrix using the :
- Be able to do scalar addition, subtraction, multiplication, division, and exponentiation with matrices with MATLAB and by hand
- Know how to generate a vector with a : (ex. x = 1:5)
- Know how to generate a vector with an increment other than one (ex. x=2:2:8 or y=5:-3:-5)
- 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)
- Know how to access a subgroup of elements within a matrix
- 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)
- Understand how to use the end keyword with matrices and vectors
- Never use an element that does not exist in your matrix or vector on the right part of an assignment or in an equation!!!!!
- Know what happens if you assign a value to a matrix element outside the bounds of your original matrix
- Understand how sum, mean, min, and max work with matrices and vectors
- Logic
- 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)
- Understand the difference between a single = (the assignment statement) and a double == (asking the question, “are these two things equal?”)
- Be able to use logical masks to generate a piecewise function.
- Know how to use the logical operators (~, &, |) and what order they process in (parentheses first, then nots, then ands, then ors last)
- 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)
- Know how to use the find command to determine values for which logical expressions are true
- Know how any() and all() commands work
- If trees
- Know the format of an if tree (including elseif and else and end) – and recognize that “else if” is very different from elseif.
- 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
- Know that an if or elseif looks at all the logic at once and does not go element by element
- Know that else does not take a logical expression
- Plotting
- 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
- 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: - : -. --
- 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
- plot(x, y, 'g-', x, y, 'rs')
- Know the commands to add a title, axis labels, and a well-placed legend
- Know the command and adverb to save a plot into a PostScript file
- For loops
- Know the format of a for loop
- 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
- Know the difference between a for loop and a while loop
- 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.
- Be able to tell how many times a loop will execute.
- 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).
- Be able to use a counter variable in conjunction with a for loop
- While loops
- Know the format of a while loop
- Know how to use a while loop to check input conditions
- Use a while loop when you will repeat commands until a certain condition is met.
- Be able to tell how many times a loop will execute (remember that some while loops will never execute and some will execute infinitely).
- Know that an infinite loop can occur
- Know how to use the break command to get out of a while loop
- Be able to use a counter variable in conjunction with a while loop
Good To Know; Not Explicitly On Quiz
- Strings
- Be able to compare strings using strcmp
- Be familiar with the functions lower, upper, num2str and str2num
- Switch trees
- Know the format of a switch tree (including case and otherwise and end)
- Know when you can use a switch tree
- Know how to have multiple triggers from a case, as in {‘blue’, ‘Blue’}
- Know that otherwise does not take a logical expression
Test I
- Know everything from the Quiz. Especially logic, loops, decision structures, input, fprintf’ing, plotting, loading, and functions.
- Matrices and Parametric Functions
- 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
- Know how to use meshc and surfc to generate plots of surfaces
- Know how to use functions such as find, max, and min to determine significant values on a surface and where they occur
- Know how to use plot3 to generate a curve in space
- Know how to label and title 3-D Plots
- Know how to change color maps and add color bars
- Be able to use the contour and clabel functions to generate and graph contours.
- Be able generate a particular number of contours or contours at particular values
- Series Solutions
- Be able to write a program that calculates a series solution by computing single terms and accumulating them (SinSeries, plate temperature, etc)
- Seriously.
- Subplots
- Know how to use and set up subplots
- Iterative methods
- 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.
- Seriously.
- Roots in General (note: some parts specifically for continuous functions)
- Roots are values of input variables that cause a function to equal zero.
- Graphically, real roots can be found on plots as the x values where y is equal to zero.
- 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.
- Imaginary and complex roots cannot be seen on a graph of real numbers but can be found with methods such as Newton’s Method.
- 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.
- MATLAB’s built-in root finding functions
- 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.
- Be able to set up and solve for a single root of a single-input function using fzero.
- Be able to set up and solve for a single root of a multiple-input function using fzero.
- Be able to use a loop to set up and solve for multiple roots of a single- or multiple-input function using fzero.
- 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.
- 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.
- MATLAB’s built-in min finding functions
- 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.
- Be able to set up and solve for a single min or max of a single-input function using fminbnd or fminsearch.
- 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).
- 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.
- 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.