18
MATLAB MATLAB is a environment for scientific computing that is ideal for computations that require extensive use of arrays and graphical analysis of data. Matlab basics It is a interpreted language (no compiler); scripts can be saved as .m files Array indices begin with 1 (compare to 0 in C or Java) Arrays are passed by value to functions (no pointers) Array elements are accessed with the format A(1,2) (compare to the format A[0][1] in C or Java) Powerful matrix mathematical functions are built-in (e.g., \ for Gaussian elimination or least-squares solution methods for linear systems) MATLAB and the file system It is important to understand the relationship between the MATLAB operating environment and your computer's file system structure because This will simplify organizing your work Object-oriented programming in MATLAB is implemented by defining object classes through user-created directories Directory structures can be navigated through unix-like commands. Overview of the MATLAB Environment MATLAB (Matrix Algebra) is the commercial “Matrix Laboratory” package which operates as an interactive programming environment. It is a mainstay of the mathematics department software lineup and is also available for PC’s and Macintoshes MATLAB is a high-level technical computing language and interactive environment for algorithm development, data visualization, data analysis, and numeric computation. Using the MATLAB product, you can 1

matlab

  • Upload
    vinoth

  • View
    1

  • Download
    0

Embed Size (px)

DESCRIPTION

lab

Citation preview

MATLABMATLAB is a environment for scientific computing that is ideal for computations that require extensive use of arrays and graphical analysis of data. Matlab basics It is a interpreted language (no compiler); scripts can be saved as .m files Array indices begin with 1 (compare to 0 in C or Java) Arrays are passed by value to functions (no pointers) Array elements are accessed with the format A(1,2) (compare to the format A[0][1] in C or Java) Powerful matrix mathematical functions are built-in (e.g., \ for Gaussian elimination or least-squares solution methods for linear systems) MATLAB and the file systemIt is important to understand the relationship between the MATLAB operating environment and your computer's file system structure because This will simplify organizing your work Object-oriented programming in MATLAB is implemented by defining object classes through user-created directories Directory structures can be navigated through unix-like commands. Overview of the MATLAB EnvironmentMATLAB (Matrix Algebra) is the commercial Matrix Laboratory package which operates as an interactive programming environment. It is a mainstay of the mathematics department software lineup and is also available for PCs and MacintoshesMATLAB is a high-level technical computing language and interactive environment for algorithm development, data visualization, data analysis, and numeric computation. Using the MATLAB product, you can solve technical computing problems faster than with traditional programming languages, such as C, C++, and Fortran.You can use MATLAB in a wide range of applications, including signal and image processing, communications, control design, test and measurement, financial modeling and analysis, and computational biology. Add-on toolboxes (collections of special-purpose MATLAB functions, available separately) extend the MATLAB environment to solve particular classes of problems in these application areas.MATLAB provides a number of features for documenting and sharing your work. You can integrate your MATLAB code with other languages and applications, and distribute your MATLAB algorithms and applications.

Features include: High-level language for technical computing Development environment for managing code, files, and data Interactive tools for iterative exploration, design, and problem solving Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, and numerical integration 2-D and 3-D graphics functions for visualizing data Tools for building custom graphical user interfaces Functions for integrating MATLAB based algorithms with external applications and languages, such as C, C++, Fortran, Java, COM, and Microsoft ExcelThe MATLAB SystemThe MATLAB system consists of these main parts:Desktop Tools and Development EnvironmentThis part of MATLAB is the set of tools and facilities that help you use and become more productive with MATLAB functions and files. Many of these tools are graphical user interfaces. It includes: the MATLAB desktop and Command Window, an editor and debugger, a code analyzer, and browsers for viewing help, the workspace, and folders.Mathematical Function LibraryThis library is a vast collection of computational algorithms ranging from elementary functions, like sum, sine, cosine, and complex arithmetic, to more sophisticated functions like matrix inverse, matrix eigenvalues, Bessel functions, and fast Fourier transforms.The LanguageThe MATLAB language is a high-level matrix/array language with control flow statements, functions, data structures, input/output, and object-oriented programming features. It allows both "programming in the small" to rapidly create quick programs you do not intend to reuse. You can also do "programming in the large" to create complex application programs intended for reuse.

FUNCTIONS USED:

Elementary Matrices:

ONES Ones array. ONES(N) is an N-by-N matrix of ones. ONES(M,N) or ONES([M,N]) is an M-by-N matrix of ones. ONES(M,N,P,...) or ONES([M N P ...]) is an M-by-N-by-P-by-... array of ones. ONES(SIZE(A)) is the same size as A and all ones.

