 (Parte 1 de 9)

Basics of MATLAB

1 First Steps in MATLAB

1.1 Starting MATLAB matlab is a software package that lets you do mathematics and computation, analyse data, develop algorithms, do simulation and modelling, and produce graphical displays and graphical user interfaces.

To run matlab on a PC double-click on the matlab icon. To run matlab on a unix system, type matlab at the prompt.

You get matlab to do things for you by typing in commands. matlab prompts you with two greater-than signs (>>) when it is ready to accept a command from you.

To end a matlab session type quit or exit at the matlab prompt. You can type help at the matlab prompt, or pull down the Help menu on a PC. When starting matlab you should see a message:

To get started, type one of these commands: helpwin, helpdesk, or demo >>

The various forms of help available are helpwin Opens a matlab help GUI helpdesk Opens a hypertext help browser demo Starts the matlab demonstration

The complete documentation for matlab can be accessed from the hypertext helpdesk. For example, clicking the link Full Documentation c© 2000 by CRC Press LLC

Set → Getting Started with MATLAB will download a portable document format (PDF) version of the Getting Started with MATLAB manual.

You can learn how to use any matlab command by typing help followed by the name of the command, for example, help sin.

You can also use the lookfor command, which searches the help entries for all matlab commands for a particular word. For example, if you want to know which matlab functions to use for spectral analysis, you could type lookfor spectrum. matlab responds with the names of the commands that have the searched word in the ﬁrst line of the help entry. You can search the entire help entry for all matlab commands by typing lookfor -all keyword.

1.2 First Steps To get matlab to work out 1 + 1, type the following at the prompt:

1+1 matlab responds with

The answer to the typed command is given the name ans. In fact ans is now a variable that you can use again. For example you can type

ans*ans ans = 4 matlab has updated the value of ans to be 4.

The spacing of operators in formulas does not matter. The following formulas both give the same answer:

The order of operations is made clearer to readers of your matlab code if you type carefully:

c© 2000 by CRC Press LLC

1.3 Matrices

The basic object that matlab deals with is a matrix. A matrix is an array of numbers. For example the following are matrices: stands for 1 × 106—and the (3,2)-element is pi = π =3 .14159

The size of a matrix is the number of rows by the number of columns. The ﬁrst matrix is a 3×3 matrix. The (2,3)-element is one million—1e6 The second matrix is a row-vector, the third matrix is a column-vector containing the number i, which is a pre-deﬁned matlab variable equal to the square root of −1. The last matrix is a 1 × 1 matrix, also called a scalar.

Variables in matlab are named objects that are assigned using the equals sign = . They are limited to 31 characters and can contain upper and lowercase letters, any number of ‘_’ characters, and numerals. They may not start with a numeral. matlab is case sensitive: A and a are diﬀerent variables. The following are valid matlab variable assignments:

a=1 speed = 1500 BeamFormerOutput_Type1 = v*Q*v’ name = ’John Smith’

These are invalid assignments:

2for1 = ’yes’ first one = 1

To assign a variable without getting an echo from matlab end the assignment with a semi-colon ;. Try typing the following:

a=2 b=3 ; c = a+b; d = c/2; d who whos clear who c© 2000 by CRC Press LLC

1.5 The Colon Operator

To generate a vector of equally-spaced elements matlab provides the colon operator. Try the following commands:

The syntax x:y means roughly “generate the ordered set of numbers from x to y with increment 1 between them.” The syntax x:d:y means roughly “generate the ordered set of numbers from x to y with increment d between them.”

1.6 Linspace

To generate a vector of evenly spaced points between two end points, you can use the function linspace(start,stop,npoints):  generates 10 evenly spaced points from 0 to 1. Typing linspace(start, stop) will generate a vector of 100 points.

1.7 Plotting Vectors

Whereas other computer languages, such as Fortran, work on numbers one at a time, an advantage of matlab is that it handles the matrix as a single unit. Let us consider an example that shows why this is useful. Imagine you want to plot the function y = sinx for x between 0 and 2π. A Fortran code to do this might look like this:

DIMENSION X(100),Y(100) PI = 4*ATAN(1) DO 100 I = 1,100 X(I) = 2*PI*I/100 Y(I) = SIN(X(I)) 100 CONTINUE PLOT(X,Y)

Here we assume that we have access to a Fortran plotting package in which PLOT(X,Y) makes sense. In matlab we can get our plot by typing:

c© 2000 by CRC Press LLC

x = 0:.1:2*pi; y = sin(x); plot(x,y)

The ﬁrst line uses the colon operator to generate a vector x of numbers running between 0 and 2π with increment 0.1. The second line calculates the sine of this array of numbers, and calls the result y. The third line produces a plot of y against x. Go ahead and produce the plot. You should get a separate window displaying this plot. We have done in three lines of matlab what it took us seven lines to do using the Fortran program above.

2 Typing into MATLAB

2.1 Command Line Editing

If you make a mistake when entering a matlab command, you do not have to type the whole line again. The arrow keys can be used to save much typing:

↑ ctrl-p Recall previous line ↓ ctrl-n Recall next line ← ctrl-b Move back one character → ctrl-f Move forward one character ctrl-→ ctrl-r Move right one word ctrl-← ctrl-l Move left one word home ctrl-a Move to beginning of line end ctrl-e Move to end of line esc ctrl-u Clear line del ctrl-d Delete character at cursor backspace ctrl-h Delete character before cursor ctrl-k Delete (kill) to end of line

If you ﬁnish editing in the middle of a line, you do not have to put the cursor at the end of the line before pressing the return key; you can press return when the cursor is anywhere on the command line.

2.2 Smart Recall

Repeated use of the ↑ key recalls earlier commands. If you type the ﬁrst few characters of a previous command and then press the ↑ key c© 2000 by CRC Press LLC matlab will recall the last command that began with those characters. Subsequent use of ↑ will recall earlier commands that began with those characters.

2.3 Long Lines

If you want to type a matlab command that is too long to ﬁt on one line, you can continue on to the next by ending with a space followed by three full stops. For example, to type an expression with long variable names:

(Parte 1 de 9)