Sie sind auf Seite 1von 32

ADJUSTMENT COMPUTATIONS

(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.

 MATLAB returns output in two ways


 Text & numerical – appears in the same command
windows
 Graphic – appears in separate figure windows.

 While MATLAB is working – it may display a “wait”


symbol

© Khairulnizam M Idris, FGRE UTM


Rules on Variable and
Function Names

 Variable/Function name

 begins with a LETTER, e.g., A2z.


 can be a mix of letters, digits, and underscores (e.g., vector_A,
but not vector-A (since "-" is a reserved char).
 is case sensitive, e.g., NAME, Name, name are 3 distinct
variables.
 must not be longer than 31 characters.

3 © Khairulnizam M Idris, FGRE UTM


MATLAB: Introduction to
Arithmetic
 MATLAB – can do arithmetic – as calculator.

Arithmetic Operator Expression


+ Addition
- Subtraction
* Multiplication
/ Division
^ Power
( ) Specify evaluation order

Example:

© Khairulnizam M Idris, FGRE UTM


MATLAB: Introduction to
Arithmetic (cont.)

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.

© Khairulnizam M Idris, FGRE UTM


MATLAB: Introduction to
Arithmetic (cont.)

Example:

Can assign computed value


to specific variable

© Khairulnizam M Idris, FGRE UTM


Arithmetic
As we have just seen, we can use MATLAB to do arithmetic as
we would with a calculator. Arithmetic operators include;

>> 3ˆ2 - (5 + 4)/2 + 6*3


ans =
22.5000

MATLAB prints the answer and assigns the value to a variable


called ans. If you want to perform further calculations with
the answer, we can use the variable ans rather than retype
the answer.
For example;
Addition +
>> ansˆ2 + sqrt(ans) Subtraction -
ans = Division /
510.9934
Multiplication *
Exponent ^
7 © Khairulnizam M Idris, FGRE UTM
Arithmetic …
MATLAB uses double-precision floating-point arithmetic,
which is accurate to approximately 15 digits; however,
MATLAB displays only 5 digits by default. To display more
digits, use command;

>>format long ↵

Then all subsequent numerical output will have 15 digits


displayed. Type format short to return to 5-digit display.

>>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

To enter more than one


statement at once:
 Use commas (,)
 Use semicolons (;)
Recovering from Problem
• Unavoidably – when using any mathematical software – we
are bound to encounter minor glitches.

• May accidently mistype an entry, inadvertently or


careless violate a MATLAB rule.

• Example: error in an input line

“MATLAB will normally print an error message.” – the error


is a missing multiplication operator *.

11 © Khairulnizam M Idris, FGRE UTM


Algebraic or Symbolic
Computation
• MATLAB’s Symbolic Math Toolbox – can carry out algebraic
or symbolic calculations – e.g. factoring polynomial –
algebraic equations.

• To perform symbolic computation – syms to declare the


variables – plan to use to be symbolic variables

“The command expand told


MATLAB to multiply out the
expression”

“The factor forced MATLAB to


restore it to factored form”

12 © Khairulnizam M Idris, FGRE UTM


Algebraic or Symbolic
Computation (cont.)

• MATLAB has a command called simplify – use to express a


formula as simply as possible.

“more robust command – simple – better job than simplify”

Try this?

13 © Khairulnizam M Idris, FGRE UTM


Algebraic or Symbolic
Computation (cont.)

• When work with symbolic


expression – often need to
substitute a numerical value –
or even another symbolic
expression.

14 © Khairulnizam M Idris, FGRE UTM


Mathematic Function
 MATLAB offer any predefined mathematical function
for technical computing.

 Typing help elfun & help specfum calls up list


elementary & special functions.

 There is a long list of mathematic functions – built


into MATLAB.

 These functions are called built-ins,

 Standard functions: sin (x), cos (y), tan(z)

© Khairulnizam M Idris, FGRE UTM


Math. Function: Examples (cont.)

e.g. Math problem – elementary function


Where,

Math problem deploy in MATLAB

Answer:

© Khairulnizam M Idris, FGRE UTM


MATLAB Tips
“Clearing the Workspace”

© Khairulnizam M Idris, FGRE UTM


Matrices and Arrays
 Several ways to enter a matrix:
a. Separate the elements of a row with
blanks or commas.
b. Use a semicolon ; to indicate the
end of each row.
c. Surround the entire list of
elements with square brackets [ ].

© Khairulnizam M Idris, FGRE UTM


Matrices and Arrays (Cont.)

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]

MATLAB displays the matrix you just entered:


A=
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

© Khairulnizam M Idris, FGRE UTM


Generating Matrices
MATLAB provides four functions that generate basic
matrices.
 zeros - All zeros
 ones - All ones
 rand - Uniformly distributed random elements
 randn - Normally distributed random
elements
 eye - This creates an identity matrix.

© Khairulnizam M Idris, FGRE UTM


Generating Matrices (Cont.)

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

© Khairulnizam M Idris, FGRE UTM


Matrices Functions
Much of MATLAB's power comes from its matrix functions. The most useful ones are:

det determinant of amatrix


inv inverse of a matrix
size size of a matrix
eig eigenvalues and eigenvectors
chol cholesky factorization
svd singular value decomposition
lu LU factorization
qr QR factorization
hess hessenberg form
schur schur decomposition
rref reduced row echelon form
expm matrix exponential
sqrtm matrix square root
poly characteristic polynomial
norm 1-norm, 2-norm, F-norm, infinity-norm
cond condition number in the 2-norm
rank rank

© Khairulnizam M Idris, FGRE UTM


Vectors and Matrices
Vectors
A vector is an ordered list of numbers. We can enter
a vector of any length in MATLAB by typing a list of
numbers, separated by commas and/or spaces, inside
square brackets. For example:
>> Z = [2,4,6,8]
Z =
2 4 6 8
>> Y = [4 -3 5 -2 8 1]
Y =
4 -3 5 -2 8 1

23 © Khairulnizam M Idris, FGRE UTM


Vectors and Matrices (cont.)

Suppose we want to create a vector of values running


from 1 to 9. Here’s how to do it without typing each
number:

>> X = 1:9
X =
1 2 3 4 5 6 7 8 9

The notation 1:9 is used to represent a vector of


numbers running from 1 to 9 in increments of 1. The
increment can be specified as the middle of three
arguments:

>> X = 0:2:10
X =
0 2 4 6 8 10

24 © Khairulnizam M Idris, FGRE UTM


Vectors and Matrices (cont.)

Increments can be fractional or negative, for example,


X = 0:0.1:1 or X = 100:-1:0
The elements of the vector X can be extracted as X(1), X(2), etc. For example:
>> X(3)
ans = 4

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

25 © Khairulnizam M Idris, FGRE UTM


Vectors and Matrices (cont.)

We can perform mathematical operations on vectors. For


example, to square the elements of the vector X, type
>> X = [2 4 6 8]; X.^2
ans = 4 16 36 64

The period in this expression is very important; it says


that the numbers in X should be squared individually, or
element-by-element. Typing Xˆ2 would tell MATLAB to use
matrix multiplication to multiply X by itself and would
produce an error message in this case.

Similarly, you must type .* or ./ if you want to multiply


or divide vectors element-by-element. For example, to
multiply the elements of the vector X by the corresponding
elements of the vector Y, type

>> X = [1 2 3 4]; Y = [5 6 7 8];


>> X.*Y
ans = 5 12 21 32
26 © Khairulnizam M Idris, FGRE UTM
Vectors and Matrices (cont.)

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

It can be entered in MATLAB with the command

>> A = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12]