ZEROS Zeros array. ZEROS(N) is an N-by-N matrix of zeros. ZEROS(M,N) or ZEROS([M,N]) is an M-by-N matrix of zeros. ZEROS(M,N,P,...) or ZEROS([M N P ...]) is an M-by-N-by-P-by-... array of zeros. ZEROS(SIZE(A)) is the same size as A and all zeros. ZEROS with no arguments is the scalar 0.

EYE Identity matrix.

EYE(N) is the N-by-N identity matrix. EYE(M,N) or EYE([M,N]) is an M-by-N matrix with 1's on the diagonal and zeros elsewhere. EYE(SIZE(A)) is the same size as A. EYE with no arguments is the scalar 1. EYE(M,N,CLASSNAME) or EYE([M,N],CLASSNAME) is an M-by-N matrix with 1's of class CLASSNAME on the diagonal and zeros elsewhere.

LINSPACE Linearly spaced vector.

LINSPACE(X1, X2) generates a row vector of 100 linearly equally spaced points between X1 and X2. LINSPACE(X1, X2, N) generates N points between X1 and X2. For N < 2, LINSPACE returns X2.

FLIPLR Flip matrix in left/right direction.

FLIPLR(X) returns X with row preserved and columns flipped in the left/right direction. X = 1 2 3 becomes 3 2 1 4 5 6 6 5 4

Basic Array Information:

LENGTH Length of vector.

LENGTH(X) returns the length of vector X. It is equivalent to MAX(SIZE(X)) for non-empty arrays and 0 for empty ones.

SIZE Size of array.

D = SIZE(X), for M-by-N matrix X, returns the two-element row vector D = [M,N] containing the number of rows and columns in the matrix. For N-D arrays, SIZE(X) returns a 1-by-N vector of dimension lengths. Trailing singleton dimensions are ignored. [M,N] = SIZE(X) for matrix X, returns the number of rows and columns in X as separate output variables. [M1,M2,M3,...,MN] = SIZE(X) for N>1 returns the sizes of the first N dimensions of the array X.

NUMEL Number of elements in an array or subscripted array expression. N = NUMEL(A) returns the number of elements, N, in array A. N = NUMEL(A, VARARGIN) returns the number of subscripted elements, N, in A(index1, index2, ..., indexN), where VARARGIN is a cell array whose elements are index1, index2, ... indexN.

Elementary Math functions:

SIN Sine of argument in radians. SIN(X) is the sine of the elements of X.

COS Cosine of argument in radians.

COS(X) is the cosine of the elements of X.

ABS Absolute value.

ABS(X) is the absolute value of the elements of X. When X is complex, ABS(X) is the complex modulus (magnitude) of the elements of X.ANGLE Phase angle.

ANGLE(H) returns the phase angles, in radians, of a matrix with complex elements.

REAL Complex real part.

REAL(X) is the real part of X.

IMAG Complex imaginary part.

IMAG(X) is the imaginary part of X.

FIX Round towards zero.

FIX(X) rounds the elements of X to the nearest integers towards zero.

FLOOR Round towards minus infinity.

FLOOR(X) rounds the elements of X to the nearest integers towards minus infinity.

CEIL Round towards plus infinity.

CEIL(X) rounds the elements of X to the nearest integers towards infinity.

ROUND Round towards nearest integer.

ROUND(X) rounds the elements of X to the nearest integers.

SIGN Signum function.

For each element of X, SIGN(X) returns 1 if the element is greater than zero, 0 if it equals zero and -1 if it is less than zero. For the nonzero elements of complex X, SIGN(X) = X ./ ABS(X).

Signal Generation:

SQUARE Square wave generation.

SQUARE(T) generates a square wave with period 2*Pi for the elements of time vector T. SQUARE(T) is like SIN(T), only it creates a square wave with peaks of +1 to -1 instead of a sine wave. SQUARE(T,DUTY) generates a square wave with specified duty cycle. The duty cycle, DUTY, is the percent of the period in which the signal is positive.

SAWTOOTH Sawtooth and triangle wave generation.

SAWTOOTH(T) generates a sawtooth wave with period 2*pi for the elements of time vector T. SAWTOOTH(T) is like SIN(T), only it creates a sawtooth wave with peaks of +1 to -1 instead of a sine wave. SAWTOOTH(T,WIDTH) generates a modified triangle wave where WIDTH, a scalar parameter between 0 and 1, determines the fraction between 0 and 2*pi at which the maximum occurs. The function increases from -1 to 1 on the interval 0 to WIDTH*2*pi, then decreases linearly from 1 back to -1 on the interval WIDTH*2*pi to 2*pi. Thus WIDTH = .5 gives you a triangle wave, symmetric about time instant pi with peak amplitude of one. SAWTOOTH(T,1) is equivalent to SAWTOOTH(T).

TRIPULS Sampled aperiodic triangle generator.

