1 / 106

CSE 551 Computational Methods 2018/2019 Fall Chapter 2 MATLAB Basics

CSE 551 Computational Methods 2018/2019 Fall Chapter 2 MATLAB Basics. Outline. Introduction MATLAB as a Calculator Variables and Assignment Statement Arrays Subarrays Special Values Displaying Output Data Data Files Arrays and Matrix Operations Build-in MATLAB Functions

johnsonj
Télécharger la présentation

CSE 551 Computational Methods 2018/2019 Fall Chapter 2 MATLAB Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 551 Computational Methods 2018/2019 Fall Chapter 2 MATLAB Basics

  2. Outline Introduction MATLAB as a Calculator Variables and Assignment Statement Arrays Subarrays Special Values Displaying Output Data Data Files Arrays and Matrix Operations Build-in MATLAB Functions Debugging MATLAB Programs

  3. Introduction • MATLAB (short for MATrix LABoratory) • special-purpose computer program • optimized to perform engineering and scientific calculations. • started to perform matrix mathematics • over the years - a flexible computing system The • The MATLAB program implements the MATLAB programming language • a very extensive library of predefined functions to make technical programming tasks easier and more efficient

  4. References • Chapman S. J., MATLAB Programming for Engineers, 5th ed., CENGAGE Learning • Attaway S., MATLAB:A Practical Introduction to Programming and Problem Solving, 4th ed., ELSEVIER BH

  5. Advantages and Disadvantages of MATLAB • Advantages: • Ease of Use • Platform Independence • Predefined Functions • Device-Independent Plotting • Graphical User Interface • MATLAB Compiler: divice independent p-code • Disadvantages: • an interpreated language • cost

  6. The MATLAB Environment • several types of windows: • Command Windows: commands may be entered; Figure Windows: display plots and graphs • Edit Windows: permit a user to create and modify MATLAB programs.

  7. The default MATLAB desktop. The exact appearance of the desktop may differ slightly on different types of computers.

  8. Tools within MATLAB Dasktop • The major tools within or accessible from the MATLAB desktop are • The Command Window • The Command History Window • The Start Button • The Documents Window, including the Editor/Debugger and the Array Editor • Figure Windows • Workspace Browser • Help Browser • Path Browser

  9. MATLAB as a Calculator • MATLAB can be used as a calculator to perform mathematical calculations • typed directly into the Command Window • using the symbols +, –, *, /, and ^ (exponentiation) • e.g., to calculate the volume of a cylinder of radius r and • length l • The area of the circle at the base of the cylinder is given by A =r 2 • and the total volume of the cylinder: V =Al • If the radius is 0.1 m and the length is 0.5 m,

  10. » A = pi * 0.1^2 A = 0.0314 » V = A * 0.5 V = 0.0157 • Note that pi is predefined to be the value 3.141592 . . .

  11. Variables and Assignment Statement • variable - store a value • assignment statement variablename = expression >> mynum = 6 mynum = 6 >>

  12. suppressing the output • Putting a semicolon at the end of a statement suppresses the output. >> res = 9 – 2; >> • it just doesn’t show that result. Instead, another prompt appears immediately. • However, at this point in the Workspace Window the variables mynum and res can be seen

  13. Default variable ans • MATLAB uses a default variable named ans • if an expression is typed at the prompt and not assigned to a variable • e.g., the result of the expression 6+3 is stored in the variable ans: >> 6 + 3 ans = 9 • This default variable is reused any time just an expression is typed at the prompt.

  14. Variable Names • variables – identifiers • Rules for identgifier names: • 1 - must begin with a letter After that can contain letters, digits, and the underscore character (e.g., value_1), • but it cannot have a space • 2 - There is a limit to the length of the name; the built-in function namelengthmax tells how many characters this is • 3 – MATLAB is case sensitive

  15. Variable Names (cont.) • 4 - reserved words that cannot be used as variable name • 5 - Names of built-in functions can, but should not, be used as variable names • 6 – Conventions • i – use underscore: exchange_rate • ii – Java, C++ convension: exchangeRate

  16. Arrays • fundamental unit of data – array • collection of data values organized into rows and columns and known by a single name • Individual data values within an array may be accessed

  17. Vectors ands Matices • Individual data values within an array accessed by • name of the array followed by subscripts in parentheses (row,column) • scalars – arrays - one row and one column • Arrays - vectors or matrices. • “vector” - an array with only one dimension • “matrix” - an array with two or more dimensions. • The size of an array - # of rows and # of columns # of rows mentioned first

  18. Examples

  19. Vectors • Individual array elements are addressed by the array name followed by the • row and column of the element • for row or column vectors - only one subscript is required • e.g., in the preceding arrays a(2,1) is 3 • and c(2) = 2. • A MATLAB variable - region of memory containing an array refered by a user-specified name • contents of the array may be used or modified

  20. Common Types • common types of MATLAB variables - double and char. • double - scalars or arrays of 64-bit double-precision floating-point numbers • can hold real, imaginary, or complex values. • in the range to 10-308 10308 with 15 to 16 significant decimal digits of accuracy. • double - the principal numerical data type.

  21. double Type • variable type double automatically created whenever a numerical value is assigned to a variable name • real, imaginary, or complex var = 10.5 • imaginary number - appending the letter i or j to a number • 10i and –4j var = 4i • complex value var = 10 + 10i

  22. char Type • Variables of type char consist of scalars or arrays of 16-bit values, each • representing a single character. • Arrays of this type are used to hold character strings. • e.g., • comment will be a character array. comment = 'This is a character string'

  23. Strongly and Weakly typed languages • In a strongly typed language, the type of every variable must be explicitly declared before it is used. • MATLAB - weakly typed language • Variables may be created at any time by simply assigning values to them, • the type of data assigned to the variable determines the type of variable

  24. MATLAB variables are automatically created when they are initialized. : • 1. Assign data to the variable in an assignment statement. • 2. Input data into the variable from the keyboard. • 3. Read data from a file.

  25. assign it one or more values • assignment statement. var = expression; • expression: • scalar constant • an array • combination of constants, other variables, and mathematical operations var = 40i; var2 = var/5; x = 1; y = 2; array = [1 2 3 4];

  26. variables can be initialized with arrays of data. • constructed using brackets ([]) and semicolons. • All of the elements • of an array are listed in row order. • the values in each row are listed • from left to right, with the topmost row first and the bottommost row last. Individual • values within a row - separated by blank spaces or commas • the rows - separated by semicolons or new lines.

  27. Clearing Variables

  28. The number of elements in every row must be the same • the number of elements in every column must be the same. • An expression such as [1 2 3; 4 5]; • is illegal

  29. The expressions used to initialize arrays can include algebraic operations and all of or portions of previously defined arrays • e.g., a = [0 1+7]; b = [a(2) 7 a]; • will define an array a [0 8] • b [8 7 0 8].

  30. not all of the elements in an array must be defined when it is created. • If a specific array element is defined and one or more of the elements before it are not • then the earlier elements will automatically be created and initialized to zero • e.g., c(2,3) = 5; • will produce the matrix c =

  31. an array can be extended by specifying a value for an element beyond the currently defined size. • suppose that array d [1 2] • the statement d(4) = 4; • will produce the array d [1 2 0 4]

  32. Initilizing with Shortcut Expressions • for large arrays: • the colon operator. specifies a whole series of values • The general form of a colon operator is • first:incr:last • first: first value in the series • incr: stepping increment • last: last value in the series. • If the increment is one, it may be omitted • generate an array containing the values • first, first+incr, first+2*incr, first+3*incr, and so forth as long as the values are less than or equal to last. • The list stops when the next value in the series is greater than the value of last.

  33. An Example • the expression 1:2:10 - shortcut for a 1 x 5 row vector containing the values 1, 3, 5, 7, and 9. • The next value in the series would be 11, which • is greater than 10, so the series terminates at 9. » x = 1:2:10 x = 1 3 5 7 9

  34. Transpose Operator • Shortcut expressions can be combined with the transpose operator (') • to initialize column vectors and more complex matrices. • The transpose operator • swaps the row and columns of any array that it is applied to. • Thus the expression f = [1:4]'; • generates a 4-element row vector [1 2 3 4] • then transposes it into the • 4-element column vector f.

  35. Example • the expressions g = 1:4; h = [g' g']; • will produce the matrix h

  36. Initilizing with Build-in Functions • zeros function - create an all-zero array of any size. • single scalar argument - a square array • two scalar arguments – • first: # of rows, second: # of columns • size function returns two values: • # of rows and columns in an array • combined with the zeros function • ones function - arrays containing all ones • eye function - arrays containing identity matrices

  37. Examples a = zeros(2); b = zeros(2,3); c = [1 2; 3 4]; d = zeros(size(c)); • These statements generate the following arrays:

  38. list of common MATLAB functions for initializing variables.

  39. Initilizing Variables with Keybord Input • input function • displays a prompt string in the Command Window • and then waits for the user to type in a response. my_val = input('Enter an input value:'); • If enters a single number - be typed in • If enters an array – enclosed in brackets • If only the return key is entered - an empty • matrix

  40. Multidimensional Arrays • One dimensional arrays - series of values laid out • row or column – • single subscript - select individual array elements • MATLAB - arrays with as many dimensions as necessary • one subscript for each dimension,

  41. Example » c(:,:,1)=[1 2 3; 4 5 6]; » c(:,:,2)=[7 8 9; 10 11 12]; » whos c Name Size Bytes Class Attributes c 2x3x2 96 double » c c(:,:,1) = 1 2 3 4 5 6 c(:,:,2) = 7 8 9 10 11 12

  42. Storing Arrays in Memory • A two-dimensional array m rows, n columns • m x n successive locations in the computer’s • memory • MATLAB always allocates array elements in column major order. • allocates the first column in memory • then the second, then the third, and so on., • e.g., • element a(1,2) - fifth element allocated in memory • single-subscript addressing • low-level I/O functions

  43. Data values for array a. (b) Layout of values in memory for array a.

  44. Storing Arrays with More Then TWo Dimensions • This same allocation scheme applies to arrays with more than two dimensions. • The first array subscript is incremented most rapidly, • the second subscript is incremented less rapidly, and so on, and the last subscript in incremented • most slowly. • e.g., in a 2 x 2 x 2 array, the elements would be • allocated in the following order: (1,1,1), (2,1,1), (1,2,1), (2,2,1), (1,1,2), (2,1,2), • (1,2,2), (2,2,2)

  45. Accessing Multidimensional Arrays with One Dimension • MATLAB permit treat • a multidimensional array - a one-dimensional array • length = # of elements in the multidimensional array. • If a MDA addressed with a single dimension, then the elements accessed: • in the order in which they were allocated in memory.

  46. Example • a :4 x 3 » a = [1 2 3; 4 5 6; 7 8 9; 10 11 12] a = 1 2 3 4 5 6 7 8 9 10 11 12 • value of a(5) is 2 - value of element a(1,2), • a(1,2) allocated fifth in memory.

  47. Subarrays • possible • to select and use • subsets of MATLAB arrays as if separate arrays • To select a portion of an array • just include a list of all of the elements • to be selected in the parentheses after the array name. • e.g., array arr1: arr1 = [1.1 -2.2 3.3 -4.4 5.5]; • arr1(3): 3.3 • arr1([1 4]): array [1.1 -4.4] • arr1(1:2:5): array [1.1 3.3 5.5].

  48. Examples • For a two-dimensional array • a colon can be used in a subscript to select all • of the values of that subscript. • e.g., arr2 = [1 2 3; -2 -3 -4; 3 4 5]; • arr2(1,:): [1 2 3] • arr2(:,1:2:3):

More Related