Unformatted text preview:

Using CommandsMATLAB is Vector/Matrix-OrientedCreating Matrices or ArraysExtracting Data for Inclusion in HW or ReportsWriting MATLAB ProgramsMATLAB Tutorial EECE 301 Prof. Fowler We will be using MATLAB in EE301 to illustrate MATLAB is available on the computers on campus. You can also buy a student version to put on your own computer. In addition to this tutorial The MathWorks (the company that makes MATLAB) has a tutorial available. I have links to it on my EECE 301 web page. Be aware that there are lots of parts of MATLAB that we won’t need, so if you run across something that sounds unfamiliar don’t worry about it (for example, there are lots of matrix commands such eig and svd that we won’t be needing; there are also lots of fancy plotting functions that we won’t need, either). © 2005 Mark L. Fowler2Using Commands MATLAB has two ways it can be used: (1) as a command line environment and (2) as a program-running environment; in fact, the two environments can be used together. Command Line Environment: You issue commands at a prompt and MATLAB returns results; the results can be stored in variables that can be named as desired (within certain limitations). Program-Running Environment: You call a program from the prompt; the program does a series of commands that have been stored in a so-called “m-file”. An m-file can, if desired, accept multiple inputs variables and return multiple output variables. In this tutorial handout I will mostly discuss the command-line environment. Once you start up MATLAB you get a window that has a command line prompt like this: >> Once you have that prompt you can issue MATLAB commands. Here are some you can try: “pwd” stands for “Present Working Directory” >> pwd ans = C:\MATLAB_S (your computer’s response here will likely be different). This returns the name of the “present working directory”; it is the most accessible directory for MATLAB. When you perform a load or save command (discussed later) it operates on this directory (unless otherwise specified to be some other directory). To change the pwd, use the cd command (it works much like the DOS cd command); type >> help cd for more info. You can also change the directory using the GUI interface of MATLAB.3Getting Help >> help command_name will provide help on the various commands. For example, >> help sum provides help on the “sum” command that sums the elements in a vector. There is even help on the help command: >> help help give help on using help. Also just typing help gives some information useful for finding help on the various types of MATLAB commands there are: >> help gives a list of topics or categories into which MATLAB commands are grouped. Then you can get help on a specific topic by entering: >> help topic_name For example, one of the topics listed when the command “help” is issued is “elfun,” which stands for “elementary math functions” so that entering >> help elfun gives a list of trigonometric functions, exponential functions, numeric functions (such as “round” and “sign”), and functions for dealing with complex numbers (such as “abs”, “angle”, “conj”, “real”, and “imag”). Another way to get help is to use the “lookfor” command. It searches for commands whose help files contain a specified word. For example, >> lookfor cosine provides a list of functions that mention the word “cosine” in their help files.4MATLAB is Vector/Matrix-Oriented Much of the power of MATLAB comes from the fact that it can operate simultaneously on all the elements in a vector or matrix (unlike C, which operates only on individual elements at a time). Create vectors: >> x=1:10; creates the 1x10 row vector of the first 10 integers and assigned it to the variable. The ; at the end of this command suppressed the result of this assignment . To see the result of the assignment do this: >> x x = 1 2 3 4 5 6 7 8 9 10 >> y=1:2:10 y = 1 3 5 7 9 allows the step size to be varied. Column vectors can also be created. The easiest way is to transpose (or “rotate by 90 degrees”) a row vector: >> y=(1:2:10)’ y = 1 3 5 7 9 Warning: for complex-valued vectors use x.’ instead of x’ because the later will transpose it and conjugate it.5Operate on Vectors: This is where MATLAB really shines. There is usually no need, as in C, to write loops to operate in vectors and matrices. Instead, most operations work simultaneously on all elements of the vector or matrix. >> x=1:10; >> y=1:2:20; >>z=x+y z = 2 5 8 11 14 17 20 23 26 29 gives the element-by-element sum of the two vectors x and y. We can use vectors to represent functions of time: First define a series of time points in a vector t: >> t=0:0.01:5; % creates a vector of times spaced every 1/100th of a second Then we can use that time vector to create a function vector by using operations on the vector t. For example, to create a vector y that takes on values of a quadratic function of t we could do: >> y=3*(t.^2) + t + 5; which creates a vector y having the same size as t whose elements are the values of evaluated at the time points specified in the vector t. 53)(2++= ttty Notice the dot before the ^ (the power sign). That indicates that it operates on the vector t on an element-by-element basis. This is called “array power” in MATLAB. Without the dot it is called “matrix power” and performs a more complicated power operation defined in matrix theory (we won’t need that, here). Type “help arith” for more info. This “dot” modifier to imply element-by-element operations also holds for multiplication (e.g., x.*y is element-by-element multiplication of vectors x and y, where as x*y is matrix multiplication) and for division (x./y is element-by-element division; without the dot it is a more complicated matrix mathematics operation that we won’t need here). ramp function:r(t) = tu(t) Here is another example: to compute )5sin(2)()(ttrtyπ+= we do the following. >> t=-5:0.01:5; % create a vector of time points from –5 seconds to 5 seconds >> u=sign(t+eps); % computes unit step function – the eps variable is the % smallest number that MATLAB can represent; adding it to t here avoids % sign(0) for which the result is 0, and


View Full Document

BU EECE 301 - MATLAB Tutorial

Download MATLAB Tutorial
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view MATLAB Tutorial and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view MATLAB Tutorial 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?