Matlab - Getting start, страница 5
Описание файла
PDF-файл из архива "Matlab - Getting start", который расположен в категории "". Всё это находится в предмете "цифровая обработка сигналов (цос)" из 8 семестр, которые можно найти в файловом архиве МГТУ им. Н.Э.Баумана. Не смотря на прямую связь этого архива с МГТУ им. Н.Э.Баумана, его также можно найти и в других разделах. Архив можно найти в разделе "книги и методические указания", в предмете "цифровая обработка сигналов" в общих файлах.
Просмотр PDF-файла онлайн
Текст 5 страницы из PDF
MATLAB also provides many more advancedmathematical functions, including Bessel and gamma functions. Most of thesefunctions accept complex arguments. For a list of the elementary mathematicalfunctions, typehelp elfunFor a list of more advanced mathematical and matrix functions, typehelp specfunhelp elmat3-113Manipulating MatricesSome of the functions, like sqrt and sin, are built-in. They are part of theMATLAB core so they are very efficient, but the computational details are notreadily accessible. Other functions, like gamma and sinh, are implemented inM-files. You can see the code and even modify it if you want.Several special functions provide values of useful constants.pi3.14159265…iImaginary unit, √-1jSame as iepsFloating-point relative precision, 2-52realminSmallest floating-point number, 2-1022realmaxLargest floating-point number, (2-ε)21023InfInfinityNaNNot-a-numberInfinity is generated by dividing a nonzero value by zero, or by evaluating welldefined mathematical expressions that overflow, i.e., exceed realmax.Not-a-number is generated by trying to evaluate expressions like 0/0 orInf-Inf that do not have well defined mathematical values.The function names are not reserved.
It is possible to overwrite any of themwith a new variable, such aseps = 1.e-6and then use that value in subsequent calculations. The original function canbe restored withclear eps3-12ExpressionsExamples of ExpressionsYou have already seen several examples of MATLAB expressions. Here are afew more examples, and the resulting values.rho = (1+sqrt(5))/2rho =1.6180a = abs(3+4i)a =5z = sqrt(besselk(4/3,rho-i))z =0.3730+ 0.3214ihuge = exp(log(realmax))huge =1.7977e+308toobig = pi*hugetoobig =Inf3-133Manipulating MatricesWorking with MatricesThis section introduces you to other ways of creating matrices.Generating MatricesMATLAB provides four functions that generate basic matrices.zerosAll zerosonesAll onesrandUniformly distributed random elementsrandnNormally distributed random elementsHere are some examples.Z = zeros(2,4)Z =000000F = 5*ones(3,3)F =55555555500N = fix(10*rand(1,10))N =4944R = randn(4,4)R =1.06680.29440.0593-1.3362-0.09560.7143-0.83231.62363-148-0.69180.85801.2540-1.59375-1.44100.5711-0.39990.69002680Working with MatricesThe load CommandThe load command reads binary files containing matrices generated by earlierMATLAB sessions, or reads text files containing numeric data.
The text fileshould be organized as a rectangular table of numbers, separated by blanks,with one row per line, and an equal number of elements in each row. Forexample, outside of MATLAB, create a text file containing these four lines.16.05.09.04.03.010.06.015.02.011.07.014.013.08.012.01.0Store the file under the name magik.dat. Then the commandload magik.datreads the file and creates a variable, magik, containing our example matrix.An easy way to read data into MATLAB in many text or binary formats is touse Import Wizard.M-FilesYou can create your own matrices using M-files, which are text files containingMATLAB code.
Use the MATLAB Editor or another text editor to create a filecontaining the same statements you would type at the MATLAB commandline. Save the file under a name that ends in .m.For example, create a file containing these five lines.A = [ ...16.03.05.010.09.06.04.015.02.011.07.014.013.08.012.01.0 ];Store the file under the name magik.m. Then the statementmagikreads the file and creates a variable, A, containing our example matrix.3-153Manipulating MatricesConcatenationConcatenation is the process of joining small matrices to make bigger ones.
Infact, you made your first matrix by concatenating its individual elements. Thepair of square brackets, [], is the concatenation operator. For an example, startwith the 4-by-4 magic square, A, and formB = [AA+32; A+48A+16]The result is an 8-by-8 matrix, obtained by joining the four submatrices.B =16594645357523106155158546321171450595562138121615660494837413632212520354238471926223134433946182723304540443329242817This matrix is half way to being another magic square.
Its elements are arearrangement of the integers 1:64. Its column sums are the correct value foran 8-by-8 magic square.sum(B)ans =260260260260260260260260But its row sums, sum(B')', are not all the same. Further manipulation isnecessary to make this a valid 8-by-8 magic square.3-16Working with MatricesDeleting Rows and ColumnsYou can delete rows and columns from a matrix using just a pair of squarebrackets. Start withX = A;Then, to delete the second column of X, useX(:,2) = []This changes X toX =16594211714138121If you delete a single element from a matrix, the result isn’t a matrix anymore.So, expressions likeX(1,2) = []result in an error. However, using a single subscript deletes a single element,or sequence of elements, and reshapes the remaining elements into a rowvector.
SoX(2:2:10) = []results inX =16927131213-173Manipulating MatricesMore About Matrices and ArraysThis section shows you more about working with matrices and arrays, focusingon:• Linear algebra• Arrays• Multivariate dataLinear AlgebraInformally, the terms matrix and array are often used interchangeably. Moreprecisely, a matrix is a two-dimensional numeric array that represents a lineartransformation. The mathematical operations defined on matrices are thesubject of linear algebra.Dürer’s magic squareA =16594310615211714138121provides several examples that give a taste of MATLAB matrix operations.You’ve already seen the matrix transpose, A'.
Adding a matrix to its transposeproduces a symmetric matrix.A + A'ans =32811178201723111714261723262The multiplication symbol, *, denotes the matrix multiplication involving innerproducts between rows and columns. Multiplying the transpose of a matrix bythe original matrix also produces a symmetric matrix.3-18More About Matrices and ArraysA'*Aans =378212206360212370368206206368370212360206212378The determinant of this particular matrix happens to be zero, indicating thatthe matrix is singular.d = det(A)d =0The reduced row echelon form of A is not the identity.R = rref(A)R =1000010000101-330Since the matrix is singular, it does not have an inverse. If you try to computethe inverse withX = inv(A)you will get a warning messageWarning: Matrix is close to singular or badly scaled.Results may be inaccurate.
RCOND = 1.175530e-017.Roundoff error has prevented the matrix inversion algorithm from detectingexact singularity. But the value of rcond, which stands for reciprocal conditionestimate, is on the order of eps, the floating-point relative precision, so thecomputed inverse is unlikely to be of much use.3-193Manipulating MatricesThe eigenvalues of the magic square are interesting.e = eig(A)e =34.00008.00000.0000-8.0000One of the eigenvalues is zero, which is another consequence of singularity.The largest eigenvalue is 34, the magic sum. That’s because the vector of allones is an eigenvector.v = ones(4,1)v =1111A*vans =34343434When a magic square is scaled by its magic sum,P = A/34the result is a doubly stochastic matrix whose row and column sums are all one.P =0.47060.14710.26470.11763-200.08820.29410.17650.44120.05880.32350.20590.41180.38240.23530.35290.0294More About Matrices and ArraysSuch matrices represent the transition probabilities in a Markov process.Repeated powers of the matrix represent repeated steps of the process.
For ourexample, the fifth powerP^5is0.25070.24970.25000.24960.24950.25010.24980.25060.24940.25020.24990.25050.25040.25000.25030.2493This shows that as k approaches infinity, all the elements in the kth power, Pk,approach 1/4.Finally, the coefficients in the characteristic polynomialpoly(A)are1-34-6421760This indicates that the characteristic polynomialdet( A - λI )isλ4 - 34λ3 - 64λ2 + 2176λThe constant term is zero, because the matrix is singular, and the coefficient ofthe cubic term is -34, because the matrix is magic!ArraysWhen they are taken away from the world of linear algebra, matrices becometwo dimensional numeric arrays.