A Brief Introduction to MATLAB MATLAB (Matrix Laboratory) is an interactive software system for numerical computations and graphics. As the name suggests, MATLAB was first designed for matrix computations: solving systems of linear equations, computing eigenvalues and eigenvectors of matrices, factoring matrices, and so on. Throughout the years, it has become a computer language for technical computing. It has a variety of computational and graphical capabilities. MATLAB is installed locally on each PC in Computer Labs: Thompson 215 and Thompson 220. TostarttheMATLABprogram,clickonStart All Program MATLAB R2011b (or MATLAB R2014). You will see the following screen with three windows. (It may take a while for MATLAB to be up and run, so please be patient.) The one at the top left is Current Directory, the bottom left is Command History and the one at the right is Command Window. If you click on Desktop on the toolbar, you see these three are marked. You can unmark any of them if you want to close it. On the toolbar, next to Current Directory, type in directory (or your flash-drive) where your files will be (or are). A list of files under that directory should be shown in the Current Directory window on the left. At the top of the Command Window, it lists Video, Demos and Getting Started. Check out these tutorials to learn more about MATLAB. Let us go through some basic MATLAB commands. 1. To Quit To quit from MATLAB, type "quit" at the MATLAB prompt, i.e. quit (enter) 1
2. Help: Type help with a command or a name of an MATLAB build-in function to see its description. For example, help log YoucanalsoclickonHelpontoolbartogetmorehelp. 3. Using MATLAB as a calculator MATLAB can be used as an expression evaluator. To do this you simply type a mathematical expression after the MATLAB prompt. For example, we want to evaluate pi (enter) Now You see: ans 3.1416. If you need more decimal digits, type format long and then press the enter key. Now type in pi again. formate long pi 3.14159265358979 Type format short to return back to the 5 decimal digits display. pi is a built-in MATLAB variable. Try more examples: sin(pi/4) 2^(log2(4)) sqrt(9) 8^(1/3) 4. Assigning Values to Variables MATLAB allows you create variables. For example, x 5; y pi/4; z y sqrt(x) z 3.02146614089724 From this point on, x 5, y 4 and z 4 5 until you replace them. Use who command to list the currently active variables. who Your variables are: ans x y z Note that with having ";" at the end of a command, there will not be a display of the value of the variable after you press "enter". 5. Built-in Functions MATLAB has many built-in functions. For example, (try them to see what you get) abs(-1) sin(pi/2) cos(pi/3) 2
tan(pi/4) exp(0.2) atan(1) log(exp(1)) log2(4) Guess and try a function yourself. How does it work? After you use MATLAB for a while, you can guess some commands. 6. Operations (1) Operations:,,. /,^ Try the following. 2^(1/2)-sqrt(2) 3*exp(-1/2)*sin(pi/3) 2*log(3) (2) Dot operations:.,./,.^ Each operation listed in (1) can operate on vectors and matrices by adding a dot (.) in front of it. Here are examples. Example 1: Build 6 column vectors. x [1;1;2;3;5;8]; y 2*x; z x.*x; w x.^3; u x./5; v x./x; [xyzwuv] ans 1.0000 2.0000 1.0000 1.0000 0.2000 1.0000 1.0000 2.0000 1.0000 1.0000 0.2000 1.0000 2.0000 4.0000 4.0000 8.0000 0.4000 1.0000 3.0000 6.0000 9.0000 27.0000 0.6000 1.0000 5.0000 10.0000 25.0000 125.0000 1.0000 1.0000 7. Set up and Plot Data Matrix (Arrays): a. Graph a function: Example: Graph y sin x e x x 2 1, for x. x -pi:0.1:pi; (Note: x is a row vector that contains values: -pi, -pi 0.1, -pi 0.2,...,pi, with adding ";" at the end, elements of the vector x will not be displayed). y x.*sin(x)-exp(-x) x.^2 1; plot(x,y) Though y is continuous function of x, only finite many points are plotted. MATLAB makes a smooth connection between two points. Type Help plot to learn more about line and point formats for plot. 3
b. Set up a data matrix: If we have a table of data: (Example 1 on Page 11) 9.6 8.7 18.3 10.7 20.0 18.2 47.2 23.9 71.1 48.0 119.1 55.5 174.6 82.7 now we want to load data in a matrix labeled A. In MATLAB, select File New Script. An Editor window is opened: Type in the data (or copy/paste from a text file). Add "A [" at the beginning of the file and "];" at the end. Now save the file as data1.m. Make sure the m-file is saved in the directory that is listed at the Current Directory. Then in the Command Window, we type in data1 (without.m), and check to see if the matrix A is definite: data1 A 1.0e 002 * 0.096000000000000 0.087000000000000 0.183000000000000 0.107000000000000 0.200000000000000 0.182000000000000 0.472000000000000 0.239000000000000 4
0.711000000000000 0.480000000000000 1.191000000000000 0.555000000000000 1.746000000000000 0.827000000000000 To plot the 2nd column of A vs. the 1st column of A: clf (comment: clear the screen) plot(a(:,1),a(:,2), b* ) hold on plot(a(:,1),0.5*a(:,2), r- ) hold off Go to the Figure Window, you should see the following graph: 8. Programs - MATLAB m-files: Select File New Script, type in % Example 2 - Section 1.2 % clear clf data1 nv 1:1:18; nv nv ; B A2(:,1); subplot(3,1,1),plot(nv,b(1:18,1), b* ) xlabel( n ) ylabel( pn ) for i 1:18 B(i,2) B(i 1,1)-B(i,1); end 5
subplot(3,1,2),plot(b(1:17,2),b(1:17,1), r* ) xlabel( pn ) ylabel( pn 1-pn ) for i 1:18 B(i,3) B(i,1)*(665-B(i,1)); end subplot(3,1,3),plot(b(1:17,3),b(1:17,2), k* ) xlabel( pn(665-pn) ) ylabel( pn 1-pn ) save as an m-file: Ex2_1_2.m Ex2_1_2 6