15

Click here to load reader

Matlab Review

Embed Size (px)

Citation preview

3

MATLAB ReviewJ. M. Sebeson, DeVry University, 2009This course makes use of MATLAB for calculations and the plotting of functions. MATLAB has extensive capabilities, but we will focus on just a few of them. These notes will review the following topics on the use of MATLAB:1. Variables and operations: scalars, vectors, and matrices; uniformly spaced vector sequences; element-by-element calculations.2. Representation of complex numbers, magnitude and angle.3. Common built-in functions; trig and exponential functions.4. Entering and plotting functions and signals.5. Polynomials and their roots.6. MATLAB help. Tabbing for command names; lookfor command.7. Symbolic Math toolbox; transforms and inverse transforms; ordinary differential equations

Topic 1 Variables and operationsNumbers and variables in MATLAB can be represented as scalars, vectors (either row or column strings) or matrices (row and column arrays). For example:

>> a=[2] % A scalara =

2

>> b=[1,2,3,4] % A row vectorb =

1 2 3 4

>> c=[1,2,3;4,5,6;7,8,9] % A matrixc =

1 2 3

4 5 6

7 8 9A screen shot of the MATLAB window for the above example is given below (MATLAB version R2011a). Commands are always entered in the Command Window at the >> prompt and the results are always displayed in the Command Window. The other windows provide other information, but this is not important for our discussion in these notes.

Continuing with our example, row vectors can be turned into column vectors and vice versa with the transpose character ().

>> b' % Changes the row vector b to a column vectorans =

1

2

3

4

In the construction of signals, it is often necessary to create a vector of values over a certain range in evenly spaced intervals. For example, suppose you wish to generate the function

over the interval of t = 0 to t = 1 second in intervals of 0.01 seconds. The commands would be:

>> t=0:.01:1;

>> s=sin(2*pi*5*t); % The number pi = 3.1416.. is just written pi in MATLAB> length(s)

ans =

101

Notice the use of the colon (:) in the construction of the t vector. Both the t and the s vectors would contain 101 elements.

Other useful commands for constructing vectors with evenly spaced values are linspace and logspace. (See their help descriptions.)When carrying out certain operations with vectors, such as addition, subtraction, and element-by-element multiplication or division, the length of the operand vectors must be the same (that is, they must have the same number of elements). If you wish to carry out element-by-element operations, you must use the dot-operation syntax. For example:>> b=[1,2,3,4];

>> d=[5,6,7,8];>> b.*d % b and d have the same length. The .* defines the operation of element-by-element % multiplication of b and dans =

5 12 21 32>> b.^3 % note the use of .^ to define the operation of cubing each element of vector b

ans =

1 8 27 64

Topic 2 Complex numbers

In MATLAB the unity imaginary number (that is, ) is represented either by i or j. (Therefore, never use these letters as variable names.) To see this, take the squares of i and j:>> i*i

ans =

-1

>> j*j

ans =

-1

MATLAB always represents complex numbers in their rectangular form:

>> x=exp(-j*pi/4) % A complex number in polar form

x =

0.7071 - 0.7071i % Rectangular form

>> real(x) % The real part of xans =

0.7071 >> imag(x) % The imaginary part of xans =

-0.7071

Given a complex number, you can find the components of its polar form as follows:

>> x=5*exp(j*2.5) % Polar formx =

-4.0057 + 2.9924i % Rectangular form>> abs(x) % Finds the magnitude of xans =

5

>> angle(x) % Finds the angle of x in radiansans =

2.5000

By default, MATLAB represents angles in radians. It is a good idea to always use radians for angular measure in mathematics.Topic 3 Common functions in MATLAB

The functions in MATLAB can be found in the built-in help documentation. Basic mathematical functions can be listed with the command help elfun. Some of the more specialized mathematical functions can be listed with the command help specfun. (Try some of the functions that are returned by these commands.) Especially useful functions in signal processing are the trigonometric functions ( sin, cos, etc) and the exponential function ex.

>> sin(pi/2) % note that 90 degrees is pi/2 radiansans =

1

>> exp(1) % This calculates e^1, the base of natural logarithms.ans =

2.7183

> format long % This commands extends the number of decimal places displayed

