VIEWS: 6 PAGES: 19 POSTED ON: 1/28/2012
An Introduction to MATLAB ELG3506 What is MATLAB ? • The name MATLAB is an abbreviation for MATrix LABoratory. • In its early years (1970), MATLAB was a matrix-based program for scientific and engineering calculations. • Today, MATLAB is much more than a matrix-based program. It is a powerful computer program for doing numerical computations with vectors and arrays. What is MATLAB ? (Cont‟d) • It is both an environment and a programming language, which gives it its great strength. • MATLAB is also very useful for representing information and data graphically in 2D and 3D. • MATLAB has many toolboxes, which are a collection of useful functions for specific classes of problems. Some of these toolboxes are: controls, signal processing and image processing, radio frequency, filter design just to name a few. The MATLAB Interface • There are two important windows on the MATLAB interface, • The “Command Window”, where you enter the command at the prompt (>>) and, • The “Workspace”, where all variables, arrays, matrices and vectors are temporally saved. • Programs or scripts can also be written and executed in a single file called “M-File”. The MATLAB Interface (Cont‟d) The M-File * Adding a semi-colon at the end of a command, tells MATLAB not to print the results of the operations to the command screen. The M-File (Cont‟d) To open the editor and create a new M-File, select, The File Menu New M-File MATLAB Syntax* Variables Variables are defined with the assignment operator, “=“. Values can come from constants, from computation involving values of other variables, or from the output of a function. For example: * Some content taken from Wikipedia (http://en.wikipedia.org/wiki/MATLAB) MATLAB Syntax (Cont‟d) Vectors / Matrices In MATLAB, a vector refers to a one dimensional (1×N or N×1) matrix. A matrix generally refers to a multi-dimensional matrix, that is, a matrix with more than one dimension, for instance, an N×M, an N×M×L, etc., where N, M, and L are greater than 1. Basic Scalar Arithmetic • The basic arithmetic operators applied to scalars are +, -, * (multiplication), / (division) & ^ for powers. Where the constant „pi‟ is equal to 3.1416‟ Matrix, Vector and Array Arithmetic • The Operators are +, -, *, /, ^, and ' . • MATLAB has two different types of arithmetic operations. 1. Matrix arithmetic operations are defined by the rules of linear algebra. 2. Array arithmetic operations are carried out element by element, and can be used with multidimensional arrays. • The period character (.) distinguishes the array operations from the matrix operations. • However, since the matrix and array operations are the same for addition and subtraction, the character pairs .+ and .- are not used. Matrix, Vector and Array Arithmetic A+B Adds A and B. A and B must have the same size. A-B Subtracts B from A. A and B must have the same size. A*B Matrix multiplication. C = A*B is the linear algebraic product of the matrices A and B. A.*B Array multiplication. A.*B is the element-by-element product of the arrays A and B. A and B must have the same size. A/B Slash or matrix right division. A./B Array right division. A./B is the matrix with elements A(i,j)/B(i,j). A and B must have the same size A^B Matrix power. X^p is X to the power p, if p is a scalar. A.^B Array power. A.^B is the matrix with elements A(i,j) to the B(i,j) power. A and B must have the same size. A' Matrix transpose. A' is the linear algebraic transpose of A. For complex matrices, this is the complex conjugate transpose. A.' Array transpose. A.' is the array transpose of A. For complex matrices, this does not involve conjugation. Defining a Complex Number >> z = a + bi or, >> z = a + bj or, >> z = a + i*b or, >> z = complex(a,b) or, >> z = Aexp(j*B) or, >> z = a + b*sqrt(-1) Note: „i‟ and „j‟ must not be defined otherwise Iteration Statements (The „for‟ and „while‟ Loops) For Loop While Loop Condition Index for j=1:4, while n <= M statements statements end end Some Built-In Functions pi 3.1415.... zeros(n,m) n×m matrix of zeros eye(m) m×m identity matrix ones(n,m) n×m matrix of ones abs(z) absolute value of „z‟ angle(z) angle of „z‟ sqrt(x) square root, e.g. i=sqrt(-1) real(z), imag(z) real, imaginary parts of „z‟ conj(z) complex conjugate of „z‟ sin(x), cos(x), tan(x) trigonometric functions asin(y),acos(y), atan(y) inverse trigonometric functions sinh(x), cosh(x), tanh(x) hyperbolic functions exp(z) exponential function log(x) natural logarithm complex(a,b) define complex number a+jb Graphics in MATLAB plot(x,y) linear plot of y versus x grid on / off turns grid lines on graphics screen on or off title(‟text‟) prints a title for the plot xlabel(‟text‟) prints a label for the x-axis ylabel(‟text‟) prints a label for the y-axis axis([0, 1, -2, 2]) overrides' default limits for plotting hold on superimpose all subsequent plots hold off turns off a previous hold on legend(„data1‟,‟data2‟) include a legend for each data set clf clear graphics screen Graphics in MATLAB Example 1 The following MATLAB code plots the incident, reflected and standing waves on a transmission line for any reflection coefficient, Gamma.