1 / 107

主讲教师: 胡章芳 E-Mail: huzf@cqupt

MATLAB 及其工程应用 MATLAB and It’s Engineering Application. 主讲教师: 胡章芳 E-Mail: huzf@cqupt.edu.cn. Chapter 2 MATLAB Basics. 2.1 Variables and Arrays 2.2 Initializing Variables in MATLAB 2.3 Multidimensional Arrays 2.4 Subarrays 2.5 Special Values 2.6 Displaying Output Data

njuanita
Télécharger la présentation

主讲教师: 胡章芳 E-Mail: huzf@cqupt

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. MATLAB及其工程应用MATLAB and It’s Engineering Application 主讲教师: 胡章芳 E-Mail: huzf@cqupt.edu.cn

  2. Chapter 2 MATLAB Basics • 2.1 Variables and Arrays • 2.2 Initializing Variables in MATLAB • 2.3 Multidimensional Arrays • 2.4 Subarrays • 2.5 Special Values • 2.6 Displaying Output Data • 2.7 Data Files • 2.8 Scalar and Array Operations • 2.9 Hierarchy of Operations • 2.10 Built-in MATLAB Functions • 2.11 Introduction to Plotting • 2.12 Examples • 2.13 Debugging MATLAB Programs • 2.14 Summary All sections about Plotting will be introduced in a separated chapter.

  3. Introduction In this chapter, we will introduce more basic elements of the MATLAB language. By the end of the chapter, you will be able to write simple but functional MATLAB programs.

  4. Review • A program can be input • Typing commands using the command line (lines starting with “»” on the MATLAB desktop) • As a series of commands using a file(a special file calledM-file) • If a command is followed by a semicolon (;), result of the computation is not shown on the command window

  5. 2.1 Variables and Arrays • The fundamental unit of data is array An array is a collection of data values organized into rows and columns. scalar value vector matrix

  6. 2.1 Variables and Arrays • Data values within an array are accessed by including the name of the array followed by subscripts in parentheses. arrayarr Row1 The shaded element in this array would be addressed as arr(3, 2). Row2 Row 3 Row4 Col3 Col4 Col5 Col1 Col2

  7. A(2, 1) = 3, and C(2) = 2

  8. 2.1 Variables and Arrays • Arrays can be classified as either vectors or matrices. • The term “vector” is usually used to describe an array with only one dimension; • The term “matrix” is usually used to describe an array with two or more dimensions. • The size of an array:

  9. 2.1 Variables and Arrays • Variableis a name given to a reserved location in memory • class_code = 111; • number_of_students = 65; • name = ‘Chongqing University of Posts and Telecommunications'; • radius = 5; • area = pi * radius^2;

  10. 2.1 Variables and Arrays • MATLAB variable names • must begin with a letter • can contain any combination of letters, numbers and underscore (_) • must be unique in the first 31 characters • MATLAB is case sensitive “name”, “Name” and “NAME” are considered different variables • Never use a variable with the same name as a MATLAB command • Naming convention: use lowercase letters

  11. 2.1 Variables and Arrays • Use meaningful names for variables • number_of_students • exchange_rate

  12. 2.1 Variables and Arrays • data dictionary: • The most common types of MATLAB variables are double and char: • Double: consist of scalars or array of 64-bit double-precision floating-point numbers.10-308~10308 • Char:consist of scalars or array of 16-bit values.

  13. 2.1 Variables and Arrays • MATLAB: weakly typed language • C: strongly typed language

  14. 2.2 Initializing Variables • Three common ways: 1.Initialization using assignment statements; 2.Input data from the keyboard; 3.Read data from a file.

  15. 2.2 Initializing Variables • 2.2.1 Initialization using assignment statements • General form: var = expression • x = 5 x = 5 • y = x + 1 y = 6 • vector = [ 1 2 3 4 ] vector = 1 2 3 4

  16. 2.2 Initializing Variables • matrix = [ 1 2 3; 4 5 6 ] matrix = 1 2 3 4 5 6 • matrix = [ 1 2 3; 4 5 ] ??? Error • a = [ 5 (2+4) ] a = 5 6

  17. 2.2 Initializing Variables • 2.2.2 Initialization using shortcut statements • colon operator first:increment:last • x = 1:2:10 x = 1 3 5 7 9 • y = 0:0.1:0.5 y = 0 0.1 0.2 0.3 0.4 0.5

  18. 2.2 Initializing Variables • Transpose operator ' • u = [ 1:3 ]' u = 1 2 3 • v = [ u u ] v = 1 1 2 2 3 3 • v = [ u'; u' ] v = 1 2 3 1 2 3

  19. 2.2 Initializing Variables 2.2.3 Initialization using built-in functions • zeros() • x = zeros(2) x = 0 0 0 0 • z = zeros(2,3) z = 0 0 0 0 0 0 • ones(), size(), length() • y = zeros(1,4) y = 0 0 0 0 • t = zeros( size(z) ) t = 0 0 0 0 0 0

  20. 2.2 Initializing Variables 2.2.4 Initialization using keyboard input • input() • value = input( 'Enter an value: ' ) Enter an value: 1.25 value = 1.2500 • name = input( 'What is your name: ', 's' ) What is your name: Selim name =Selim

  21. 2.3 Multidimensional Arrays • Initialization • c(:,:,1)=[1 2 3; 4 5 6]; c(:,:,2)=[7 8 9; 10 11 12]; • whos Name Size Bytes Class c 2x3x2 96 double array • c c(:,:,1) = 1 2 3 4 5 6 c(:,:,2) = 7 8 9 10 11 12

  22. 2.3 Multidimensional Arrays • Storing in Memory • Two-dimensional : Column major order • Multidimensional Arrays : array subscript incremented first, second, third, … (1,1,1), (2,1,1), (1,2,1), (2,2,1), (1,1,2), (2,1,2), (1,2,2), (2,2,2) • Accessing Multidimensional arrays with a Single Subscript • The elements will be accessed in the order in which they were allocated in memory • c(5)

  23. 2.4 Subarrays • Array indices start from 1 • x = [ -2 0 9 1 4 ]; • x(2) ans = 0 • x(4) ans = 1 • x(8) ??? Error • x(-1) ??? Error

  24. 2.4 Subarrays y(1) y(2) y(3) y(4) y(5) y(6) y(1,1) y(2,1) y(1,2) y(2,2) y(1,3) y(2,3) • y = [ 1 2 3; 4 5 6 ]; • y(1,2) ans = 2 • y(2,1) ans = 4 • y(2) ans = 4 (column major order)

  25. 2.4 Subarrays • y = [ 1 2 3; 4 5 6 ]; • y(1,:) ans = 1 2 3 • y(:,2) ans = 2 5 • y(2,1:2) ans = 4 5 • y(1,2:end) ans = 2 3 • y(:,2:end) ans = 2 3 5 6

  26. 2.4 Subarrays • x = [ -2 0 9 1 4 ]; • x(2) = 5 x = -2 5 9 1 4 • x(4) = x(1) x = -2 5 9 -2 4 • x(8) = -1 x = -2 5 9 -2 4 0 0 -1

  27. 2.4 Subarrays • y = [ 1 2 3; 4 5 6 ]; • y(1,2) = -5 y = 1 -5 3 4 5 6 • y(2,1) = 0 y = 1 -5 3 0 5 6 • y(1,2:end) = [ -1 9 ] y = 1 -1 9 0 5 6

  28. 2.4 Subarrays • y = [ 1 2 3; 4 5 6; 7 8 9 ]; • y(2:end,2:end) = 0 y = 1 2 3 4 0 0 7 0 0 • y(2:end,2:end) = [ -1 5 ] ??? Error • y(2,[1 3]) = -2 y = 1 2 3 -2 0 -2 7 0 0

  29. 2.5 Special Values • pi:  value up to 15 significant digits • i, j: sqrt(-1) • Inf: infinity (such as division by 0) • NaN: Not-a-Number (such as division of zero by zero) • clock: current date and time as a vector • date: current date as a string (e.g. 20-Feb-2008) • eps: epsilon • ans: default variable for answers

  30. 2.6 Displaying Output Data • 2.6.1 Changing the data format • value = 12.345678901234567 format short  12.3457 long 12.34567890123457 short e  1.2346e+001 long e  1.234567890123457e+001 rat  1000/81 compact loose

  31. 2.6 Displaying Output Data • 2.6.2 The disp( array ) function • disp( 'Hello' ); Hello • disp(5); 5 • disp( [ ‘重庆邮电大学’ ] ); 重庆邮电大学 • name = 'Selim'; • disp( [ 'Hello ' name ] ); Hello Selim

  32. 2.6 Displaying Output Data 2.6.2 Thenum2str()andint2str()functions • d = [ num2str(25) '-Feb-' num2str(2008) ]; • disp(d); 25-Feb-2008 • x = 23.11; • disp( [ 'answer = ' num2str(x) ] ); answer = 23.11 • disp( [ 'answer = ' int2str(x) ] ); answer = 23

  33. 2.6 Displaying Output Data 2.6.3 The fprintf( format, data )function • %d integer • %f floating point format • %e exponential format • \n skip to a new line • \g either floating point format or exponential format,whichever is shorter. Limitation:itonly displays the real portion of a complex value.

  34. 2.6 Displaying Output Data • fprintf( 'Result is %d', 3 ); Result is 3 • fprintf( 'Area of a circle with radius %d is %f', 3, pi*3^2 ); Area of a circle with radius 3 is 28.274334 • x = 5; • fprintf( 'x = %3d', x ); x = 5 • x = pi; • fprintf( 'x = %0.2f', x ); x = 3.14 • fprintf( 'x = %6.2f', x ); x = 3.14 • fprintf( 'x = %d\ny = %d\n', 3, 13 ); x = 3y = 13

  35. 2.7 Data Files • savefilename var1 var2… • save homework.mat x y  binary Including: name,type,size,value But annot be read by other programs • save x.dat x –ascii  ascii But name and type are lost; and the resulting data file will be much larger

  36. 2.7 Data Files • loadfilename • load filename.mat  binary • load x.dat –ascii  ascii

  37. 2.8 Scalar & Array Operations • variable_name = expression; • addition a + b a + b • subtraction a - b a - b • multiplication a x b a * b • division a / b a / b • exponent aba ^ b

  38. Scalar-matrix operations a = [ 1 2; 3 4 ] a = 1 2 3 4 2 * a ans = 2 4 6 8 a + 4 ans = 5 6 7 8 Element-by-element operations a = [ 1 0; 2 1 ]; b = [ -1 2; 0 1 ]; a + b ans = 0 2 2 2 a .* b ans = -1 0 0 1 2.8.1 Scalar & Array Operations

  39. 2.8.2 Array & Matrix Operations • Matrix multiplication: C = A * B • If • A is a p-by-q matrix • B is a q-by-r matrix then • C will be a p-by-r matrix where

  40. 2.8.2 Array & Matrix Operations • Matrix multiplication: C = A * B

  41. Examples a = [ 1 2; 3 4 ] a = 1 2 3 4 b = [ -1 3; 2 -1 ] b = -1 3 2 -1 a .* b ans = -1 6 6 -4 a * b ans = 3 1 5 5 2.8.2 Array & Matrix Operations

  42. Examples a = [ 1 4 2; 5 7 3; 9 1 6 ] a = 1 4 2 5 7 3 9 1 6 b = [ 6 1; 2 5; 7 3 ] b = 6 1 2 5 7 3 c = a * b c = 28 27 65 49 98 32 d = b * a ??? Error using ==> *Inner matrix dimensions must agree. 2.8.2 Array & Matrix Operations

  43. 2.8.2 Array & Matrix Operations • Identity matrix: I • A * I = I * A = A • Examples • a = [ 1 4 2; 5 7 3; 9 1 6 ]; • I = eye(3) I = 1 0 0 0 1 0 0 0 1 • a * I ans = 1 4 2 5 7 3 9 1 6

  44. 2.8.2 Array & Matrix Operations • Inverse of a matrix: A-1 • AA-1 = A-1A = I • Examples • a = [ 1 4 2; 5 7 3; 9 1 6 ]; • b = inv(a) b = -0.4382 0.2472 0.0225 0.0337 0.1348 -0.0787 0.6517 -0.3933 0.1461 • a * b ans = 1.0000 0 0 0.0000 1.0000 0 0 -0.0000 1.0000

  45. 2.8.2 Array & Matrix Operations • Matrix left division: C = A \ B • Used to solve the matrix equationAX = B where X = A-1B • In MATLAB, you can write • x = inv(a) * b • x = a \ b (second version is recommended)

  46. 2.8.2 Array & Matrix Operations • Example: Solving a system of linear equations • A = [ 4 -2 6; 2 8 2; 6 10 3 ]; • B = [ 8 4 0 ]'; • X = A \ B X = -1.8049 0.2927 2.6341

  47. 2.8.2 Array & Matrix Operations • Matrix right division:C = A / B • Used to solve the matrix equationXA = BwhereX = BA-1 • In MATLAB, you can write • x = b * inv(a) • x = b / a (second version is recommended)

  48. 2.9 Hierarchy of Operations • x = 3 * 2 + 6 / 2 • x = ? • Processing order of operations is important • parenthesis (starting from the innermost) • exponentials (left to right) • multiplications and divisions (left to right) • additions and subtractions (left to right) • x = 3 * 2 + 6 / 2 x = 9

  49. 2.9 Hierarchy of Operations • Example p49(2.2) • You should always ask yourself: • will I easily understand this expression if I come back to it in six months? • Can another programmer look at my code and easily understand what I am doing? • Use extra parentheses • (2+4)/2

  50. 2.10 Built-in Functions • General form: result = function_name( input ); • abs, sign • log, log10, log2 • exp • sqrt • sin, cos, tan • asin, acos, atan • max, min • round, floor, ceil, fix • mod, rem • helpfunction_name

More Related