《MATLAB快速入门——国外大神的教程》由会员分享,可在线阅读,更多相关《MATLAB快速入门——国外大神的教程(13页珍藏版)》请在金锄头文库上搜索。
1、MATLAB stands for MATrix LABoratory. It is a powerful numerical computing language commonly used in engineering and mathematics.If you have any feedback please feel free to reach me at the_ozzinator, or .% Code sections start with two percent signs. Section titles go on the same line.% Comments star
2、t with a percent sign.%Multi line comments looksomethinglikethis% commands can span multiple lines, using .:a = 1 + 2 + .+ 4% commands can be passed to the operating system!ping who % Displays all variables in memorywhos % Displays all variables in memory, with their typesclear % Erases all your var
3、iables from memoryclear(A) % Erases a particular variableopenvar(A) % Open variable in variable editorclc % Erases the writing on your Command Windowdiary % Toggle writing Command Window text to filectrl-c % Abort current computationedit(myfunction.m) % Open function/script in editortype(myfunction.
4、m) % Print the source of function/script to Command Windowprofile on % turns on the code profilerprofile off % turns off the code profilerprofile viewer % Open profilerhelp command % Displays documentation for command in Command Windowdoc command % Displays documentation for command in Help Windowlo
5、okfor command % Searches for command in the first commented line of all functionslookfor command -all % searches for command in all functions% Output formattingformat short % 4 decimals in a floating numberformat long % 15 decimalsformat bank % only two digits after decimal point - for financial cal
6、culationsfprintf(text) % print “text“ to the screendisp(text) % print “text“ to the screen% Variables % Semi colon suppresses output to the Command Window4 + 6 % ans = 108 * myVariable % ans = 322 3 % ans = 8a = 2; b = 3;c = exp(a)*sin(pi/2) % c = 7.3891% Calling functions can be done in either of t
7、wo ways:% Standard function syntax:load(myFile.mat, y) % arguments within parentheses, separated by commas% Command syntax:load myFile.mat y % no parentheses, and spaces instead of commas% Note the lack of quote marks in command form: inputs are always passed as% literal text - cannot pass variable
8、values. Also, cant receive output:V,D = eig(A); % this has no equivalent in command form,D = eig(A); % if you only want D and not V% Logicals1 5 % ans = 010 = 10 % ans = 13 = 4 % Not equal to - ans = 13 = 3 % equal to - ans = 13 1 A.c = 1 2;A.d.e = false;% Vectorsx = 4 32 53 7 1x(2) % ans = 32, indi
9、ces in Matlab start 1, not 0x(2:3) % ans = 32 53x(2:end) % ans = 32 53 7 1x = 4; 32; 53; 7; 1 % Column vectorx = 1:10 % x = 1 2 3 4 5 6 7 8 9 10x = 1:2:10 % Increment by 2, i.e. x = 1 3 5 7 9% MatricesA = 1 2 3; 4 5 6; 7 8 9% Rows are separated by a semicolon; elements are separated with space or co
10、mma% A =% 1 2 3% 4 5 6% 7 8 9A(2,3) % ans = 6, A(row, column)A(6) % ans = 8% (implicitly concatenates columns into vector, then indexes into that)A(2,3) = 42 % Update row 2 col 3 with 42% A =% 1 2 3% 4 5 42% 7 8 9A(2:3,2:3) % Creates a new matrix from the old one%ans =% 5 42% 8 9A(:,1) % All rows in
11、 column 1%ans =% 1% 4% 7A(1,:) % All columns in row 1%ans =% 1 2 3A ; A % Concatenation of matrices (vertically)%ans =% 1 2 3% 4 5 42% 7 8 9% 1 2 3% 4 5 42% 7 8 9% this is the same asvertcat(A,A);A , A % Concatenation of matrices (horizontally)%ans =% 1 2 3 1 2 3% 4 5 42 4 5 42% 7 8 9 7 8 9% this is
12、 the same ashorzcat(A,A);A(:, 3 1 2) % Rearrange the columns of original matrix%ans =% 3 1 2% 42 4 5% 9 7 8size(A) % ans = 3 3A(1, :) = % Delete the first row of the matrixA(:, 1) = % Delete the first column of the matrixtranspose(A) % Transpose the matrix, which is the same as:A onectranspose(A) %
13、Hermitian transpose the matrix% (the transpose, followed by taking complex conjugate of each element)A % Concise version of complex transposeA. % Concise version of transpose (without taking complex conjugate)% Element by Element Arithmetic vs. Matrix Arithmetic% On their own, the arithmetic operato
14、rs act on whole matrices. When preceded% by a period, they act on each element instead. For example:A * B % Matrix multiplicationA .* B % Multiple each element in A by its corresponding element in B% There are several pairs of functions, where one acts on each element, and% the other (whose name end
15、s in m) acts on the whole matrix.exp(A) % exponentiate each elementexpm(A) % calculate the matrix exponentialsqrt(A) % take the square root of each elementsqrtm(A) % find the matrix whose square is A% Plottingx = 0:.10:2*pi; % Creates a vector that starts at 0 and ends at 2*pi with increments of .1y = sin(x);plot(x,y)xlabel(x axis)ylabel(y axis)title(Plot