资源预览内容
第1页 / 共13页
第2页 / 共13页
第3页 / 共13页
第4页 / 共13页
第5页 / 共13页
第6页 / 共13页
第7页 / 共13页
第8页 / 共13页
第9页 / 共13页
第10页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 osvaldo.t.mendozagmail.com.% Code sections start with two percent signs. Section titles go on the same line.% Comments start 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 google.comwho % Displays all variables in memorywhos % Displays all variables in memory, with their typesclear % Erases all your variables 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.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 Windowlookfor 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 calculationsfprintf(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 two 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 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, indices 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 comma% 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 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 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) % 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 operators 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 ends 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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号