A=
1234
5678
9 10 11 12

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;

>> A = [ 1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12];


>> B = [11, 12, 13, 14; 15, 16, 17, 18; 19, 14, 15, 16];
>> A + B
ans =
12 14 16 18
20 22 24 26
28 24 26 28
>> A - B
ans =
-10 -10 -10 -10
-10 -10 -10 -10
-10 -4 -4 -4

28 © Khairulnizam M Idris, FGRE UTM


Vectors and Matrices (cont.)

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

29 © Khairulnizam M Idris, FGRE UTM


Vectors and Matrices (cont.)

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.

>> A = [ 1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12];


>> B = [11, 12, 13, 14; 15, 16, 17, 18; 19, 14, 15, 16];
>> A * B
??? Error using ==> mtimes
Inner matrix dimensions must agree.
>> B = [11; 12; 13; 14;];
>> A * B
ans =
130
330
530

30 © Khairulnizam M Idris, FGRE UTM


M-Files
1. M-files allow us to save multiple MATLAB commands in a file and then run them with a single
command or mouse click. While we may have solved the simple problem correctly on the first
try, more complicated problems generally require some trial and error – running, editing, and
re-running a series of commands several times.

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.

5. Extension of any M-file must be “.m”

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

Das könnte Ihnen auch gefallen