Beruflich Dokumente
Kultur Dokumente
(SUG610)
MATLAB
Dr. Sr Mohd Azwan Abbas
mohdazwanabbas@perlis.uitm.edu.my
Office: STAR Complex (F214)
Phone: 04-9882265
MATLAB: Basic Input &
Output
Input command to MATLAB in the command windows.
Variable/Function name
Example:
Example:
If to
perform
MATLAB print the answer & further
assigns the value to a computation
with the
variable called ans. answer – can
use the
variable
Example: ans.
Example:
>>format long ↵
>>format short ↵
Aborting Calculations
If MATLAB gets hung up in a calculation, or seems to be
taking too long to perform an operation, you can usually
abort it by typing CTRL+C.
Suppressing Output
Typing a semicolon (;) at the end of an input line
suppresses printing of the output of the MATLAB command.
8 © Khairulnizam M Idris, FGRE UTM
Example - Arithmetic
Problem in mathematic form:
true
?
false
?
Precedence Mathematical Operations
First (1st) The contents of all parentheses are evaluated first, starting from the innermost parentheses and
working outward.
Second (2nd) All exponentials are evaluated, working form left to right..
Third (3rd) All multiplications and divisions are evaluated, working form left to right.
Fourth (4th) All additions and subtraction are evaluated, starting from left to right.
Example – Entering Multiple
Statement per Line
Try this?
Answer:
Example:
A = [16,3,2,13; 5,10,11,8; 9,6,7,12; 4,15,14,1]
A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
Examples:
(i)Z = zeros(2,4) (ii) X = ones(1,5)
Z= X=
0000 11111
0000
(iii)rand(2,3)
ans =
0.9501 0.6068 0.8913
0.2311 0.4860 0.7621
>> X = 1:9
X =
1 2 3 4 5 6 7 8 9
>> X = 0:2:10
X =
0 2 4 6 8 10
To change the vector X from a row vector to a column vector, put a prime (’) after X:
>> X = [2 4 6 8];
>> X’
ans =
2
4
6
8
Matrices
A matrix is a rectangular array of numbers. Row and column vectors are examples of matrices. The
order of a matrix is defined as (rows x columns). Consider the (3 × 4) matrix
Note that the matrix elements in any row are separated by commas or spaces, and the rows are
separated by semicolons (;).
27 © Khairulnizam M Idris, FGRE UTM
Vectors and Matrices (cont.)
If two matrices A and B are the same size, their (element-by-element) sum (A+B) and difference
(A-B)is obtained as;
We can also add/subtract a scalar (a single number) to/from a matrix such as;
>> A = [ 1 2 3 4; 5 6 7 8; 9 10 11 12 ];
>> A + 2
ans =
3 4 5 6
7 8 9 10
11 12 13 14
>> ans - 2
ans =
1 2 3 4
5 6 7 8
9 10 11 12
If A and B are multiplicatively compatible, i.e., if A is n×m and B is m×l, then their product A*B is n ×
l.
2. While the Command History Window can be useful during the first stages but m-files are more
efficient to use. M-files also allow you to share your solution to a problem with other MATLAB
users, and to format your results for others to read.
3. M-files are ordinary text files containing MATLAB commands and can created using any text
editor or word processor that is capable of saving files as plain ASCII text. More conveniently,
you can use the built-in Editor
4. There are two different kinds of M-files: script M-files and function M-files.
31
M-Files …
Create a file containing the following lines:
format long
x = [0.1, 0.01, 0.001];
y = sin(x)./x
% These values illustrate the fact that the limit of
% sin(x)/x as x approaches 0 is 1.
We will assume that you have saved this file with the name task1.m in your current directory, or in
some directory in your path. In MATLAB, the percent sign (%) begins a comment; the rest of the
line is not executed by MATLAB.
Now we can tell MATLAB to run (or execute) this script by typing the following command in the Command
Window;
>> task1
Remember, must not type the “.m” extension, as MATLAB automatically adds it when searching for M-files.)
The output – but not the commands that produce them – will be displayed in the Command Window.
32