TRIPULS(T) generates samples of a continuous, aperiodic, unity-height triangle at the points specified in array T, centered about T=0. By default, the triangle is symmetric and has width 1. TRIPULS(T,W) generates a triangle of width W. TRIPULS(T,W,S) allows the triangle skew S to be adjusted. The skew parameter must be in the range -1 < S < +1, where 0 generates a symmetric triangle.

Convolution & Correlation:

CONV Convolution and polynomial multiplication.

C = CONV(A, B) convolves vectors A and B. The resulting vector is length LENGTH(A)+LENGTH(B)-1. If A and B are vectors of polynomial coefficients, convolving them is equivalent to multiplying the two polynomials.

XCORR Cross-correlation function estimates. C = XCORR(A,B), where A and B are length M vectors (M>1), returns the length 2*M-1 cross- correlation sequence C. If A and B are of different length, the shortest one is zero-padded. C will be a row vector if A is a row vector, and a column vector if A is a column vector. XCORR produces an estimate of the correlation between two random(jointly stationary) sequences: C(m) = E[A(n+m)*conj(B(n))] = E[A(n)*conj(B(n-m))] It is also the deterministic correlation between two deterministic signals. Plotting of signals:

PLOT Linear plot.

PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix, then the vector is plotted versus the rows or columns of the matrix, whichever line up. If X is a scalar and Y is a vector, disconnected line objects are created and plotted as discrete points vertically at X. PLOT(Y) plots the columns of Y versus their index. If Y is complex, PLOT(Y) is equivalent to PLOT(real(Y),imag(Y)).

STEM Discrete sequence or "stem" plot.

STEM(Y) plots the data sequence Y as stems from the x axis terminated with circles for the data value. If Y is a matrix then each column is plotted as a separate series. STEM(X,Y) plots the data sequence Y at the values specified in X. In all other uses of PLOT, the imaginary part is ignored.

XLABEL X-axis label.

XLABEL('text') adds text beside the X-axis on the current axis. XLABEL('text','Property1',PropertyValue1,'Property2',PropertyValue2,...) sets the values of the specified properties of the xlabel.

YLABEL Y-axis label.

YLABEL('text') adds text beside the Y-axis on the current axis. YLABEL('text','Property1',PropertyValue1,'Property2',PropertyValue2,...) sets the values of the specified properties of the ylabel.

TITLE Graph title.

TITLE('text') adds text at the top of the current axis. TITLE('text','Property1',PropertyValue1,'Property2',PropertyValue2,...) sets the values of the specified properties of the title.

SUBPLOT Create axes in tiled positions.

H = SUBPLOT(m,n,p), or SUBPLOT(mnp), breaks the Figure window into an m-by-n matrix of small axes, selects the p-th axes for the current plot, and returns the axis handle. The axes are counted along the top row of the Figure window, then the second row, etc.

Inputting & Outputting data:

INPUT Prompt for user input.

R = INPUT('How many apples') gives the user the prompt in the text string and then waits for input from the keyboard.The input can be any MATLAB expression, which is evaluated, using the variables in the current workspace, and the result returned in R. If the user presses the return key without entering anything, INPUT returns an empty matrix. R = INPUT('What is your name','s') gives the prompt in the text string and waits for character string input. The typed input is not evaluated; the characters are simply returned as a MATLAB string.

DISP Display array.

DISP(X) displays the array, without printing the array name. In all other ways it's the same as leaving the semicolon off an expression except that empty arrays don't display. If X is a string, the text is displayed.

DFT & IDFT Computation:

FFT Discrete Fourier transform.

FFT(X) is the discrete Fourier transform (DFT) of vector X. For matrices, the FFT operation is applied to each column. For N-D arrays, the FFT operation operates on the first non-singleton dimension. FFT(X,N) is the N-point FFT, padded with zeros if X has less than N points and truncated if it has more.

IFFT Inverse discrete Fourier transform.

IFFT(X) is the inverse discrete Fourier transform of X. IFFT(X,N) is the N-point inverse transform.

Radix 2 FFT Computation:

DEC2BIN Convert decimal integer to a binary string.

DEC2BIN(D) returns the binary representation of D as a string. D must be a non-negative integer smaller than 2^52. DEC2BIN(D,N) produces a binary representation with at least N bits.

BIN2DEC Convert binary string to decimal integer.

X = BIN2DEC(B) interprets the binary string B and returns in X the equivalent decimal number. If B is a character array, or a cell array of strings, each row is interpreted as a binary string. Embedded, significant spaces are removed. Leading spaces are converted to zeros.

LOG2 Base 2 logarithm and dissect floating point number.

Y = LOG2(X) is the base 2 logarithm of the elements of X. [F,E] = LOG2(X) for each element of the real array X, returns an array F of real numbers, usually in the range 0.5