Вы находитесь на странице: 1из 29

Variable, expression and two data 

types: arithmetic and logical
Week 3

1
Using MATLAB
Menu and toolbar

Workspace

History Command
Start working with Matlab
• Starting Matlab
– double‐click the MATLAB icon

• Exit Matlab
– Select Exit MATLAB from the desktop File menu.
– Enter quit or exit at the Command Window prompt.

3
Doing computing inside matlab
• Inside the commend window, you should see
>>
– The Prompt
– Matlab is ready to response to inputed commend 
• First arithmetic routine
>> 1+2 <ENTER>
ans =
3

>> 

4
Inline help inside matlab
• General resources for getting  help and introduction
>> help
>> helpwin
>> helpdesk
>> demo
• Search for some specific instruction
>> lookfor KEYWORD
example: 
>> lookfor solve

5
Do computing in more than one lines
• Program in script
– A collection of commend/statements to solve some 
problems
• Example: find the value of  11940+45940 and  
1927*4449
– Input two lines
– Input a program
• script file with example.m
>> example 

6
Variable and assignment
• Variables are fundamental to programming. In a sense, 
the art of programming is
getting the right values in the right variables at the  right 
time.
• Rule for name of variable
– consist only of the letters a–z, the digits 0–9 and the 
underscore _
– Starting with a letter
– As long as your want, <31 (recommended by matlab)
– Reserved word can not be used as the variable name
– The name had better make sense

7
(cont’)
– Examples of valid variable names: 
• r2d2, pay_day
– Examples of invalid variable names:
• pay‐day, 2a, name$ _2a, _abc
• for, end, if
• Matlab is case sensitive
• Matlab distinguishes between upper‐ and lower‐case 
letters.
• e.g. 
Name ≠ name

8
Variable assignment
• Format of variable assignment
– variablename = expression
– Expression is executed and store the result in 
variablename
– “=“ does not mean equal, it means “store”
• Example
>> mynum=6+9
mynum =
15
>>

9
(cont’)
• A semicolon in the end of expression will suppress 
the output
• Example
>> x=5; 
>> y=6;
>> z=x+y
z =
11
>>

10
More commands related to variable
• Show names of all variables in the workspace
– who
• Show details of all variables in the workspace
– whos
• Remove all variables in the workspace
– clear
• Remove only variable variablename
– clear variablename

11
Expression
• Allowable items in expression
– Values
– Variables (already defined)
– Operators
– Pre‐defined functions
• Matlab
• User
– Parenthesis
• Example:  
– (x+2)*3+sin(x)

12
Calculus operators
• Two types of operators
– Operand
– Binary
• Common operators
+  addition
‐ subtraction
*  multiplication
^  exponentiation
/   division (divided by, e.g. 5/2=2.5)
\ division (divided into, e.g. 2/4 =2)

13
Precedency rule of operators
>> 4+5*3

ans =

19

>> (4+5)*3

ans =

27

14
(cont’)
• Within the same precedency level
– Expression is evaluated from right to left
– Question: what is the value of a*b/c?
• Nested parenthesis
– Inner parenthesis is evaluated first
• Precedency of operators (from the first to the last)
1. ()
2. ^
3. ‐ (Negation)
4. *,\,/
5. +,‐

15
More mathematical function for calculation
• Trigonometric function
– sin, cos, tan ctan, asin, acos, atan
• More commonly used mathematical functions and constant
– round(x): Rounds a number to the nearest integer
– ceil(x): Rounds a number up to the nearest integer
– floor(x): Rounds a number down to the nearest integer
– fix(x): Rounds a number to the nearest integer towards zero
– abs(x): The absolute value of x
– sign(x): The sign of x
– exp(x):   Exponential of x, 
– log(x):  nature logarithm of x
– pi:    3.1415926 … . .
– i:  Imaginary symbol
– sqrt(x):  square root of x
– and more

16
Format of output
• The Way in which numbers appear
• Three types
>> format short 
5 digits
>> format long
15 digits
>> format rat
show approximate rational number

17
Example
• Run the following code for s = ½, 1/3, sqrt(2), pi
format short; s
format long; s
format rat; s
format ; s
• Be careful with format rat
– It only shows approximate value
– Could be mis‐leading

18
Arithmetic  data types
• Integer
– int8, int16, int32, int64: integers in 8,16,32,64 bits)
– uint8, uint16, uint32,uint64: unsigned integers
• Non‐integer
– single: single‐precision numbers
– Double: double‐precision numbers

Why need so many data types?

19
How computer store number
• It is easy with computers to use binary system, 
– 1 can be represented as "on" 
– 0 can be represented as "off". 
– That is, "has electricity" vs "has no electricity". 
• How do you know integer 2347

• How binary system representing integer 229
– 1 * 27 + 1 * 26 + 1 * 25 + 0 * 24 +0 * 23 +1 * 22 + 0* 21
+1 * 20 = 229
– binary form: 11100101

20
(cont’)
• Bit
– One unit of {0,1}
• Byte
– 8 bits
– Max : 11111111  (255)
– Min: 00000000 (0)
• uint8
– Store number in 1 byte without sign

21
(cont’)
• What if we need store negative integer in one byte
– Starting with 1, negative, 1xxxxxxx
– Starting with 0, positive, 0xxxxxxx
• Int8
– Signed integer in one byte
– Max: 127
– Min: ‐128
• Similar for uint16, int16,…
– e.g., int16 store integer in 2 bytes.

22
How computer store non‐integer numbers
• How we see numbers
– significant digits * baseexponent
– . . 1.2 ∗ 10
• How computer store numbers

• Single‐precision
– p=24
– occupying 4 bytes
• Double precision
– P=53
– Occupying 8 bites

23
How and when to use different arithmetic data 
types
• Default data type
– Double‐precision
• Defined data type
>>x= int8(32.1)
ans =
32
>> 
• Balancing between the precision and storage 
efficiency

24
Logical expression
• Evaluate the correctness of the statement
– TRUE
– FALSE
• Six relationship operator
1. ==     equal
2. ~=     not equal 
3. >        greater than
4. >=      greater than or equal to
5. <       less than
6. <=     less than or equal to

25
(cont’)
• Examples
– bˆ2 < 4*a*c (b2 <4ac)
– x >= 0 (x ≥0)
– a ˜= 0 (a =0)
– bˆ2 == 4*a*c (b2 =4ac)
• Warning
– Be careful when using relationship operator on floating‐
point number 
• Logical value
– 1 (true)
– 0 (true)

26
Logical operator
• logical operators
~:  not
&: and
|:  or
• Example
>> 1& 0
ans =
0
>>

27
Operator precedence
• Precedence (from the first to the last)
– Parenthesis ()
– Arithmetic operators
– Relationship operators
• ==,~=,>,>=,<,<=
– & (and)
– | (or)
– ~ (not)

28
(con’t)
• Example
>> 2 > 1 & 0
ans =
0
• Danger
1<r<2
– What you are expecting?
• Play it safe
– It’s never be wrong to use parenthesis

29

Вам также может понравиться