>> exp(1)

ans =

2.71828182845905

Topic 4 Plotting graphs - continuous and discrete signalsMATLAB has very sophisticated graphics capabilities that can be explored in the help documentation. However, the most basic commands for plotting are plot (connects the data points with continuous lines) or stem (creates stem plots of discrete data). Example 1: Plot the sum of two equal amplitude sinusoids of frequencies 50 Hz and 100 Hz from 0 to 50 milliseconds. Use an increment of .01 ms in the plot. Note that the signal to be plotted is

In the above equation, f1 = 50 Hz and f2 =100 Hz and t is in units of seconds. This can also be written as:

In this equation, T1 is the period of f1 and T2 is the period of f2. T and t can be in any units of time as long as they are the same units (milliseconds in this example).

>> t=0:.01:50; % A time vector from 0 to 50 in increments of .01. This vector is in ms.>> f1=50; % Frequencies in Hz>> f2=100;

>> T1=1000/f1; % Periods in milliseconds (there are 1000 milliseconds per second)>> T2=1000/f2;

>> s=sin(2*pi*t/T1)+sin(2*pi*t/T2); % Note that t and T would have the same units (ms)>> plot(t,s)

Example 2: Using the same signal as above, plot the discrete-time signal (digital signal) that would result from sampling s(t) at a frequency of 500 Hz (that is, a sample every 2 milliseconds). For this example, we will generate a stem plot, because that is the standard way to represent discrete-time signals and functions. We can use the same code as above except that we will make the time vector run from 0 to 50 milliseconds with an increment of 2 ms and use stem in place of plot.>> t=0:2:50; % Now the t vector has an increment of 2 instead of .01

>> f1=50; % Frequencies in Hz

>> f2=100;

>> T1=1000/f1; % Periods in milliseconds

>> T2=1000/f2;

>> s=sin(2*pi*t/T1)+sin(2*pi*t/T2);

>> stem(t,s)

To see that the above plot is in fact a sample of the continuous function, we can re-plot the original graph on the stem plot by using the hold function.

>> f1=50;

>> f2=100;

>> T1=1000/f1;

>> T2=1000/f2;

>> t=0:2:50; % Time vector with a 2 ms interval

>> s=sin(2*pi*t/T1)+sin(2*pi*t/T2);

>> stem(t,s)

>> t=0:.01:50; % Time vector with a .01 ms interval

>> s=sin(2*pi*t/T1)+sin(2*pi*t/T2);

>> hold % hold the last plot%Current plot held

>> plot(t,s,'k') % The signal s is plotted with a black line (k flag)>> hold off % Release the plot so that it is not re-used by later plots

Topic 5 Polynomials and their rootsIn engineering mathematics, finding the roots of polynomials is an important mathematical procedure. In general, a polynomial in the variable z can be written as:

The set of numbers are the coefficients of the polynomial while the number n is the order of the polynomial. The roots of the polynomial are those values of z for which p(z) = 0. According to the Fundamental Theorem of Algebra, the number of roots of a polynomial is equal to the order. For example, a cubic equation (n = 3) will always have three roots. The roots may be either real or complex or both, as the examples below will show. With the possible exception of quadratic equations (n = 2), it is usually difficult to find the roots of a polynomial, so a calculator or MATLAB is necessary. MATLAB uses the command roots to find the roots of a polynomial.

Example 1: Find the roots of the polynomial . Since this polynomial has order 3, we expect to find three roots.>> c=[1,2,3,4] % The coefficients in descending powers of z

c =

1 2 3 4

>> roots(c) % The roots command only needs the vector of coefficients.

ans =

-1.6506 % As expected, 3 roots, one real, two complex

-0.1747 + 1.5469i % The complex roots are complex conjugates of each other

-0.1747 - 1.5469i

Example 2. Find the roots of the polynomial . Notice that this polynomial is equivalent to:

We expect to find eight roots, all of which will have the magnitude of one.

>> c=[1,0,0,0,0,0,0,0,-1];

>> r=roots(c)

r =

-1.0000

-0.7071 + 0.7071i % Notice that all complex roots come in complex conjugate pairs -0.7071 - 0.7071i

0 + 1.0000i

