【正文】
?1sin x?1tan x?1tan yx?Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 71 sin (x) Calculates sin x, with x in radians. tan (x) Calculates tan x, with x in radians. exp (x) Calculates . log (x) Calculates the natural logarithm . [value, index] = max (x) Returns the maximum value in vector x, and optionally the location of that value. [value, index] = min (x) Returns the minimum value in vector x, and optionally the location of that value. mod (x, y) Remainder, or modulo, function. sqrt (x) Calculates the square root of x. xeloge xChapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 72 Rounding Functions ceil (x) Rounds x to the nearest integer towards positive infinity: ceil () = 4 and ceil () = 3. fix (x) Rounds x to the nearest integer towards zero: fix () = 3 and fix () = 3. floor (x) Rounds x to the nearest integer towards minus infinity: floor () = 3 and floor() = 4. round (x) Rounds x to the nearest integer. Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 73 String Conversion Function char (x) Converts a matrix of number into a char string. For ASCII characters the matrix should contain numbers ≤ 127. double (x) Converts a character string into a matrix of numbers. int2str (x) Converts x into a character string representing the number as an integer. num2str (x) Converts x into a character string representing the number with a decimal point. str2num (s) Converts characters string s into a number. Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 74 Unlike most puter language, MATLAB functions can deal with plex input and produce plex result. sqrt (2) ans = 0 + Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 75 Introduction to Plotting Use plot function, create two vectors containing the x and y values to be plotted. x = 0 : 1 : 10。 (b) output = a * (b + c) *d。 b = 2。 Programming Pitfalls The fprintf function only displays the real part of a plex number, which can produce misleading answers when working with plex values. Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 45 Data Files ?save Saves data from current MATLAB workspace into disk file. save filenames var1 var2 var3 Default extent filename is .mat, called MATfile, a special pact format. MATfile can be created and read by MATLAB on any platform. MATfile cannot read by other programs. Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 46 How to share data with other programs? ascii option. Special information such as variable names and types are lost, file much larger. Easy to share data between MATLAB programs and other applications. x = [ 。 disp(str)。, pi) The limitation of fprintf function It only displays the real portion of a plex value! disp is better! Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 44 x = 2 * (1 – 2 * i) ^ 3。 arr4(1 : 2, 1 : 2) = 1 arr4 = 1 1 3 4 1 1 7 8 9 10 11 12 Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 33 Special Values ?Predefined values without initializing. ?Can be overwritten. ?Use clear mand can recovery the value. Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 34 Function Purpose pi Contain π to 15 significant digits. i, j Contain the value . Inf Represent machine infinity, . division by 0. NaN Stands for NotaNumber, . division of 0 by 0. clock Current data and time, 6element row vector, year/month/day/hour/minute/second. date Current data in a character string format. eps Short for epsilon(ε ) 2^52=, the smallest difference between 2 numbers represented on the puter. ans Store the result of an expression. ? ?1i ?Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 35 Programming Pitfalls Never redefine the meaning of a predefined variable in MATLAB. Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 36 Quiz Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 37 Displaying Output Data Changing the Default Format Integer and other values. a = 10 x = default format y = +003 scientific notation z = Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 38 Format Command Results Example format short 4 digits after decimal (default) format long 14 digits after decimal 3457 format short e 5 digits plus exponent +001 format short g 5 total digits with or without exponent format long e 15 digits plus exponent 3457e+001 format long g 15 total digits with or without exponent 346 format bank ―dollars and cents‖ format Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 39 Format Command Results Example format hex hexadecimal display of bits 4028b0fcd32f707a format rat approximate ratio of small integers 1000/81 format pact suppress extra line feeds format loose restore extra line feeds format + only the sign of number are printed Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 40 The disp Function ?disp Accepts an array argument and display the value, involve type char. ?num2str Converts a number to a string. ?int2str Convert an integer to a string. str = [?The value of pi=? num2str(pi) ]。 22 23] arr4 = 20 2 3 21 22 6 7 23 9 10 11 12 Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 31 arr4(1 : 2, 1 : 2) = [3 4]。 9 10 11 12]。 2 3 4。 the second less rapidly, the last most slowly Chapter 2 MATLAB Basics 10/23/2022 Hangzhou Dianzi University 26 Accessing Multidimensional Arrays with a Single