matlab system environmentnumerical systems in matlab 1. dec2bin: convert the decimal number d to...
TRANSCRIPT
-
Dr. Tefool Hussein Computer Lecture 1- First Stage
1
MATLAB System Environment
Arithmetic Operations in MATLAB
1. + : is used for addition of two numbers or two matrices.
2. - : is used for subtraction of two numbers or two matrices.
3. * : is used for multiplication of two numbers or two matrices.
4. / or \ : is used for division of two numbers or two matrices. Ex. 7/70 =7,
7\70 =0.1. (why?)
5. ^ : is used to raise a value to a certain exponent. Ex.: 2^10 =100.
6. sqrt : is used for finding the square root for any number. EX.: sqrt(100)=10.
7. % : for writing comments on command window or in m-file.
8. .* : for element by element matrix multiplication.
9. ./ or .\ : for element by element matrix division.
10. .^ : for raising the elements of a matrix to a certain exponent.
Command Window
Command History
Work Space
-
Dr. Tefool Hussein Computer Lecture 1- First Stage
2
11. โฆ : used for continuing writing the expression located in the current line in
the next line.
Examples:
>> pi
ans =
3.1416
>> k=pi
k =
3.1416
>> d=i
d =
0 + 1.0000i
>> a=j
a =
0 + 1.0000i
>> s=0/0
s =
NaN
>> b=90/0
b =
Inf
Some predefined variables in MATLAB
-
Dr. Tefool Hussein Computer Lecture 1- First Stage
3
We noticed that all the names of the variables exist in the work space window and all
the commands previously implemented are found in the command history window.
The command who gives the names of variables and the command whos gives the
names of variables, their types and their storage space
>> who
Your variables are:
a ans b c d k s
>> whos
Name Size Bytes Class Attributes
a 1x1 16 double complex
ans 1x1 8 double
b 1x1 8 double
c 1x1 8 double
d 1x1 16 double complex
k 1x1 8 double
s 1x1 8 double
The command clear is used to delete variables from the computer storage and from the work space.
>> clear b c
>> clear
>> who
>>
Notice that there are no results when who command is used after clear command.
Arithmetic Expression
The arithmetic expression consists of set of constants and variables related by arithmetic operations. The arithmetic symbols like +, -, /, *, ^ etc. Some examples of arithmetic expressions are indicated below:
-
Dr. Tefool Hussein Computer Lecture 1- First Stage
4
Arithmetic Expression Expression in MATLAB
a-3b a-3*b
๐2 โ 10 c^2-10
๐2 + ๐2
12 (a^2+b^2)/12
m(7d-3t) m*(7*d-3*t)
1
2 + 32+4
5ร6
7 1/(2+3^2)+4/5*6/7
Rules of Precedence
The following table summarizes the precedence rules for implementing arithmetic expressions
Precedence Arithmetic Operation
1. Brackets The expression between the brackets has the higher precedence than other expressions where all brackets are implemented firstly from the inner to the outer brackets.
2. Power After brackets all exponents are implemented.
3. Multiplication and division
All multiplications and divisions are implemented from left to right.
4. Addition and subtraction
All additions and subtractions are implemented from left to right.
5. Assignment operation
The last step.
Example: if we have the expression ๐ = ๐จ โ๐ฉ
๐ช the sequence of operations will be
as follows:
-
Dr. Tefool Hussein Computer Lecture 1- First Stage
5
Example: while the sequence of operations for the expression ๐ด
๐ต+๐ถ would be as
follows
Example:
H.W.: for each of the following, convert the algebraic expressions to the MATLAB expressions formats and determine the precedence map:
1. ๐ = ๐๐ฟ +๐๐ฟ๐
๐๐โ ๐๐ฟ๐ 2. ๐ญ =
โ๐+โ๐๐โ๐๐๐
๐๐
2. ๐ป =๐
๐[๐๐ญ๐ โ ๐๐ญ๐ +
๐๐ญ๐๐ญ๐
๐ญ๐โ๐ญ๐] 4. ๐ฟ =
๐ฟ๐๐๐โ๐ฟ๐๐๐
๐๐โ๐๐
Numerical Systems
If x is a real positive number where:
๐ฅ = (๐๐๐๐โ1โฆ๐0. ๐1๐2โฆ)10
Then the value of x is the summation of the two sequences:
-
Dr. Tefool Hussein Computer Lecture 1- First Stage
6
โ๐๐ ร 10๐ +โ๐๐10
โ๐
โ
๐=1
๐
๐=0
If is a real positive binary number:
๐ฅ = (๐๐๐๐โ1โฆ๐0. ๐1๐2โฆ)2
Then the value of x is the summation of the two sequences:
โ๐๐ ร 2๐ +โ๐๐2
โ๐
โ
๐=1
๐
๐=0
If is a real positive binary number:
๐ฅ = (๐๐๐๐โ1โฆ๐0. ๐1๐2โฆ)16
Then the value of x is the summation of the two sequences:
โ๐๐ ร 16๐ +โ๐๐16
โ๐
โ
๐=1
๐
๐=0
Example: convert the decimal number (39.125) to binary hexadecimal and octal.
Solution:
We firstly convert the integer part of the real number as follows:
Residual Base number
1
1
1
0
0
1
2
2
2
2
2
2
39
19
9
4
2
1
0
0.125 x 2 = 0.250 0
0.25 x 2 = 0.5 0
0.5 x 2 = 1.0 1
First place in the
number
-
Dr. Tefool Hussein Computer Lecture 1- First Stage
7
2(111001.001=)10(39.125)
ุงูุนุฏุฏ ุงุฃูุณุงุณ
7
4
8
8
39
4
0
0.125 x 8 = 1.0 1
8(47.1=)10(39.125)
Example: convert the binary number 2(111.001) to decimal.
0 1 2 1 2 3
2(111.001) 1 2 1 2 1 2 0 2 0 2 1 2
= 1 + 2 + 4 + 0 + 0 + 0.125
= (7.125)10
Example: convert the octal number 8(212.726) to decimal.
(212.726)8 = 2 ร 80 + 1 ร 81 + 2 ร 82 + 7 ร 8โ1 + 2 ร 8โ2 + 6 ร 8โ3
= 2 + 1 + 128 + 0.875 + 0.03125 + 0.01171875 = (131.91795)10
Numerical Systems in MATLAB
1. dec2bin: convert the decimal number d to binary. Either of the following formats can be used: str = dec2bin(d)
str = dec2bin(d,n)
where:
str: contains the result of conversion, d: is the number to be converted and n: is the number of binary places on the resulted number.
First place in
the number
-
Dr. Tefool Hussein Computer Lecture 1- First Stage
8
>> dec2bin(100)
ans =
1100100
>> dec2bin(100,4)
ans =
1100100
>> dec2bin(100,8)
ans =
01100100
>> dec2bin(100,10)
ans =
0001100100
2. dec2hex: converts the decimal number to hexadecimal number. One of the following formats can be used: str=dec2hex(d)
str=dec2hex(d, n)
>> dec2hex(1023)
ans =
3FF
>> dec2hex(1023,6)
ans =
0003FF
3. dec2base: converts the decimal number d to a number to the base 'base'. str = dec2base(d, base) str = dec2base(d, base, n)
>> dec2base(100,2) ans = 1100100
-
Dr. Tefool Hussein Computer Lecture 1- First Stage
9
>> dec2base(1023,16) ans = 3FF >> dec2base(80,8) ans = 120 >> dec2base(120,4) ans = 1320 >> dec2base(120,4,7) ans = 0001320
4. bin2dec: converts the binary number to decimal.
bin2dec(binarystr)
5. hex2dec: converts the hexadecimal number to decimal.
d = hex2dec('hex_value')
6. base2dec: converts the number to the base 'base' to decimal.
d = base2dec('strn', base)
H.W.: convert each of the following decimal numbers to binary, octal and
hexadecimal
1( .10.675 ) 2( .22.892 ) 3( .101.776 ) 4( .100 ) 5( .500 )
6. (750 )7( .1000)
H.W.: convert each of the following numbers to decimal:
1 .2(111101.011 ) 2 .16(6E.FA ) 3 .8(765.67 )
4 .2(111000001 ) 5 .16(B12 ) 6 .8(4571)