Вы находитесь на странице: 1из 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.

1. Access to MATLAB.
2. To open a new file, click File, New, M-file. We are in Matlab editor, give a file name, say a:solve.M (note that the file extension has to be .M). (You may use any editor to edit an M-file. It is not necessary to edit your M-file in the LAB; in fact you can edit your file from your PC, as long as the file namewith an extension .M) Note that again, you can have .M file before entering MATLAB. 3. In the file we type A=[1 1 1 1; 2 4 8 16; 3 9 27 81; 4 16 64 256]; b=[1;9;36;100]; C=[A, b]; Cech=rref( C ) x=Cech(:, 5) % (this gives the solution)

4. Save the file by pulling down the file menu and select SAVE AS then give a file name for the
file.

5. Go back to MATLAB screen by clicking the MATLAB screen, or X out the editor screen.
6. Execute the M-file by typing >> solve <Enter> 7. Let us make the following changes: the M-file solve.m has an input A and b and an output x. It contains the commands. function x=solve(A,b) % x is the solution of Ax=b C=[A,b]; Cech=rref( C ); [m,n]=size( C); x=Cech(:,n); We then save it again before returning to MATLAB. 8. To execute the M-file in MATLAB, we have to prepare the matrix A and vector b. >>A=[1 1 1 1; 2 4 8 16; 3 9 27 81; 4 16 64 256]; >>b=[1;9;36;100]; >> x=solve(A,b) >> help solve % it prints the comments lines between the function and the first command. As an exercise, try to create an M-file called prct2.m that will perform the following: (1) Enter matrices A=[1,2; 2,3], B=[-3, 2; 1, 2], C=[1,0,-2;0,1,1], and D=[3,1;1,2;1,1] (2) Compute A*B, B+5*A, A*C, C*D, D*C, A^2 (A squared), A^5 (A raised to the 5 th power).

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

by

7/6/2013

Вам также может понравиться