当前位置:文档之家› matlab基础语句句法和举例

matlab基础语句句法和举例

rootsSyntaxDescriptionr = roots(c) returns a column vector whose elements are the roots of the polynomial c. Row vector c contains the coefficients of a polynomial, ordered in descending powers. If c has n+1 components, the polynomial it represents is c1x n+c2x (n-1)+…+c n-1+1.>> c=[ 1 2 3]c =1 2 3>> roots(c)ans =-1.0000 + 1.4142i-1.0000 - 1.4142iexpexpExponentialSyntaxY = exp(X)DescriptionThe exp function is an elementary function that operates element-wise on arrays. Its domain includes complex numbers. Y = exp(X) returns the exponential for each element of Xlog %(注意MA TLAB里log是ln的意思)logNaturallogarithm SyntaxY = log(X)DescriptionThe log function operates element-wise on arrays. Its domain includes complex and negative numbers, which may lead to unexpected results if used unintentionally. Y = log(X) returns the natural logarithm of the elements of X.>> exp(log(1))ans =1反三角函数asinacosnorm 求向量的模或矩阵的范数Vector and matrix normsSyntaxn = norm(A)n = norm(A,p)DescriptionThe norm of a matrix is a scalar that gives some measure of the magnitude of the elements of the matrix.The norm function calculates several different types of matrix norms:n = norm(A) returns the largest singular value of A, max(svd(A)).n = norm(A,p) returns a different kind of norm, depending on the value of p.RemarksNote that norm(x) is the Euclidean length of a vector x. On the other hand, MATLAB uses "length" to denote the number of elements n in a vector. This example uses norm(x)/sqrt(n) to obtain the root-mean-square (RMS) value of an n-element vector x. x = [0 1 2 3]x =0 1 2 3sqrt(0+1+4+9) % Euclidean lengthans =3.7417norm(x)ans =3.7417n = length(x) % Number of elementsn =4rms = 3.7417/2 % rms = norm(x)/sqrt(n)rms =1.8708axis 建立坐标系axis Axis scaling and appearanceSyntaxaxis([xmin xmax ymin ymax])axis([xmin xmax ymin ymax zmin zmax cmin cmax])v = axisaxis autoaxis manualaxis tightaxis fillaxis ijaxis xyaxis equalaxis imageaxis squareaxis vis3daxis normalaxis offaxis onaxis(axes_handles,...)[mode,visibility,direction] = axis('state')Descriptionaxis manipulates commonly used axes properties. (See Algorithm section.)axis([xmin xmax ymin ymax]) sets the limits for the x- and y-axis of the current axes.axis([xmin xmax ymin ymax zmin zmax cmin cmax]) sets the x-, y-, and z-axis limits and the color scaling limits (see caxis) of the current axes.v = axis returns a row vector containing scaling factors for the x-, y-, and z-axis. v has four or six components depending on whether the current axes is 2-D or 3-D, respectively. The returned values are the current axes' XLim, Ylim, and ZLim properties.axis auto sets MATLAB to its default behavior of computing the current axes' limits automatically, based on the minimum and maximum values of x, y, and z data. You can restrict this automaticbehavior to a specific axis. For example, axis 'auto x' computes only the x-axis limits automatically; axis 'auto yz' computes the y- and z-axis limits automatically.axis manual and axis(axis) freezes the scaling at the current limits, so that if hold is on, subsequent plots use the same limits. This sets the XLimMode, YLimMode, and ZLimMode properties to manual.axis tight sets the axis limits to the range of the data.axis fill sets the axis limits and PlotBoxAspectRatio so that the axes fill the position rectangle. This option has an effect only if PlotBoxAspectRatioMode or DataAspectRatioMode are manual. axis ij places the coordinate system origin in the upper-left corner. The i-axis is vertical, with values increasing from top to bottom. The j-axis is horizontal with values increasing from left to right.axis xy draws the graph in the default Cartesian axes format with the coordinate system origin in the lower-left corner. The x-axis is horizontal with values increasing from left to right. The y-axis is vertical with values increasing from bottom to top.axis equal sets the aspect ratio so that the data units are the same in every direction. The aspect ratio of the x-, y-, and z-axis is adjusted automatically according to the range of data units in the x, y, and z directions.axis image is the same as axis equal except that the plot box fits tightly around the data.axis square makes the current axes region square (or cubed when three-dimensional). MATLAB adjusts the x-axis, y-axis, and z-axis so that they have equal lengths and adjusts the increments between data units accordingly.axis vis3d freezes aspect ratio properties to enable rotation of 3-D objects and overrides stretch-to-fill.axis normal automatically adjusts the aspect ratio of the axes and the relative scaling of the data units so that the plot fits the figures shape as best as possible.axis off turns off all axis lines, tick marks, and labels.axis on turns on all axis lines, tick marks, and labels.axis(axes_handles,...) applies the axis command to the specified axes. For example, the following statements:h1 = subplot(221);h2 = subplot(222);axis([h1 h2],'square')set both axes to square. [mode,visibility,direction] = axis('state') returns three strings indicating the current setting of axes properties:magic() 幻方函数magicMagic squareSyntaxM = magic(n)DescriptionM = magic(n) returns an n-by-n matrix constructed from the integers 1 through n^2 with equal row and column sums. The order n must be a scalar greater than or equal to 3.RemarksA magic square, scaled by its magic sum, is doubly stochastic.ExamplesThe magic square of order 3 is M = magic(3)M =8 1 63 5 74 9 2This is called a magic square because the sum of the elements in each column is the same. sum(M) =15 15 15And the sum of the elements in each row, obtained by transposing twice, is the same. sum(M')' =151515This is also a special magic square because the diagonal elements have the same sum. sum(diag(M)) =15The value of the characteristic sum for a magic square of order n is sum(1:n^2)/nwhich, when n = 3, is 15.AlgorithmThere are three different algorithms: n odd n even but not divisible by four n divisible by four To make this apparent, typefor n = 3:20A = magic(n);r(n) = rank(A);endFor n odd, the rank of the magic square is n. For n divisible by 4, the rank is 3. For n even but not divisible by 4, the rank is n/2 + 2.[(3:20)',r(3:20)']ans =3 34 35 56 57 78 39 910 711 1112 313 1314 915 1516 317 1718 1119 1920 3Plotting A for n = 18, 19, 20 shows the characteristic plot for each category.eye 制作单位阵(?)eyeIdentity matrixSyntaxY = eye(n)Y = eye(m,n)Y = eye(size(A))DescriptionY = eye(n) returns the n-by-n identity matrix.Y = eye(m,n) or eye([m n]) returns an m-by-n matrix with 1's on the diagonal and 0's elsewhere. Y = eye(size(A)) returns an identity matrix the same size as A.LimitationsThe identity matrix is not defined for higher-dimensional arrays. The assignment y = eye([2,3,4]) results in an error.>> n=3;y = eye(n)y =1 0 00 1 00 0 1>> n=4;m=9;eye(n,m)ans =1 0 0 0 0 0 0 0 00 1 0 0 0 0 0 0 00 0 1 0 0 0 0 0 00 0 0 1 0 0 0 0 0>>zeros OzerosCreate an array of all zerosSyntaxB = zeros(n)B = zeros(m,n)B = zeros([m n])B = zeros(d1,d2,d3...)B = zeros([d1 d2 d3...])B = zeros(size(A))DescriptionB = zeros(n) returns an n-by-n matrix of zeros. An error message appears if n is not a scalar.B = zeros(m,n) or B = zeros([m n]) returns an m-by-n matrix of zeros.B = zeros(d1,d2,d3...) or B = zeros([d1 d2 d3...]) returns an array of zeros with dimensionsd1-by-d2-by-d3-by-... .B = zeros(size(A)) returns an array the same size as A consisting of all zeros.RemarksThe MATLAB language does not have a dimension statement; MATLAB automatically allocates storage for matrices. Nevertheless, for large matrices, MATLAB programs may execute faster if the zeros function is used to set aside storage for a matrix whose elements are to be generated one at a time, or a row or column at a time.For examplex = zeros(1,n);for i = 1:n, x(i) = i; endtriu 取下三角行列式Upper triangular part of a matrixSyntaxU = triu(X)U = triu(X,k)>> triu(ones(4,4),-1)ans =1 1 1 11 1 1 10 1 1 10 0 1 1>> triu(ones(4,4),1)ans =0 1 1 10 0 1 10 0 0 10 0 0 0>> triu(ones(4,4),0)ans =1 1 1 10 1 1 10 0 1 10 0 0 1A.’矩阵A的转置inv 对矩阵求逆invMatrix inverseSyntaxY = inv(X)DescriptionY = inv(X) returns the inverse of the square matrix X.A warning message is printed if X is badly scaled or nearly singular. In practice, it is seldom necessary to form the explicit inverse of a matrix.A frequent misuse of inv arises when solving the system of linear equations A x=b. One way to solve this is with x = inv(A)*b. A better way, from both an execution time and numerical accuracy standpoint, is to use the matrix division operator x = A\b. This produces the solution usingGaussian elimination, without forming the inverse. See \ and / for further information.例1-9画出函数y = x2cosx 与z = sinx / x 在区间[-6π,6π]上的图像。

相关主题