MATLAB is a numerical computing environment and programming language. Created by The MathWorks, MATLAB allows easy matrix manipulation, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs in other languages. Many classes in our ECEn Department requires professional skill using matlab, such as ECEn 360, ECEn 370, ECEn 380, ECEn 483...This tutorial is for those who never touch matlab before or need some review for matlab.

Matlab has 2 useful default arrays: zeros, and ones. Each is an array filled with either zeros or ones. The format is : zeros/ones("number of dimension", length of array);

With these two useful arrays, you can save time on setting values. For example, if you want to simulate a step function: x=[zeros(1,5) ones(1,5)], then x will be: 0 0 0 0 0 1 1 1 1 1

After setting the variables, you can do normal operations between arrays. To make an operation work on an array, just add a dot before the normal operator.

P.S. Matrix operations are very similar to the array operations, detailed information can be found in the attached document.

Graphing

After setting and operating on arrays, the best way to see the behavior of x and y, or x,y, and z is to graph them. The most common functions for graphing are; plot, stem, polar, subplot, mesh, meshc, surf, surfc , and hold on.

Occasionaly, polar graphs may be better to see the relation between 2 variables. In EcEn 360, to graph the signal of an antenna, using a polar function is useful.

Similar to plot and stem commands, the format for a polar graph is: polar(x,y).

p.s. In this case, x probably is always an angle with a range of 0 to 2pi.

Some times we need to see the behavior of three variables. In EcEn 360, it's required to plot a 3D graph of the probability with two variables. There are many useful functions that can simulate 3D graphs depending on which one you need.

This function is mainly used for parametric functions, the syntax is: plot3(X1,Y1,Z1,...) where X1, Y1, Z1 are vectors or matrices. It will plot one or more lines in three-dimensional space through the points whose coordinates are the elements of X1, Y1, and Z1.

The syntax for these two functions is very similar to plot3, both are: mesh/surf(x,y,z). The difference is that these two functions will output a surface graph instead of only the connections of the dots.

if mech is replaced by surf, then the result will be:

P.S. In this program, [X,Y] = meshgrid(x,y) transforms the domain specified by vectors x and y into arrays X and Y, which can be used to evaluate functions of two variables and three-dimensional mesh/surface plots. The rows of the output array X are copies of the vector x; columns of the output array Y are copies of the vector y.

These two functions will give you the idea of a 3D graph in a 2D one. "meshc" function will give you the contour plot of a 3D graph. "view(2)" will give you the view of a 3D graph when looking down from the top of it.

Matlab is able to do differences and approximate derivatives, the basic function is diff.

The Syntax is: Y = diff(X) or Y = diff(X,n), n is the dimension of the derivative (such as a second derivative, third derivative, etc.) If n=1, it is the same as y=diff(x).

y=diff(X) calculates differences between adjacent elements of X.

If X is a vector, then diff(X) returns a vector, one element shorter than X, of differences between adjacent elements:y=[X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)]

If X is a matrix, then diff(X) returns a matrix of row differences: [X(2:m,:)-X(1:m-1,:)]

Y = diff(X,n) applies diff recursively n times, resulting in the nth difference. For example, diff(X,2) is the same as diff(diff(X)). So for the first example above, if you type: z = diff(x,2) The result will be "z = 0 0 0"

Integrals work similar to derivatives. Matlab can also approximate implement integrals. There are many functions that can do this, such as: cumsum, trapz, quad, etc. The cumsum function is the most useful one.

The syntax is: B = cumsum(A). It returns the cumulative sum along different dimensions of an array. If A is a vector, cumsum(A) returns a vector containing the cumulative sum of the elements of A. If A is a matrix, cumsum(A) returns a matrix the same size as A containing the cumulative sums for each column of A.

X=cos(n^2)*sin(2*pi*n/5)*u(t) and h=((0.9)^n)*(u(t)-u(t-10))

Then the code will be:

n=0:99;

unit=ones(1,100);

unit_10=[ones(1,11) zeros(1,89)];

h=((0.9).^n).*unit_10;

x=cos(n.^2).*sin(2*pi*n/5).*unit;

y=conv(x,h);

subplot(2,2,1);

stem(n,x);

subplot(2,2,2);

stem(n,h);

subplot(2,2,3);

stem(n,y(1:100));

The result is:

Filter and sampling processing will be taught in detail in ECEn380 class, you can also check it in the attached document.

Help and look for function

The Help function is the most useful function in Matlab. It lists all primary help topics in the Command Window. Each main help topic corresponds to a directory name on the MATLAB search path.

The Syntax of help function is:

help /

This command lists all operators and special characters, along with their descriptions.

help functionname

This command displays M-file help, which is a brief description and the syntax for functionname, in the Command Window. The output includes a link to doc functionname, which displays the reference page in the Help browser, often providing additional information. I think that doc link is the most useful link in Matlab. It should contains all you want to know.

A very convenient tool of MATLab 7.0 for graphing

If you don't like coding and still want to see the behavior of a function or two functions, the easiest way to do that in MatLab 7.0 is using the "funtool" function.

As you see, you can do almost all the common opertions between these two functions or just one function itself. The result plot will show up in Figure 2 or Figure 3.

If you have any other questions, you can find the answers in this detailed matlab tutorial pdf file from BYU Department of Physics and Astronomy.