0% found this document useful (0 votes)
138 views21 pages

Lecture NO.02 PDF

This document discusses using MATLAB for mathematics and provides an example. It covers basic arithmetic commands like addition, subtraction, division and multiplication in MATLAB. It shows the MATLAB desktop after performing some sample calculations and how the answers are displayed. It also discusses using the 'ans' variable to store and refer to previous answers without retyping them, allowing further calculations to be performed.

Uploaded by

Muhammad Nawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views21 pages

Lecture NO.02 PDF

This document discusses using MATLAB for mathematics and provides an example. It covers basic arithmetic commands like addition, subtraction, division and multiplication in MATLAB. It shows the MATLAB desktop after performing some sample calculations and how the answers are displayed. It also discusses using the 'ans' variable to store and refer to previous answers without retyping them, allowing further calculations to be performed.

Uploaded by

Muhammad Nawaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Doing Mathematics with Matlab (I)

Exercise

Doing Mathematics with Matlab (I)

Department of Mathematics and Statistics


Virtual University of Pakistan, Lahore, Pakistan.

[email protected]

Imran Talib VU Lecture NO.02 1


Doing Mathematics with Matlab (I)
Exercise

Outline

1 Doing Mathematics with Matlab (I)


Arithmetic
Symbolic Computation
Substitution in Symbolic Expressions
Errors in input

2 Exercise

Imran Talib VU Lecture NO.02 2


Doing Mathematics with Matlab (I)
Exercise

Outline

1 Doing Mathematics with Matlab (I)


Arithmetic
Symbolic Computation
Substitution in Symbolic Expressions
Errors in input

2 Exercise

Imran Talib VU Lecture NO.02 2


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

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.

Imran Talib VU Lecture NO.02 3


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

f\
File

'0
MATLAB 7.10.0 (R2010a)

@
Edit

I .1'
Debug

"' ~
Parallel

., ("
D•sktop -
Window

I ~ r:iJ ![)
H• lp
--~-r==================
I ~--

I ~ I Curr• nt Folder: \\lro-homes-01\ usersS\ im rant alib\ My Documents\ MATLAB


--
~ - - - - - - - - -- --
X

Shortcuts !tl How t o Add !tJ What's Now


IG Workspace ... , C] p X

'@ • ~ Ill .. IStack: ~ ]I I$> S•loct dato to p... •

an.s =
Name • Value Min Max
EEl ans 104.1000 104.10... 104.10...
10 ~ . 1000

Command History .,.1 Ci f'X


~ ·· c l c

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 - - \

~..·%- - 3/8/16 11:17 Ali - - \


