MATLAB GLOSSARY (as of September 21) -------------------- MATLAB "Things": integers: 1, 2, 3 real numbers: 1.23, -0.004, 5.67e8, 9.12E-4 complex numbers: mathematically, "a+bi". logical variables: a = true, b = false, c = ( x < 10 ) strings: 'a', 'Baby', 'Cat in a hat.', 'My child''s age is 7.' vectors (lists of data): [], [ a, b ], [ 1, 3, 5, 7 ], [ 'Mon', 'Tue', 'Wed', 'Thu', 'Fri' ] matrices: (coming soon) Special Names: eps the smallest number you can add to 1; Inf the result of computations like 1/0 NaN the result of computations like 0/0 pi realmax the largest real number MATLAB can handle realmin the smallest positive real number MATLAB can handle Arithmetic: A + 1 B - 2.3 C * D E / F G^2 Math functions: y = abs ( x ) y = acos ( x ) inverse cosine y = asin ( x ) inverse sine y = atan ( x ) inverse tangent y = ceil ( x ) round a real number up to an integer y = cos ( x ) y = exp ( x ) "e to x" function y = factorial ( n ) 1*2*3*...*n y = floor ( x ) round a real number down to an integer y = gcd ( m, n ) Greatest common divisor; gcd(30,24) is 6. y = humps ( x ) an example function to plot or minimize x = input ( 'Enter a value for x:' ) asks user to input value (for scripts) tf = isprime ( n ) true if integer n is a prime number. y = lcm ( m, n ) Least common multiple; lcm(6,20) is 60. y = log ( x ) y = log2 ( x ) y = log10 ( x ) z = max ( x, y ) z = min ( x, y ) r = mod ( x, y ) remainder from division x/y mod(32,5) is 2 m = nchoosek ( n, k ) the binomial coefficient "n choose k" y = rand ( ) random real 0 < x < 1. n = randi ( [ a, b ] ) random integer a <= i <= b. y = round ( x ) round a real number to the nearest integer. y = sin ( x ) y = sqrt ( x ) y = tan ( x ) Assignment statements: sqrt(x) computes the value, stores it as "ans", displays it. y = sqrt(x) computes the value, stores it as "y", displays it. y = sqrt(x); computes the value, stores it as "y". Logical operators: x < y x <= y x > y x >= y x == y x is equal to y Careful! TWO equal signs needed! x ~= y x is not equal to y x < y && y < z x is less than y AND y is less than z x < y || y < z x is less than y OR y is less than z ~ ( x == 0 && y == 0 ) it's NOT the case that both x and y are zero. Logical control: if ( x < y ) fprintf ( ' X is smaller than Y!\n' ); end if ( x <= y && y <= z ) disp ( ' Y is between X and Z.' ) end if ( 0 <= x ) y = sqrt ( x ); else y = sqrt ( -x ); end if ( 0 <= x ) pos = pos + 1; elseif ( x == 0 ) zero = zero + 1; else neg = neg + 1; end FOR loops, nested FOR loops, the BREAK statement: for i = 1 : n commands to repeat end Variations on the FOR statement include: for i = 10 : 9 (does nothing) for i = 10 : 10 10 for i = 10 : 20 10, 11, 12, ..., 19, 20 for i = 10 : 2 : 20 10, 12, 14, ..., 18, 20 for i = 20 : -1 : 10 20, 18, 16, ..., 12, 10 for i = 1 : 100 x = rand ( ); if ( 0.95 < x ) fprintf ( ' Jumping out on step %d\n', i ); break; <-- terminates loop early. end end for i = 1 : m for j = 1 : n fprintf ( ' %d', 10 * i + j ); end fprintf ( '\n' ); end WHILE loops: repeat "as long as" some condition is true while ( condition ) commands to carry out, because condition is true end sum = 0; k = 0; while ( sum < 100 ) k = k + 1; sum = sum + k; end A "do forever" version of WHILE lets you check conditions any time: while ( true ) commands to carry out; if ( condition ) break end other commands end Printing: x displays x (unformatted) x; no display y = x + 1 sets y, and displays y (unformatted) y = x + 1; sets y, no display fprintf ( ' The value of x is %f', x ); Prints fprintf ( ' The value of x is %f\n', x ); Prints, then starts new line. format long 16 digit real printout format short 6 digits real printout disp ( 'Cannot divide by zero!' ) Displays a string. fprintf ( 'Cannot divide by zero!\n' ); Also displays a string. disp ( '' ) Prints a blank line. fprintf ( '\n' ); Prints a blank line. disp ( 'Can''t divide by zero!' ) Type apostrophe TWICE in strings. fprintf ( ' 4 %% is a low rate ); Type percent TWICE in strings. fprintf() formats: %f print a real number with "fixed point" format. %10f print a real number, using 10 spaces %7.2f print a real number, using 7 spaces, with 2 decimal places %.2f print a real number, using 2 decimal places %e print a real number with "exponential" format (scientific notation). %g print a real number with "general" format (fixed or exponential). %d print the value as an integer %10d print the value as an integer, using 10 spaces Lists (a simple version of vectors); xlist = []; defines a list with nothing in it; xlist = [ xlist, x ]; adds the value x to the end of list xlist xlist = [ 1, 2, 3, 4 ]; makes a list with entries 1, 2, 3, and 4. Plots: clf clears the current graphics data, leaving a blank plot. plot ( xlist, ylist ) where xlist and ylist are lists of x and y data, of the same length. plot ( xlist1, ylist1, xlist2, ylist2 ) plots two sets of data on one graph. plot ( ylist ) plots the data in ylist, using X coordinates 1, 2, 3, ..., N. plot ( xlist, ylist, 'LineWidth', 3 ) plots the graph with a line that is 3 pixels wide (default is 1) plot ( xlist, ylist, 'r-' ) plots the graph using a RED line. Color options are 'r', 'g', 'b', 'c', 'm', 'y', 'k', 'w' red, green, blue, cyan, magenta, yellow, black, white. plot ( xlist, ylist, 'b*' ) plots the data using BLUE stars, but no lines connect the points. plot ( xlist, ylist, 'go-' ) plots the data using a GREEN line, with circles at data points. grid on will add grid lines title ( 'YOUR TITLE HERE' ) puts a title above the plot xlabel ( 'X Label' ) puts a label along the X axis ylabel ( 'Y Label' ) puts a label along the Y axis hold on allows you to issue more plot() commands, whose results will appear on the same graph hold off stops using the current graph for plot output. The next plot() command will appear without any old data. axis off Don't show the x and y axis and the box, usually surrounding the graph. axis equal Use the same scale for the x and y axes. Useful if you want to draw a circle, or you want a sine curve plot to look right. print ( '-djpeg', 'name.jpg' ) saves a copy of the current plot as a JPEG file. print ( '-dpng', 'name.png' ) saves a copy of the current plot as a PNG file. Polygons: fill ( xlist, ylist, color ) draws the shape outlined by the points in xlist and ylist, and fills it with the given color. COLOR can be 'r', 'g', 'b', 'c', 'm', 'y', 'k', 'w'. COLOR can be a list of three RGB values, each between 0 and 1. orange = [ 1.0, 0.4, 0.0 ]; maroon = [ 0.4, 0.0, 0.0 ]; plot ( xlist, ylist ) can be used to draw a polygon. However, you have to add a copy of the first x value to the end of the xlist, and the first y value to the end of the y list, in order for the polygon to be drawn completely. Bar plota: bar ( ylist ) will create a bar graph of the y values in ylist. The y values will have x "coordinates" 1, 2, 3, ... bar ( xlist, ylist ) will create a bar graph where a bar of height y is drawn at coordinate x, for each pair if x and y values in xlist and ylist. Sequences defined by a formula: * Sequences are represented as a(i), where i is an index (an integer) * Mathematical sequences might start at a(0) or a(1). * Print sequence elements 0 through n: for i = 0 : n value = FORMULA based on i; fprintf ( ' %d', value ); end fprintf ( '\n' ); * Find first element for which CONDITION is true: i = 0; while ( true ) value = FORMULA based on i; if ( CONDITION ) break; end i = i + 1; end Sequences define by "stepping stones" (recurrence): * Print sequence elements 0 through n: for i = 0 : n if ( i == 0 ) value = INITIAL VALUE else value_old = value; value = FORMULA based on value_old; end fprintf ( ' %d', value ); end fprintf ( '\n' ); * Find first element for which CONDITION is true: i = 0; while ( true ) if ( i == 0 ) value = INITIAL VALUE else value_old = value; value = FORMULA based on value_old; end if ( CONDITION ) break; end i = i + 1; end Sequences define by TWO "stepping stones": * Print sequence elements 0 through n: for i = 0 : n if ( i == 0 ) value = INITIAL VALUE #1 elseif ( i == 1 ) value_old = value value = INITIAL VALUE #2 else value_oldest = value_old value_old = value; value = FORMULA based on value_old and value_oldest; end fprintf ( ' %d', value ); end fprintf ( '\n' ); * Find first element for which CONDITION is true: i = 0; while ( true ) if ( i == 0 ) value = INITIAL VALUE #1 elseif ( i == 1 ) value_old = value value = INITIAL VALUE #2 else value_oldest = value_old value_old = value; value = FORMULA based on value_old and value_oldest; end if ( CONDITION ) break; end i = i + 1; end Miscellaneous: error ( 'Message' ) print message, terminate script. cd .. move MATLAB's current directory up one level cd dirname move MATLAB's current directory to "dirname" clear clears MATLAB's memory; all variable values are lost. clc clear the command window. dir list the files in the current directory doc sin extensive help on "sin" function exit exit MATLAB completely (lose unsaved work!) help sin brief help on "sin" function ls list the files in the current directory pause MATLAB will wait until you hit RETURN before carrying out the next command. pwd what is the full name of MATLAB's current directory? quit exit MATLAB completely (lose unsaved work!) type filename type the contents of the file "filename" % begin a comment ... continue this text on a new line Ctrl-C force MATLAB to stop the current computation. Parentheses: A + B * C ---------- must be written ( a + b * c ) / ( d + e ) D + E F + G ----- must be written ( f + g ) / ( 2 * h ) 2 H 2 1 - ( x - y ) - e must be written ( 1 / 2 ) * exp ( - ( ( x - y )^2 ) ) 2 2 sin pi x must be written ( sin ( pi * x ) ) ^ 2