Lecture NO.02 PDF
Lecture NO.02 PDF
Exercise
Outline
2 Exercise
Outline
2 Exercise
Arithmetic
This lecture describes some of the basic MATLAB commands that will be used
in this course. For example
+ command is used for addition.
- command is used for subtraction.
For division, we use /.
* command is used for multiplication.
The MATLAB Desktop looks like the Figure 1 after performing these
commands.
f\
File
'0
MATLAB 7.10.0 (R2010a)
@
Edit
I .1'
Debug
"' ~
Parallel
., ("
D•sktop -
Window
I ~ r:iJ ![)
H• lp
--~-r==================
I ~--
an.s =
Name • Value Min Max
EEl ans 104.1000 104.10... 104.10...
10 ~ . 1000
A»
l "h e l pdesk
; ·· c l c
i ·· doc
; ·· c l c
; ·· he l pwin
·· c l c
"h e l pdesk
·· c l c
~..·%- - 8/4/16 3:08 PH - - \
Figure: (1) The Matlab Desktop display after performing some basic arithmetic
commands
Arithmetic
Note that in the Figure 1, MATLAB prints the answer and assigns the
value to a variable called ans.
If the further calculation is needed then simply use the variable ans, rather
than retype the answer.
It is to be noted that MATLAB assigns a new value to ans with each
calculation.
For example the square root of the previous answer can be taken using the
command sqrt(ans). The output is displayed in the Figure 2
'0 @ I ,l' ... ~ II) ~ I ~ tin i§!l I ~ I Current Folder: I\\lro-homes-01\ usersS\imrantalib\ My Documents\ MATLAB
!!I How to Add I!!I What's New I
..
Shortcuts
Command Window IC:I~ Workspace ... [j ~ X
>> sqrt (a n s )
an s =I
10 .20 2 9
>> an s "S
an s =
Figure: (2) The Matlab Desktop display after performing some basic arithmetic
commands
Arithmetic
[) ~ I d' ... ~ ") (!' I ~ l!'iU i§'Q I ~ I Current Folder: I\\lro-homes-01\ usersS\ im rantalib\ My Docum ents\ MATLAB
!!I !!I
..
Shortcuts How to Add What's New
Command Window 1%::1~ Workspace ... [j ~ X
>> b =sin(1)
b =
0 . 8415
an s =
fx >> L.. c l c
$ %-- 8/4/1 6 3 :08 Pl1 --%
'···· c l c
~ %-- 8/8/1 6 11 :17 AH --%
; ·· c l c
I.. 5 -3+( 5 - 4) / 10- 7°3
I "Sqrt (a n s )
i ·· an s "S
! .. an s "2 - sqrt (an s )
! ·· c l c
'
; .. a=cos (1)
I.. b =sin (1) =
! .. a "2 +b "2 ~
~ Start I
Figure: (3) The Matlab Desktop display after performing some basic arithmetic
commands
Symbolic Computation
'0 E3i I .1' IIIII ~ "') (II I ~ ~ 1§1 I ~ I Current Folder: I\\lro-homes-01\ usersS\ imrantalib\ My Documents\ MATLAB
Shortcuts !!I How to Add !!I What's New
.,;C~:.;ma
om ::;:on:=,W
d ~indow
>> h e l p symbol ic
-.1.11!•.,~
:=;;:..--------------------------------------------------ICI Workspace
~ ~ • ~ Ill .. IStack:! Ba... y
.., Cl ~
Ill~ Select data to p... y
X
I
Symbol ic Hath Tool box
Name • Value Min Max
Versi o n 5. 4 (R2 010a ) 25- J a n - 2 010
Cal c ulus.
diff - Di ffere n t i ate.
int - Integ rate.
limit - Limit .
taylor - Ta ylor seri es.
iacobian - J acobi a n matri x.
svmsum - S ummati o n of seri es.
Linear Algebra.
diag - Create or extract d i a g o n a l s.
t r iu - Upper t ri a ngle.
tril - Lo wer t ri a ngle.
Command History ~•Cll'X
inv - Hatri x inverse.
det - Determina n t. - 5 A3+ (5 - 4)/10- 7°3
rank - Ra n k. '- sqrt (a n s )
rref - Redu ced r ow ech e l o n form. .. a n s " S
n u ll - Basi s for null space. i· .. a n s " 2 - sqrt (a n s )
col space - Basi s for column space. !. ·c l c
eig - Eige nva lues a n d e ige nvectors. , .. a =cos (1)
svd - S ingular v a lues a n d s ingular v ectors.
' ·b=sin (1)
iordan - J ordan can o nical (norma l) form.
'· .. a " 2 +b " 2
EQ!y - Ch aracteri sti c polynomia l .
expm - Hatri x expon e n t i a l .
~··%-- 8/9/1 6 9:57 Al1 --%
Figure: (4) The Matlab Desktop display with help symbolic command
Symbolic Computation
; '[) ~ I ,!' -.a ~ ") ~ I ~ jjJ ~ I ~ I Current Folder: I\\lro-homes-01\ usersS\ imrantalib\ My Documents\ MATLAB
: Shortcuts !!I How to Add !!I What's New
'r. Workspace ~• Cl ll X
>> s yms x y f!l • ~ rj .. IStack:j Ba... • Il l~ Select data to p... •I
>> (x - y )•( x - y )A2
Name • Value Min Max
>> exp a n d (a n s )
an s =
an s =
Command History ~•Cll'X
I L. h e l p symbol ic
an s = 8 .. %-- 8/9/1 6 1o:o5 AJ1 --%
~ ·· c l c
I ·h e l p symbol ic
l ·· c l c
I ·· s yms x y
an s = i · (x - y ) • (x - y ) A2
I ·exp a n d ( a n s )
X - y l ·· fac t or (an s )
I ·s i mpl ify ( (x A2 - y A2 ) / x +y)
fx >> 1 ·s i mpl i f y ( (x A2 - y A2 ) I (x +y)) =
1. . Start J
Figure: (5) The Matlab Desktop display after declaring x and y to be symbolic
Symbolic Computation
Remark
It is to be noted that MATLAB uses floating point arithmetic for its
calculations. But exact arithmetics can also be done using the Symbolic Math
Toolbox with symbolic expressions.
For example, the floating point format of cos(pi/2) in MATLAB is
6.1232e − 17. However, it is known that cos(pi/2) is really equal to zero.
The inaccuracy is due to the fact that typing pi in MATLAB gives an
approximation to π accurately to about 15 digits. But sym command gives
accurate value of π. Look at the Figure 6
; '0 E3i I .1' IIIII ~ "') (II I ~ ~ 1§1 I ~ I Current Folder: I\\lro-homes-01\ usersS\ imrantalib\ My Documents\ MATLAB
: Shortcuts !!I How to Add !!I What's New
.,;C~:.;ma
om :;,;::n:.;;,W
d ~indow -.1.11!•.,~
=;;;.--------------------------------------------------ICI Workspace .., Cl ~ X
>> cos (s ym( 'pi/2' )) ~ • ~ Ill .. IStack:! Ba... y Il l~ Select data to p... y I
Name • Value Min Max
an s
~ ans <lxl sym>
0
~X <lxl sym>
~y <lxl sym>
>> s ym ( '1/2' ) +s ym ( '1/3' )
an s
5/ 6
an s =
: '0 a I "' ... ll:l!l "' ("' I ~ ~ ~ I ~ I Current Folder: I\\lro-homes-01\ usersS\ im rantalib\ My Documents\ MATLAB
!t1 !t1 What's New
,. .. I
: Shortcuts How to Add
Command Window II)~ Workspace ... Cl ~ X
>> s yms X y ~ • @ I
Stack: Ba ... • Ill~ Select data top... · I
>> u=x+y"2
Name • Value Min Max
u =
~ ans <lxl sym>
~u <lxl sym>
yA2 + X ~X <lxl sym>
~y <lxl sym>
>> s ubs (u , x , 2 ) 1
an s =
yA2 + 2
an s =
Com mand History ... Cl ~ X
>· vpa (pi/2 )
X + (X + y ) A2 A
1 L. vpa ( 'pi/2' )
>> s impl i f y (a n s ) 8 %-- 8/12/1 6 10 : 27 Al1 --%
·· c l c
an s = l ·· s ubs X y
·· u =
X + (X + y ) A2 ; ·· clc
.. s ubs X y
fx.. >> I ; "Clc
.. s yms X y
; ·· u=x+y"2
.. s ubs (u , x , 2 )
l ·· s ubs ( u , y, y+x )
=
' "S impl ify (a n s ) ~
.. Start I
Errors in input
'0 fJ I ~ - ~ "J (!' I ~ rjJ 1§1 I ~ I Current Folder: I\\lro-homes-01\ usersS\ imrantalib\ My Documents\ MATLAB
Shortcuts !!I How to Add !!I What's New
...
Command Window
>> s inlO
??? Undefined funct ion or variable • s inlo• .
ICI ~
@.
Workspace
Name •
~ fil ' I Stack: l Ba...
Value
~Ill~ Select data top...
Min
[j
Max
~ X
~I
fx. » I
.... s ubs X y
"" U =
···· c l c
.... s ubs X y
·· .. c l c
.... s yrns X y
···· u=x+ y"2
.... s ubs (u , x , 2 )
.... s ubs ( u , y , y + x )
·· s imp l ify (a n s )
···· c l c
"" S inlO
·· s in (10 )
=~
A Start I