Matlab For Beginners (Chap 1)
Matlab For Beginners (Chap 1)
Tan Do
Vietnamese-German University
Lecture 1
Lecturer
Do Duc Tan
Email [email protected]
Oce A.105
Exam: To be announced.
References
Roy H. Kwon.
In this lecture
variables.
MATLAB overview
(b) MATLAB
MATLAB desktop
Current folder window shows all MATLAB les in the current folder.
Examples:
» 1 + 1
ans =
2
» 3*ans
ans =
6
» a = 8
a =
8
Tan Do (VGU) Introduction to MATLAB Lecture 1 9 / 34
MATLAB session
Putting semicolon ; at the end of the command will suppress the display.
» a = 8;
»
» a=8
a =
8
» a = 8
a =
8
Tan Do (VGU) Introduction to MATLAB Lecture 1 10 / 34
MATLAB session
Comma
» 1 + 1, a = 8; b = 5, a + b
ans =
2
b =
5
ans =
13
Strings
» name = ’Tan’
name =
Tan
You can also concatenate strings together.
» name(1)
ans =
T
Tan Do (VGU) Introduction to MATLAB Lecture 1 12 / 34
MATLAB session
» name(5)
ans =
D
» name(2:5)
ans =
an D
for short.
Arithmetic operators
» s = 30*r
s =
9
» s = 30r
s = 30r
|
» 15/3
ans =
5
» 15\3
ans =
0.2000
Note that the slash slants toward the denominator, i.e., 15\3 = 3/15 = 0.2.
Order of precedence
4b2 − 12 − 8/4 ∗ 2?
The arithmetic operators +, -, *, /, \ and b follow a set of rules called
precedence.
Precedence Operation
First Parentheses, evaluated from the innermost pair
» 8 + 3*5
ans =
23
» (8+3)*5
ans =
55
» 4b2 - 12 - 8/4*2
ans =
0
» 4b2 - 12 - 8/(4*2)
ans =
3
» 3*4b2 + 5
ans =
53
» (3*4)b2 + 5
ans =
149
» 27b(1/3) + 32b(0.2)
ans =
5
» 27b(1/3) + 32b0.2
ans =
5
» 27b1/3 + 32b0.2
ans =
11
Assignment operator
» x = 6
x =
6
We can also type
» x = x + 2
x =
8
The above command tells MATLAB to add 2 to the current value of x
(which is 6), and then to replace the current value of x with this new value
(which is 8).
Tan Do (VGU) Introduction to MATLAB Lecture 1 21 / 34
MATLAB session
» x = 6
is important.
» 6 = x
6 = x
|
» x + 2 = 10
x + 2 = 10
|
» x = 6 + y
Undefined function or variable y.
Variable names
1 Beginning with a letter, the rest can contain letters, digits and
underscore characters.
Note that these rules are also applied to naming MATLAB les.
Name Description
ans Temporary variable containing the most recent answer
√
i, j Imaginary unit −1
Inf Innity
pi Number π
gures.
» x = pi
x =
3.1416
» format short
» x
x =
3.1416
» format long
» x
x =
3.141592653589793
Other format commands include
» format bank
» x
x =
3.14
» format rat
» x
x =
355/113
Tan Do (VGU) Introduction to MATLAB Lecture 1 27 / 34
MATLAB session
be in scientic notation.
» y = 32/2017
y =
0.0159
» format short e
» y
y =
1.5865e-02
» format long e
» y
y =
1.586514625681705e-02
Here e stands for 10 to the power of . For example, e-02 equals 10−2 .
Tan Do (VGU) Introduction to MATLAB Lecture 1 28 / 34
MATLAB session
The following are some useful commands for managing a work session.
Command Description
clc clears the Command window, variables retain values
clear removes all variables from memory
clear var1 var2 removes the variables var1 and var2 from memory
exist(‘name’) determines if a le or variable exists having the name `name'
quit quit MATLAB
who lists all variables currently in memory
whos lists current variables and sizes
... continues a line (called ellipsis)
help function
The help function is the most basic way to determine the syntax and
» help sin
in MATLAB Command window
help function
Overloaded methods:
codistributed/sin
gpuArray/sin
sym/sin
lookfor function
The help command is useful only when you know the exact MATLAB
» help sine
MATLAB returns
» lookfor sine
xfourier - Square Wave from Sine Waves
acos - Inverse cosine, result in radians.
acosd - Inverse cosine, result in degrees.
acosh - Inverse hyperbolic cosine.
asin - Inverse sine, result in radians.
asind - Inverse sine, result in degrees.
asinh - Inverse hyperbolic sine.
cos - Cosine of argument in radians.
cosd - Cosine of argument in degrees.
cosh - Hyperbolic cosine.
sin - Sine of argument in radians.
sind - Sine of argument in degrees.
sinh - Hyperbolic sine.
...
called matlab.mat.
You can also specify the le name for the session that you want to
Typing load only will cause MATLAB to load the default le
matlab.mat.