0 - 1.0000i

1.0000

0.7071 + 0.7071i

0.7071 - 0.7071i

>> abs(r) % The magnitude of all the roots is 1ans =

1.0000

1.0000

1.0000

1.0000

1.0000

1.0000

1.0000

1.0000

A picture of the location of the roots in the complex plane can be generated by the command zplane. >> c=[1,0,0,0,0,0,0,0,-1];

>> zplane(c) % In this case zplane uses the vector of polynomial coefficients

In the above figure, the roots of are shown as small open circles. The dotted circle defines the unit circle in the complex plane where the magnitude of complex numbers is equal to one.

Topic 6 MATLAB help

MATLAB has extensive built-in help and examples. It is a good idea to always read the help file for a command or an M-file (custom MATLAB program) to understand the syntax and options for the command.

Finding the commands you want can sometimes be challenging. Several useful tips are the following:1. Try help elfun and help specfun for lists of the many mathematical functions in MATLAB.

2. Try help general for general purpose commands.

3. Try help graph2d for 2 dimensional plotting commands

4. If you think the command name or M-file may start with a particular set of letters, try help with your guess, followed by tab. This will list all the commands that start with the letters you entered.

5. The command lookfor will find matches to a word or phase in the first line of the help documentation for all commands.

Example: Try to find a command that will carry out numerical integration. Our first guess would be help integ . Trying this, you will only find a match for integerMath. Next, try the lookfor command:>> lookfor integral % Search for the word integral in the first line of a help file.ELLIPKE Complete elliptic integral.

EXPINT Exponential integral function.

DBLQUAD Numerically evaluate double integral.

QUAD Numerically evaluate integral, adaptive Simpson quadrature.

QUAD8 Numerically evaluate integral, higher order method.

QUADL Numerically evaluate integral, adaptive Lobatto quadrature.

TRIPLEQUAD Numerically evaluate triple integral.

COSINT Cosine integral function.

SININT Sine integral function.

COSINT Cosine integral function.

FOURIER Fourier integral transform.

IFOURIER Inverse Fourier integral transform.

SININT Sine integral function.Notice that the lookfor command found the commands involving QUAD, which, it turns out, are the commands for carrying out a numerical integration.

Topic 7 Symbolic Math Toolbox

When we discuss Laplace transforms, Z-transforms, and Fourier transforms, we will show how MATLAB can help find transforms and inverse transforms using the Symbolic Math Toolbox. This is a much more convenient method than the traditional table look-up of transforms. The Symbolic Math Toolbox has other capabilities, but we will mainly restrict our attention to finding transforms and their inverses. The commands for transform analysis are the following:1. laplace(f) finds the Laplace transform of the function f.

2. ilaplace(F) finds the inverse Laplace transform of the transform F.

3. ztrans(x) finds the Z-transform of the sequence x

4. iztrans(X) finds the inverse Z-transform of the transform X

5. fourier(f) finds the Fourier transform of the function f

6. ifourier(F) finds the inverse Fourier transform of the transform F.When using the Symbolic Math Toolbox, those characters that are to be used as symbols must be declared as symbols by the syms command. A few examples will illustrate the usage.

Example: Find the Laplace transform of the function . This is a sinusoid with a frequency of radians/sec. Since MATLAB does not use Greek characters, we will use w instead of .>> syms t s w % This declares the symbols

>> f=sin(w*t); % The function for which we will find the Laplace transform

>> F=laplace(f) % The command to find the Laplace transform

F =

w/(s^2 + w^2)This result is equivalent to:

Reference to a table of Laplace transforms will verify that this is the correct answer.

Example: Find the inverse Laplace transform of

>> syms t s % Declare the symbols>> F=1/(s+3); % The Laplace transform for which we want the inverse>> f=ilaplace(F) % Finds the inverse Laplace transformf =

1/exp(3*t)

This result is equivalent to:

_1306859799.unknown

_1306866148.unknown

_1384155236.unknown

_1384155608.unknown

_1384167146.unknown

_1384155406.unknown

_1384154369.unknown

_1306863799.unknown

_1306865509.unknown

_1306863646.unknown

_1306779803.unknown

_1306859650.unknown

_1306777566.unknown