Professional Documents
Culture Documents
3: Introduction to MATLAB
E26-1
You can enter matrices into MATLAB in several different ways, including: o manually as an explicit list of elements, o as data loaded from an external file, o generated using functions built into MATLAB, or o generated with your own functions (i.e., your own M-files). To enter matrices manually, you have only to follow a few basic conventions: o separate the elements of a row with blanks or commas, o use a semicolon (;) to indicate the end of each row, and o surround the entire list of elements with square brackets, [ ]. For example, to enter the 3x3 matrix A , shown below:
2 3 1 A= 1 5 1 7 1 2
(A.3-1)
Note that MATLAB will echo the results of carrying out each command as it is entered; in this case, MATLAB will display the A matrix that was created after you press the Enter key.
E26-2
A = 2 1 7 3 5 1 1 1 2
The echo can be turned off by using a semicolon after your command (the opposite of Maple where the semicolon requests an echo). For example, typing:
>> A = [2 3 1; 1 5 1; 7 1 2];
would not be echoed in the working environment. If you ever want to see what the MATLAB variable A is, just type A and press the Enter key:
>> A A = 2 1 7 3 5 1 1 1 2
(A.3-2)
(A.3-3)
E26-3
MATLAB has an extensive help system that can be accessed either by typing help followed by the function you are interested in or by selecting MATLAB Help from the Help menu. To obtain help relative to the use of the exponential function:
>> help exp EXP Exponential. EXP(X) is the exponential of the elements of X, e to the X. For complex Z=X+i*Y, EXP(Z) = EXP(X)*(COS(Y)+i*SIN(Y)). See also expm1, log, log10, expm, expint. Overloaded functions or methods (ones with the same name in other directories) help sym/exp.m Reference page in Help browser doc exp
The details of the help message may differ depending on what version of MATLAB you are using; however, the specific capabilities of MATLAB that we will be using should be compatible with any version later than 5.0. Note that, unlike EES, variables in MATLAB are case-sensitive. Typically, we will be assigning or addressing the elements in a vector or array using subscripts. The element in row i and column j of matrix A is denoted by A(i,j). For example, A(2,1) is the number in the second row and first column; for matrix A in Eq. (A.3-1), A(2,1) is 1 and A(3,1) is 7. If you want to change the value of A(2,1) from 1 to 8, simply re-assign it:
>> A(2,1)=8; >> A A =
E26-4
2 8 7
3 5 1
1 1 2
It is possible to extract or manipulate entire rows or columns of a matrix. To obtain the 2nd column of A , use the : symbol which indicates all entries; therefore, A(:,2) would indicate all rows and column 2.
>> A(:,2) ans = 3 5 1
The mathematical operators +, -, *, / etc. have their typical meanings when applied to scalar arguments. However, if these (and any) operator is applied to a matrix then the operator will refer to the matrix form of the operation. For example, Ab is the matrix multiplication of the matrix A by the vector b (you may need to review your linear algebra to remember what this means):
>> A*b ans = 13 23 19
The order matters for matrix multiplication; that is b A will not be equal to Ab , in fact, b A is not even possible, as you cannot multiply a 3x1 matrix with a 3x3 matrix:
>> b*A ??? Error using ==> mtimes Inner matrix dimensions must agree.
You may want to avoid carrying out matrix operations and, instead, operate on each element in a matrix individually. This can be accomplished by placing a period (.) in front of the operator. For example, the operation A*A leads to the matrix multiplication of A by itself:
>> A*A ans = 35 63 36 22 50 28 7 15 12
E26-5
E26: Appendix A.3: Introduction to MATLAB whereas the operation A .*A leads to each element in A being squared.
>> A.*A ans = 4 64 49 9 25 1 1 1 4
The results are quite different and most beginners with MATLAB will forget that commands are interpreted according to operations on matrices.
E26-6
When you run a script, MATLAB simply executes the commands that are contained in the Mfile. Scripts can operate on existing data in the workspace, or they can create new data on which to operate. Although scripts do not formally return output arguments, any variables that they create remain in the workspace and can therefore be used in subsequent computations. In addition, scripts can produce graphical output using functions like plot. Functions are M-files that can accept input arguments and return output arguments. The name of the M-file and of the function should be the same. Functions operate on variables within their own workspace which is separate from the workspace at the MATLAB command prompt. Both types of M-files will be needed to generate numerical solutions to heat transfer problems. Open a new M-File and save it as practice.m in a directory that is contained in MATLABs search path. Comments can be used to clarify the code contained in M-files. Comments are indicated by the % symbol; anything following the % symbol is not executed. In your practice.m script, specify the radius and calculate the area of a disk:
r=2; %radius of disk A=pi*r^2; %area of disk
Save the file and execute it from the main workspace; nothing appears to happen but if you type r or A then you can see that these variables have been assigned.
>> practice >> A A = 12.5664
E26-7
One of the nice things about using MATLAB is that it is easy to debug as you go along; for example, if you were unsure as to the value of r in the script then simply remove the semicolon:
r=2 %radius of disk A=pi*r^2; %area of disk
and MATLAB will echo the value to the workspace as it executes the script.
>> practice r = 2
Scripts can be turned into functions. The header of the function has the format: function[output argument(s)]=function name(input argument(s)) The keyword function tells MATLAB that you are creating a function rather than a script. In MATLAB (unlike EES) more than one output argument can be returned from the function; these go in the square brackets. For example, our script practice.m can be turned into a function that takes r as an input and returns A as an output:
function[A]=practice(r) A=pi*r^2; %area of disk
You can call the function practice from any other function or the main workspace.
>> A=practice(1) A = 3.1416
It is important to understand that functions have their own workspace and only communicate with the calling workspace via the input/output protocol. For example, we could try to re-assign the input argument r within the function:
function[A]=practice(r) A=pi*r^2; r=5; %area of disk %attempt to re-assign r
Executing practice will not affect the value of r in the workspace. E26-8
%area of disk
Note that the statements within the if and else statements are indented; this is good form and makes it very easy to understand the structure of the code. Execute the function practice from the workspace and check if it is working.
>> practice(1) ans = 3.1416 >> practice(6) ans = 999
Notice that the if statement is not followed by then as in most languages. Also, the relational operators are familiar with the exception of equality, which is indicated by = = rather than =; to see the list of relational operators (and other things) type help < in the workspace. Finally, dont forget that all of these programming constructs must be terminated with an end keyword.
E26-9
E26: Appendix A.3: Introduction to MATLAB The for loop is analogous to the DUPLICATE statement in EES; it executes a statement (or several statements) a specific number of times. It will not be possible to carry out numerical solutions to heat transfer problems without using a for loop. The for keyword is followed by an integer variable and a range; e.g., for i=1:10 statement(s) end would execute the statements sandwiched between for and end 10 times; each time the variable i would take on a different value. We can make the function practice return a 10x1 vector in which the first element is the input argument r and each subsequent element is twice the previous one:
function[A]=practice(r) A(1,1)=r; for i=2:10 A(i,1)=A(i-1,1)*2; end
By nesting for loops you can generate or operate on matrices rather than vectors. Finally, the while statement repeats a set of statement until some break condition is met; often, the while statement is used to terminate an iteration process within a numerical method based on achieving convergence. Here we can rewrite the practice function so that it stops adding values to the vector when they get larger than 4000.
i=1; A(i,1)=r; while(A(i,1)<4000) i=i+1; A(i,1)=A(i-1,1)*2; end
E26-10
There are many other capabilities of MATLAB and we will introduce several additional aspects of the software in the context of various problems; however, this should be sufficient to get you started.
E26-11