~···· clc
L s A3 + ( 5-~ l /10- 7*3 =
~--~~·-'-~~~. .. .~. .~. .r.J--Mi-~--~~~~~. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . .~~-~O.VR.

Figure: (1) The Matlab Desktop display after performing some basic arithmetic
commands

Imran Talib VU Lecture NO.02 4


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

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

Imran Talib VU Lecture NO.02 5


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

(l. MATLAB 7.10.0 (R2010a)


File Edit Debug Parallel Desktop Window Help

'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

>> 5 A3+ (5 - 4)/10- 7•3 ~


-~ :.1 IStack: IBa... ·Ill~ Select data to p ... ·I
Name • Value M in Max
an s =
EB ans 1.2225e•10 1.2225... 1.2225...
104 . 1000

>> sqrt (a n s )

an s =I
10 .20 2 9

>> an s "S

an s =

1.105 7 e +005 Command History ... [j ~ X


i ·· doc •
>> an s " 2 - sqrt (an s ) ! ·· c l c
I ·· he l pwin
an s = !·· c l c
I ·h e l pdesk
1. 2225e+010 1 "Cl c
$ %-- 8/4/1 6 3:08 Pl1 --%
fx >> I L. c l c
B %-- 8/8/1 6 11:17 AH --%
' "Cl c
I -5 A3+ (5 - 4)/10- 7*3
!"Sqrt (a n s )
I ·· an s "S
1·· an s " 2 - sqrt ( an s ) =~

Figure: (2) The Matlab Desktop display after performing some basic arithmetic
commands

Imran Talib VU Lecture NO.02 6


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

Arithmetic

On the other hand, sometimes while doing complex calculations, we prefer


to assign the variables of our own choice to computed values.
So, MATLAB gives us independence to assign variables of our own choice.
For example, sin2 (1) + cos 2 (1) can be computed by assigning a variable to
sin(1), and b variable to cos(1). The results are displayed in Figure 3.

Imran Talib VU Lecture NO.02 7


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

tl MA I LA~ / .lU.U l KLUlUa)

File Edit Debug Parallel Desktop Window Help

[) ~ 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

>> a=cos (1) @· ~ fii IStack: I Ba... yI l l~ Select data top... yl


Name • Value Min Max
a =
a 0.5403 0.5403 0.5403
ans 1 1 1
0 .5 403
b 0.8415 0.8415 0.8415

>> b =sin(1)

b =

0 . 8415

>> a " 2 +b" 2

an s =

1 Command History ... [j ~ X


: ..·h e l pdesk A

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

Imran Talib VU Lecture NO.02 8


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

Symbolic Computation

To perform symbolic calculations, make sure that Symbolic Toolbox is


installed on your system.
For this type help symbolic in the Command Window and run this
command by pressing enter key.
If it is installed then the MATLAB desktop looks like the Figure 4,
otherwise you have to install it like the installation of the other packages.

Imran Talib VU Lecture NO.02 9


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

/ 1 MATLAB 7.10.0 (R2010a)


File Edit Debug Parallel Desktop Window Help

'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 --%

mldivide - \ matri x l eft d ivis i o n . l··· c l c


ropo'N er - A
matri x p ower. ' ·h e l p sytr.bo l ic
mrdivide - I matri x r ight d ivis i o n . B ·%-- 8/9/1 6 1o:o5 AJ1 --%
mtimes - k matri x mult i p l icati o n . ·c l c
transpose - • matri x tran spose. L h e l p symbol ic
=
4\ Start I

Figure: (4) The Matlab Desktop display with help symbolic command

Imran Talib VU Lecture NO.02 10


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

Symbolic Computation

To do symbolic computations, you must use syms to declare the variables


you plan to use to be symbolic variables.
Usually this command is used to solve algebraic expressions. For example
To compute (x − y ) ∗ (x − y )2 , declare x and y to be symbolic.
The MATLAB Desktop looks like Figure 5.

Imran Talib VU Lecture NO.02 11


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

4\. MATLAB 7.10.0 (R2010a)


File Edit Debug Parallel Desktop Window Help

; '[) ~ 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

an s = ~ ans <1xl sym>


~X <1xl sym>
~y <1xl sym >

>> exp a n d (a n s )

an s =

>> fac t or (an s )

an s =
Command History ~•Cll'X

'···· a " 2+b" 2


$··%-- 8/9/16 9:57 AH --%

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

Imran Talib VU Lecture NO.02 12


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

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

Imran Talib VU Lecture NO.02 13


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

1(\. MATLAB 7.10.0 (R2010a)


File Edit Debug Parallel Desktop Window Help

; '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 =

Command History ~•Cll'X


2 9 5 4312 70 6550833 6 1 0752
··· s impl ify ( (x A2 - yA2 ) / x +y)
·· s impli f y ( (x A2 - yA2 ) I (x +y))
.... c l c
an s = ·· cos (s ym( 'pi/2) ' )
.... c l c
2 9 5 4312 70 6550833 69 8 6 43 ·· cos (s ym( ' pi/2 · ))
.... c l c
A» ·· cos (s ym ( 'pi/2' ))
··· s y s m ( '1/2' ) + s y s m ( '1/3' )
···· c l c
·· cos (s ym ( 'pi/2' ))
·· s ym( '1/2' ) +s ym( '1/3' )
·· s ym(3 A45 )
·· s ym( '3A45' )
=
4\ Start I

Figure: (6) The Matlab Desktop display with sym command

Imran Talib VU Lecture NO.02 14


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

Substitution in Symbolic Expressions

While working with symbolic expressions, some times it is necessary to


substitute a numerical value, or even another symbolic expression, for one
or more of the original variables in the expression.
subs command is used for this purpose.
For example, we want to substitute the numerical value 2 for the symbolic
variable x in the symbolic expression u = x + y 2 .
For this we use the command subs(u,x,2).
Similarly, we can substitute the symbolic expression y + x for the symbolic
variable y in the symbolic expression u = x + y 2 as, subs(u,y,y+x).
The MATLAB Desktop looks like the Figure 7.

Imran Talib VU Lecture NO.02 15


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

' ' MA l iA ~ / ,lU.U (KLUl ua)


File Edit Debug Parallel Desktop Window Help

: '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

>> s ubs ( u , y, y+x )

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

Figure: (7) The Matlab Desktop display with substitution command

Imran Talib VU Lecture NO.02 16


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

Errors in input

If you make an error while entering input in a Command Window,


MATLAB will beep and print an error message.
For example, note that what happens when you try to evaluate sin10:
» sin10 ??? Undefined function or variable ’sin10’.
Note that there you did not write the correct command. The correct
command is sin(10). Then Matlab gives you the correct output. See Figure
8.

Imran Talib VU Lecture NO.02 17


Arithmetic
Doing Mathematics with Matlab (I) Symbolic Computation
Exercise Substitution in Symbolic Expressions
Errors in input

I' MA I LA~ / .l U.U [ KLUlUa) liiCJ' I~ !"C._I

File Edit Debug Parallel Desktop Window Help

'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

>> s in (10 ) ~ ans -0.5440 -0.5440 -0.5440


€J u <lxl sym>
€J x <lxl sym>
an s =
€Jy <lxl sym>
- 0 .5 440

fx. » I

Command History ... [j ~ X


.... c l c A

.... 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

Figure: (8) The Matlab Desktop display while removing error

Imran Talib VU Lecture NO.02 18


Doing Mathematics with Matlab (I)
Exercise

1 Find the value of the following polynomials at x = 2 using subs command:


p(x, y ) = x 2 + 3y 2 x + 10
u(x) = x 3 + 4x + 10
π
2 Find the value of the following trigonometric functions at x = 2 using sym
command:
cos(x), sin(x), tan(x).

Imran Talib VU Lecture NO.02 19


Doing Mathematics with Matlab (I)
Exercise

1 Expand the following algebraic expressions using expand command:


(x + y )10
(x + y )3 + (x + y )2
2 Simplify the following quantities:
2 + 5 − 4 + 1010 + 72 .
2/5 + 4/10 − 1002

Imran Talib VU Lecture NO.02 20

You might also like