Professional Documents
Culture Documents
MATLAB
by
Mohamed Hussein
compliments to
Prof. Michael Negnevitsky
Univerity of Tasmania
Lecture 1
Introduction to Matlab
Basic
Features
Scientific Features
Array Operations
Script Files or M-files
Basic Features
Matlab is a tool for doing numerical
computations with matrices and vectors. It
can also display information graphically.
I will give an introduction to some of the
most useful features of Matlab. I will include
plenty of examples.
The best way to learn to use Matlab is to run
Matlab, trying the examples and
experimenting.
MATLAB IDE
Simple Math
Operation
Symbol
Example
Addition, a + b
2+3
Subtraction, a b
54
Multiplication, a b
3.14 * 0.21
Division, a b
8/2
Power, ab
5^2
Order of precedence
1st
2nd
3rd
4th
8
11
a. 2 +
+ 5(82 )
15 5(3 + 4)
b. 7 251/4 + 120.37 + 5 10 3
answer:
a. 321.6925
b. 18.1653
Variables in Matlab
Variables are case sensitive (fruit, Fruit and
FRUIT).
Variables can contain up to 63 characters
(in MATLAB 7).
Variables must start with a letter, followed
by any number of letters, digits, or
underscores.
clc
exist (name)
quit
who
whos
: (colon)
, (comma)
; (semicolon)
(ellipsis)
% (percent)
format short
- (default) four decimal digits; ex. ans = 789.1234
format long
- 16 digits; ex. ans = 0.123456789123456
format short e - four decimal (maximum five digit ) with exponent;
ex. ans = 7.8912e+02 (denotes 7.8912 x 104)
format long e - 16 digits (with 15 decimals) with exponent;
ex. ans = 1.234567891234500e-01
(denotes 1.234567891234560 x10-1)
format bank - two decimal digits; ex. ans = 789.12
format +
- displays either +ve, -ve or zero; ex. ans = +
format rat
- approximation in ratio; ex. ans = 133/100 (from a
calculated value of 1.33)
Summary
Scientific Features
Matlab offers many common functions
important to mathematics, engineering, and
the sciences.
Matlab handles complex numbers.
Complex Numbers
>> a = 1; b = 5; c = 6;
>> x1 = (- b + sqrt(b^2 - 4*a*c))/(2*a)
x1 =
-2
>> x2 = (- b - sqrt(b^2 - 4*a*c))/(2*a)
x2 =
-3
In this case, the term inside the square root is
positive and the two roots are real numbers.
x1 = 2 + 3 1;
x2 = 2 3 1;
>> a = 1; b = 4; c = 13;
>> x1 = (- b + sqrt(b^2 - 4*a*c))/(2*a)
x1 =
-2.0000 + 3.0000i
>> x2 = (- b - sqrt(b^2 - 4*a*c))/(2*a)
x2 =
-2.0000 - 3.0000i
A complex number is written as a + bi in
which a is the real part, b is the imaginary
part, and i = -1.
>> imag(a)
ans =
-2
>> b_angle = angle(a)
b_angle =
-1.1071
>> b_deg = b_angle*180/pi
b_deg =
-63.4349
- ex
log (x)
- ln x
log 10(x)
- log10 x
cos (x)
- cosine x
sin (x)
- sine x
tan (x)
- tangent x
acos (x)
- arc-cosine x
asin (x)
- arc-sine x
atan (x)
- arc-tangent x
Summary
Matlab has many mathematical functions.
Complex numbers require no special treatment in
Matlab.
The default value of i (and j) is -1. Appending i
(or j) to the end of a number tells Matlab to make
the number the imaginary part of a complex
number
>> a = 2i
a=
0 + 2.0000i
Array Operations
Array Addressing
Array Construction
>> a = [1 2 3 4 5 6];
>> b = [5 6 7 8];
>> c = [a b]
c=
1 2 3 4 5 6 7 8
>> d = [a(1:3) 9 10]
ans =
1 2 3 9 10
More on Array
More on Array(cont.)
linespace command can also be used to create a
linearly spaced array
>> a=linspace(2,4,5)
a=
2 2.5 3 3.5 4
which is equivalent to
>> a=[2:0.5:4]
ans =
2 2.5 3 3.5 4
Polynomial Roots
or
b)
answer :
a) 21, 1.4000
b) -12, -42i
File Manipulation
Variables generated within Matlab command window
can be saved in a *.mat file.
>> save filename.mat or >> save filename