MATLAB An Overview of MATLAB 1 An Overview
MATLAB 입문 An Overview of MATLAB 1
An Overview of MATLAB Starting Matlab The default MATLAB Desktop. 2
An Overview of MATLAB Operations (1/2) Symbol Operation MATLAB form ^ exponentiation: a^b * multiplication: a*b / right division: a/b left division: ab + addition: a+b - subtraction: a-b Scalar arithmetic operations 3
An Overview of MATLAB Operations (2/2) 4
An Overview of MATLAB assignment , workspace, and command n The sign ‘=‘ is called the ‘assignment or replacement’ operator. e. g. x = 3 : assign the value 3 to the variable x x = x + 2 : add 2 to the current value of x ¨ n n Workspace : the names and values of any variables in use in the current work session Commands for managing the work session clc clears the Command window clear Removes all variables from memory exist(‘name’) Determines if a file or variable exists having the name ’name’ quit Stops MATLAB who Lists the variable currently in memory whos Lists the current variables and sizes, and indicates if they have imaginary parts : Colon; generates and array having regularly spaced elements , Comma; separates elements of an array ; Semicolon; suppresses screen printing; also denotes a new row in an array … Ellipsis; continues a line 5
An Overview of MATLAB recall feature, and special variable n recall feature recall a previously typed function or variable ¨ e. g. up-arrow key(↑) & down-arrow key(↓): move up and down through the previously typed lines one line at a time. Tab key: automatically completes the name of a function, variable, or file if you type the first few letters of the name and press the Tab key. ¨ n Special variables and constants ans Temporary variable containing the most recent answer eps Specifies the accuracy of floating point precision i, j The imaginary unit Infinity Na. N Indicates an undefined numerical result pi The number π 6
An Overview of MATLAB Complex Number Operations 7
An Overview of MATLAB Formatting Commands n The Format command controls how numbers appear on the screen. format short Four decimal digits (the default); 13. 6745 format long 16 digits; 17. 27484029463547 format short e Five digits (four decimals) plus exponent; 6. 3793 e+03 format long e 16 digits (15 decimals) plus exponent; 6. 379243784781294 e-04 format bank Two decimal digits; 126. 73 format + Positive, negative, or zero; + format rat Rational approximation; 43/7 format compact Suppresses some line feeds format loose Resets to less compact display mode 8
An Overview of MATLAB Array(1/2) n n n One of the strengths of MATLAB is its ability to handle collections of numbers, called arrays. A numerical array is an ordered collection of numbers We can use square brackets e. g. >> x = [0, 1, 3, 6] ¨ n You need not type all the numbers in the array if they are regularly spaced e. g. >> u = [0: 0. 1: 10] u = [0, 0. 1, 0. 2, 0. 3, … , 9. 8, 9. 9, 10] ¨ 9
An Overview of MATLAB Array(2/2) n You can compute ‘w=5*sin (u)’ for ‘u=[0: 0. 1: 10]’ >> u=[0: 0. 1: 10]; >> w=5*sin(u); computed the formula ‘w=5*sin(u)’ 101 times. n array index: points to a particular element in the array >>u(7) ans = 0. 6000 >>w(7) ans = 2. 8232 n length: determine how many values are in an array >>m = length(w) m= 101 10
An Overview of MATLAB Mathematical functions n Some commonly used mathematical functions Function MATLAB syntax(◈) exp(x) sqrt(x) log 10(x) cos(x) sin(x) tan(x) acos(x) asin(x) ◈The MATLAB trigonometric functions use radian measure atan(x) 11
An Overview of MATLAB Working with Files n M-file: MATLAB function files and program files are saved with the extension. m, and called M-files. n MAT-file: save the names and values of variables. n ASCII-file: files written in a specific format designed to make them usable to a wide variety of software. 12
An Overview of MATLAB System, directory, and file commands n system, directory, and file commands addpath dirname Adds the directory dirname to the search path. cd dirname Changes the current directory to dirname. dir Lists all files in the current directory. dirname Lists all the files in the directory dirname. path Displays the MATLAB search pathtool Starts the Set Path tool. pwd Displays the current directory. rmpath dirname Removes the directory dirname from the search path. what Lists the MATLAB-specific files found in the current working directory. Most data files and other non-MATLAB files are not listed. Use dir to get a list of all files. what dirname Lists the MATLAB-specific files in directory dirname. 13
An Overview of MATLAB Plotting commands n Some MATLAB plotting commands [x , y] = ginput(n) Enables the mouse to get n points from a plot, and returns the x and y coordinates in the vectors x and y, which have a length n. grid puts grid lines on the plot. gtext (‘text’) Enables placement of text with the mouse. plot (x , y) Generates a plot of the array y versus the array x on rectilinear axes. title (‘text’) Puts text in a title at the top of the plot. xlabel (‘text’) Adds a text label to the horizontal axis (the abscissa). ylabel (‘text’) Adds a text label to the vertical axis (the ordinate). 14
An Overview of MATLAB Plotting with MATLAB 15
An Overview of MATLAB Linear Algebra Equations n the left division operator() ¨ e. g. 16
An Overview of MATLAB Statistics, Calculus, and Processing n Statistics ¨ perform statistical calculations and other types of data manipulation. n Numerical Calculus, Differential Equations, and Simulink ¨ MATLAB can numerically compute the derivative and the integral n Symbolic Processing ¨ obtain the derivative and the integral formula instead of as a set of numerical values) in symbolic form (a 17
An Overview of MATLAB Script Files and the Editor/Debugger(1/2) n n Two ways for performing operations in MATLAB ¨ Interactive mode : directly enter the commands in the Command window ¨ using script files (commands files) : store the commands in script files M-files ¨ script files : when need to use many commands or arrays with many elements ¨ function files : when need to repeat the operation of a set of commands 18
An Overview of MATLAB Script Files and the Editor/Debugger(2/2) n M-file 생성 및 저장 19
An Overview of MATLAB Input/output commands disp (A) Displays the contents, but not the name, of the array A. disp (‘text’) Displays the text string enclosed within single quotes. format Controls the screen’s output display format fprintf Performs formatted writes to the screen or to a file x = input (‘text’) Displays the text in quotes, waits for user input from the keyboard, and stores the value in x. x = input (‘text’ , ’s’) Displays the text in quotes, waits for user input from the keyboard, and stores the input as a string in x k = menu (‘title’, ’option 1’, ’option 2’, …) Displays a menu whose title is in the string variable ‘title’, and whose choices are ‘option 1’, ’option 2’, and so on. 20
An Overview of MATLAB The MATLAB Help System(1/4) n Help Browser Graphical user interface ¨ find information, view online documentation ¨ n Help Functions help, lookfor, doc ¨ display syntax information for specified function ¨ n Other Resources run demos, contact technical support, participate in a newsgroup. ¨ The Math. Works Website n the home of MATLAB. n http: //www. mathworks. com ¨ 21
An Overview of MATLAB The MATLAB Help System(2/4) - Help Browser n The MATLAB Help Browser § Contents: a contents listing tab § Index: a global index tab § Search: a search tab having a find function and full text search features § Demos: a bookmaking tab to start built-in demonstrations 22
An Overview of MATLAB The MATLAB Help System(3/4) - Help Functions n MATLAB Help functions (help, lookfor, doc) Function Use doc Displays the start page of the documentation in the Help Browser doc function Displays the documentation for the MATLAB function doc toolbox/function Displays the documentation for the specified toolbox function doc toolbox Displays the documentation road map page for the specified toolbox help Displays a list all the function directories, with a description of the function category each represents. help function Displays in the Command window a description of the specified function helpwin topic Displays the help text for the specified topic inside the desktop Help Browser window lookfor topic Displays in the Command window a brief description for all functions whose description includes the specified keyword topic type filename Displays the M-filename without opening it with a text editor. 23
An Overview of MATLAB The MATLAB Help System(4/4) - Help Functions n Examples 24
An Overview of MATLAB Programming in MATLAB(1/5) n Relational operators to make comparisons ¨ < , <=, >. >=, ==, ~= ¨ n Conditional statements to write programs that make decisions ¨ if, elseif ¨ n Loops a structure for reputation a calculation a number of times ¨ for, while ¨ 25
An Overview of MATLAB Programming in MATLAB(2/5) n Relational Operators Relational operator Meaning < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to ~= Not equal to >> x = [6, 3, 9]; y = [14, 2, 9]; >> z = (x < y) z= 1 0 0 >> z = (x > y) z= 0 1 0 >> z = (x == y) z= 0 0 1 >> z = (x ~= y) z= 1 1 0 >> z = (x > 8) z= 0 0 1 26
An Overview of MATLAB Programming in MATLAB(3/5) n The find Function find(x) : computes an array containing the indices of the nonzero elements of the numeric array x ¨ the find function returns the indices, not the values. ¨ <example 1> <example 2> >> x = [-2, 0, 4]; >> y = find(x) y= 1 3 >> x = [6, 3, 9, 11]; y = [14, 2, 9, 13] >> values = x(x<y) values = 6 11 >>how_many = length(values) how_many = 2 >>indices = find(x<y) indices = 1 4 27
An Overview of MATLAB Programming in MATLAB(4/5) n Conditional Statements contain one or more of the if, else, and elseif ¨ The end statement denotes the end of a conditional statement ¨ The else and elseif statements may be omitted if not required. ¨ if expression commands else commands end <example> if x >= 9 y = 15*sqrt(4 x) + 10 elseif x >= 0 y = 10*x + 10 else y = 10 end 28
An Overview of MATLAB Programming in MATLAB(5/5) n Loops repeat a calculation a number of times ¨ for loop : the number of passes is known ahead of time ¨ while loop : the looping process must terminate when a specified condition is satisfied ¨ <example of a for loop> m = 0; x(1) = 10; for k = 2: 3: 11 m = m+1; x(m+1) = x(m) + k^2; end <example of a while loop> x = 5; k = 0; while x < 25 k = k+1; y(k) = 3*x; x = 2*x-1; end x(1) = 14, x(2) = 39, x(3) = 103, x(4) = 224 y(1) = 15, y(2) = 27, y(3) = 51 29
- Slides: 29