200 likes | 445 Views
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.
E N D
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 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. • Matrix arithmetic operations are defined by the rules of linear algebra. • 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.
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 for j=1:4, statements end While Loop while n <= M statements end Condition Index
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
Example 1 The following MATLAB code plots the incident, reflected and standing waves on a transmission line for any reflection coefficient, Gamma.