You are on page 1of 3

ORENTATION OF MATLAB

page-1

The purpose of this orientation is to allow you to get acquaintance with MATLAB in the math lab. Once you know how to get into the MATLAB, you will be given several exercises so that you can practice some commands in MATLAB for our MA237 class. Furthermore, you can use MATLAB as a computational tool for your homework. A primer, which contains many useful hints and commands, can be purchased at University Supestore for a little amount \$. MATLAB is an interactive and command based language. A line of input (a command) is typed in after a >> prompt. When you are finished typing in the command, hit the Enter or Return key. MATLAB will then immediately execute the command and display the result. You cannot correct a command once it is entered. The only thing you can do is to enter a corrected version of the command. Help Feature: The up and down arrow keys can be used to move backward and forward through the list of commands which you have previously entered. If you need to change a command, especially a long one, use the error keys to bring it to the prompt and correct it before pressing the Enter key. PART 1: STEP-BY-STEP INSTRUCTIONS OF USING MATLAB 1. Access to MATLAB. After you turn on the computer, insert your disk in driver A, use mouse to press the Start button and then select Math and select matlab. 2. Now you are in the MATLAB screen and you will see the MATLAB prompt >>. 3. Open a record file. It is a good habit to keep a record of your work. To do that type diary a:pract.txt <Enter> (or some other file name). 4. Define matrices. Let us define a number of matrices. >> A=[1 2 3; 4 5 6; 7 8 9] Alternatively, you can create the same matrix by >> A=[1, 2, 3; 4, 5, 6; 7, 8, 9]; Note: Please notice the difference when you have ; and when you dont have ; after each line. Now, lets generate a column vector b by typing >>b=[1; 1; 2] Once you hit the Enter key, you see the column vector b is displayed. However, if you enter a row vector, you should do >>r = [1, 2, 3] To generate the augmented matrix, type >>C=[A, b]; 5. Row operations >> C([1,2],:)=C([2,1],:) >> C(1,:)=2*C(1,:) %swap R1 and R2 %R1=2R1

Update e:\student\ma237\matlab\orient.doc

by

7/6/2013

ORENTATION OF MATLAB
page-2

6. Gaussian Elimination. We will use MATLAB commands to solve the example 7 on page 10 in the book. >>A=[0 2 1; 3, 5, -5; 2 4 -2], b=[-2; 1; 2] >>C=[A, b] >>C([1,3],:)=C([3,1],:) %swap R1 and R3 >>C(1,:) = * C(1,:) % *R1 >>C(2,:)=C(2,:)-3*C(1,:) %R2 3R1 >>C(2,:) = -C(2,:) %(-1)*R2 >>C(3,:) = C(3,:) 2*C(2,:) %R3 2R2 Now the matrix C is in a triangular form. We can find the solution by backward solve: >>x(3)=C(3,4)/C(3,3) >>x(2)=(C(2,4)-C(2,3)*x(3))/C(2,2) >>x(1)=(C(1,4)-C(1,3)*x(3)-C(1,2)*x(2))/C(1,1) The solution is in the vector x. >>x Let us change the matrix A back to the original one. You may use the up arrow key to find it; or you can retype the matrix A >>A=[1,2,3; 4,5,6;7,8,9]; Now let us consider the linear system Au = b. Is this system consistent? If so, write out the general solution. Before you exit, dont forget to close your diary file >>diary off As an exercise, solve the same problem above by Gauss-Jordan elimination.

Other Tips: % comment line. The MATLAB will ignore what you type after the sign %. Use the cursor up and down keys to retrieve the commands you previously typed in. >>save filename.mat (This command will save all current values of working variables, such as A, b, C and x in previous demostration, into the filename.mat.) >>load filename.mat (It retrieves the working variables when you need them in a later time. The variable names are also the same as the time you saved.) Please notice the difference between save and diary commands. Warning: Diary files cannot be reloaded into MATLAB using load command. Edit your record file, in MATLAB click the menu File , Open M-file , and then give the file name (a:pract.txt, for example).

Update e:\student\ma237\matlab\orient.doc

by

7/6/2013

ORENTATION OF MATLAB
page-3

PART II: INSTRUCTIONS OF MAKING AN M-FILE IN MATLAB

An M-file is a sequence of MATLAB commands. In this session, we will learn how to make an Mfile, edit it, and run it step by step.