A MATLAB tutorial.ppt
《A MATLAB tutorial.ppt》由会员分享,可在线阅读,更多相关《A MATLAB tutorial.ppt(74页珍藏版)》请在麦多课文档分享上搜索。
1、A MATLAB tutorial,Bjrn K. Alsberg Department of Computer Science University of Wales Aberystwyth,Introduction,What is MATLAB? General info. Manipulation of vectors and matrices String manipulation Plotting Programming (script and function files) Simple file input/output Some important mathematical f
2、unctions Management of MATLAB projects,What is MATLAB?,MATLAB (MATrix LABoratory) is a language for manipulation of vector and matrix expressions. MATLAB interprets code and is not compiled (valid for earlier versions than 5.0 only) MATLAB provides a powerful on-line graphical interface to explore t
3、he results from calculations.,Starting MATLAB,Find the MATLAB icon and double click You will see something like:,Commands to get started: intro, demo, help help Commands for more information: help, whatsnew, info, subscribe,MATLAB is an interpreter,This means you are performing a dialogue with MATLA
4、B: (2+5)*10/2 what you type in ans =35 what MATLAB answers you ,MATLAB as a calculator,All usual numerical operators available: elementary operators: + - / * e.g899 - 7.88 * 0.123 math functions: abs, cos, acos, sin, asin, tan, atan, exp, asinh, cosh, log, log10, sqrt, tanh, conj e.g. abs(-34.5) , c
5、os(0.7887) (assumes radians), exp(2.989),Variables,In MATLAB you dont declare variables. In general: variable_name = value Variables cant start with a number or names assigned to the MATLAB language. a = 12; c = a - 5 c =7,“Who” and “whos” (1),who/whos are used to display variables and information a
6、bout variables currently in working memory. example: qr = 99.9; f = -190; X = 12;12;89;90; who Your variables are:X f qr,“Who” and “whos” (2),whos Name Size Elements Bytes Density ComplexX 4 by 1 4 32 Full No f 1 by 1 1 8 Full No qr 1 by 1 1 8 Full No Grand total is 6 elements using 48 bytes,“clear”
7、 and “clc”,clear removes all variables from current memory clc clears the screen and sets the cursor to the upper left corner of the working window.,Function calls,Most of MATLABs commands are like functions:OUT = function_name(INPUT);The ; means that the contents in OUT are not written to screen.,V
8、ectors - transposing,a1 = 1 2 3 produce a ROW vector a2 = 1;2;3 produce a COLUMN vector is used to TRANSPOSE: b1 = a1 b1 is now:123,Vectors -merging,We can merge vectors. Let a = 2 7 5 8 and b = 1 1 2. Then c = a b will bec = 2 7 6 8 1 1 2The transposed: d = a ; b ( here d = c).d = a; b or a;b is no
9、t allowed,Vectors -indexing (0),Individual elements in a vector are extracted as follows, e.g.:a(1) - the first element in a a(5) - the fifth element in aWithin the parentheses we have a number (or numbers) that indicate(s) the element(s) we want to extract.NOTE! All vector/matrix indicies start wit
10、h 1 (not 0)!,Vectors -indexing (1),The “:”-notation:from:step:to6.7:-1.56:-0.11ans =6.7000 5.1400 3.5800 2.0200 0.4600,Vectors -indexing (2),a = 5 6 10 11 4. We want to construct a new vector from the 3 first elements in a:b = a(1:3)If we want the three last elements we need to know the size of a fi
11、rst:n = length(a) b = a(n-2:n),Matrix - intro,A matrix contains ROWS and COLUMNS:2 5 2 7 7X = 1 3 8 9 34 1 5 2 4 The dimensions or size of X is 3 ROWS and 5 COLUMNS, i.e. n,m=size(X) n = 3 m = 5,Matrix multiplication (1),Multiplication between vectors and matrices:A = 1 2 1; 2 9 0; 4 0 3; v = 0 1 3;
12、 y = A*vy =599,A =1 2 12 9 04 0 3,Matrix multiplication (2),We could also have written:y = v*Ay =14 9 9,It is very useful to remember that in general a matrix product has dimensions:p x q = p x n*n x q,Matrix multiplication (3),Let us multiply two matrices A and B:A = 0.5 3.4 0.1 2.2; 4.1 1.2 0.1 0.
13、1;A =0.5000 3.4000 0.1000 2.20004.1000 1.2000 0.1000 0.1000size(A) ans =2 4,Matrix multiplication (4),B is a matrix of random numbers:B = rand(4,2) B =0.2190 0.93470.0470 0.38350.6789 0.51940.6793 0.8310,Matrix multiplication (5),Now performing the matrix multiplication:C = A*B C =1.8318 3.65131.090
14、0 4.4275,Matrix - indexing (0),A matrix element is extracted by indicating ROW and COLUMN number(s), e.g.:C(1,2) - first row, second column C(4,4) - fourth row, fourth column,Matrix - indexing (1),The indexing for vector can be extended to matrices:k = C(:,2) k =3.65134.4275is the second column in C
15、. The : symbol means all elements in that index. The second row in C is:r = C(2,:) r =1.0900 4.4275,Matrix - indexing (2),The indexing in MATLAB is very powerful. The general syntax is:Y = X(vec1,vec2)where vec1 and vec2 are vectors containing the the indices we want to extract from the matrix X. Ex
16、ample:Y = X(12 8 1,10 1 2 80 40) size(Y)4 5,Matrix - indexing (3),If you happen to use decimal numbers in the index vectors, MATLAB automatically performs a rounding:X(1 9,:) = X(1.2 8.99,:)(may not be true for future versions of MATLAB),Matrix - addition,Adding and subtracting matrices and vectors
17、is easy:C = A + Bor C = A - Bas long as A and B have the same size. Another example:C = 12.4*A - 4*B + 100*D,Matrix - folding/unfolding (1),Unfolding and folding:X = 1 6 109 10 451 1 185 7 0;X unfolded into a vector:xv = (X(:)1 9 1 85 6 10 1 7 10 45 1 0The unfolding process operates COLUMNWISE,Matri
18、x - folding/unfolding (2),FOLDING A VECTOR BACK TO A MATRIX:Xh = reshape(xv,4,3)1 6 109 10 451 1 185 7 0,Special matrices (1),X = ones(2,3) X =1 1 11 1 1 Y = zeros(2,3) Y = 0 0 00 0 0,H = eye(4) H =1 0 0 00 1 0 00 0 1 00 0 0 1,Special matrices (2),Q = hankel(1 2 3 4) Q =1 2 3 42 3 4 03 4 0 04 0 0 0,
19、Q = toeplitz(1 2 3 4) Q = 1 2 3 42 1 2 33 2 1 24 3 2 1,Matrix operators (1),A =3 5 101 0 7 B = fliplr(A) B =10 5 37 0 1,D = diag(1 2 3) D = 1 0 00 2 00 0 3 v = diag(D) v = 1 2 3 v = diag(D,1) v = 0 0 ,Matrix operators (2),A =3 5 101 0 7 B = flipud(A) B =7 0 110 5 3,B = rot90(A) B =10 75 03 1,rot90 i
20、s NOT the same as transposed: B = A B =3 15 010 7,Matrix operators (3),B =A.2 B =9 25 1001 0 49 or C = A.*B C =27 125 10001 0 343,The dot notation means every element is subjected to a certain operator. Is valid for matrices and vectors: matrix .operator (matrix),Strings - intro,A string is containe
21、d within characters:q = This is a string vector! ; Each element in q contains a character: q(1:4) ans = This,Strings - concatenation,a = First part ; b = Second part; c = a b c = First part Second part;d = The ,a,of this string and the ,b; d = The First part of this string and the Second part,String
22、s - conversion (1),Converting from integers to strings:i = 25;str = She is ,int2str(i),years oldConverting from real numbers to strings:k = 12.778str =The road is ,num2str(k), miles long,Strings - conversion (2),Converting from strings to MATLAB commands:str = Q = A*B; eval(str)Very useful for e.g.
23、multiple matrices:i = 12 str = Q,int2str(i),= A,int2str(i-1),*B; eval(str)Q12 = A11*B;,Strings - conversion (3),How do I create a in a string when the string is defined by ?Define the string: fn = str = We can now create ,fn, in a string! We can now create in a string!,Plots - 2D basic,For simple 2-
24、D plot we use:plot(x) or plot(x,y)example: y = 5 3 6 5 4 3;plot(x) which here is the same as plot(1:6,y);,Plots - 2D symbols,Scatter 2D plot with various symbols:plot(x,y,xw)x = symbol w = white Possible symbols in MATLAB 4.2: . point o circle x x-mark + plus - solid * star : dotted -. dashdot - das
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- AMATLABTUTORIALPPT
