You are on page 1of 10

MATLAB NOTES

(MA102)

Lia Vas
Content 1. Algebra 1.1 Basic arithmetic 1.2 Solving equations 2. Functions 2.1 Representing functions 2.2 Representing independent variable 2. !raphing functions . "alculus .1 #ifferentiation .2 $ntegration %. &ractice &roblems '. Appendi( '.1 Solving s)stems of equations '.2 !raphing multiple functions on same graph '. Limits

1. ALGEBRA
1.1 Basic Arithmetic *A+LAB can perform basic arithmetic operations. ,ou can use -. /. 0. 1 and 2 to add. subtract. multipl). divide or e(ponentiate. respectivel). For e(ample. if )ou enter3 >> 2^3 - 2*2 *A+LAB gives )ou the ans4er3 ans = $f )ou 4ant to perform further calculations 4ith the ans4er. )ou can t)pe ans rather than ret)ping the 4hole ans4er. For e(ample. >> s!"t(ans) gives )ou ans =

/1/

2 1.2 Solving 5quations For solving equations. )ou can use the command so#$e. +he command solve is al4a)s follo4ed b) parenthesis. After that. )ou t)pe the equation )ou 4ould li6e to solve in single quotes. +hen t)pe coma. and variable for 4hich )ou are solving in 7single8 quotes. +hus. the command solve has the follo4ing form so#$e(%equation%& %variable for which you are solving%) For e(ample. to solve the equation x9/2x/%:;. )ou t)pe3 >> so#$e(%'^3-2*'- =0%& %'%) and get the follo4ing ans4er3 ans = ( 2) ( -1*+) ( -1-+) <ere i stands for the imaginar) number solution. 2.

1 . +his ans4er tells us that there is =ust one real

>ote that 4e entered 2*' to represent 2x in the command above. Ente"+n, * -o" ./#t+0#+1at+on +s a#2a3s ne1essa"3 +n MATLAB. *A+LAB can give )ou both s)mbolic and numerical ans4er. For e(ample. let us solve the equation x?/@x-2:;. >> so#$e(%3*'^2-4*'*2=0%&%'%) ans = ( 53*153*10^(152)) ( 53-153*10^(152)) $f 4e 4ant to get the ans4er in the decimal form 4ith. sa). three significant digits. 4e can use the command $0a. >> $0a(ans& 3) ans = ( 2.34) ( 0.24)

/2/

B) changing in the command $0a(ans& 3) )ou can specif) the number of digits in the ans4er. +he command $0a has the general form $0a(expression you want to approximate& number of significant digits)

2. 67NCT8ONS
2.1 Representing Functions +o represent a function. use the command +n#+ne. Similarl) to solve. this command is follo4ed b) parenthesis and has the follo4ing form3 +n#+ne(%function%& %independent variable of the function%) For e(ample. let us define the function x?- x/2. 5nter >> - = +n#+ne(%'^2*3*'-2%& %'%) *A+LAB output 4ill be -= 8n#+ne -/n1t+on9 -(') = '^2*3*'-2 After defining a function. 4e can evaluate it at a point. For e(ample.
AA f728
-/n1t+on o" s3.:o# e2( ln ( log ( log. base a of ( sin ( cos ( arctan7(8 C "e0"esentat+on +n MATLAB e(p7(8 log7(8 log7(8Blog71;8 log7(8Blog7a8 sin7(8 cos7(8 atan7(8 pi

ans = 4 +he follo4ing table gives an overvie4 of ho4 most commonl) used functions or e(pressions are represented in *A+LAB.

$f a function is short. it might be -aste" to e$a#/ate a -/n1t+on at a 0o+nt s+.0#3 :3 t30+n, t;e $a#/e o- x <+"e1t#3 -o" x. For e(ample. to evaluate sin7x8 at x:2. simpl) t)pe >> s+n(2) and obtain the ans4er ans = .=0=2=> As 4hen using the calculator. one must be careful 4hen representing a function. For e(ample 1 x x+ 6 should be represented as 15('*('*?)) not as 15'*('*?) nor as 15'('*?)&

3 x 5x 6
2

should be represented as 35('^2*@*'*?) not as 35'^2*@*'*?&

e 5x

should be represented as e'0(@*'^2) not as e^(@*'^2)& e'0*(@*'^2)& e'0(@'^2) nor as e'0^(@*'^2).

Ln7(8 should be represented as #o,('). not #n('). log 7(28 should be represented as #o,('^2)5#o,(3) not as #o,(')5#o,(3)*'^2. 2.2 Representing independent variable *ost of the mathematics operations on functions 7e.g. graphing. differentiation. integration8 are performed easier in *A+LAB if )ou declare independent variable for a *A+LAB variable. Dse the command s3.s to declare the variables )ou plan to use. For e(ample. if )ou 4ant to use variable (. simpl) t)pe >> s3.s ' before an) of the commands for graphing. differentiation or integration. 2. !raphing Functions Let us start b) declaring that x is a variable3 >> s3.s ' +he simplest command in *A+LAB for graphing is eA0#ot. +he command has the follo4ing form eA0#ot(function) For e(ample. to graph the function x+x+1. )ou simpl) t)pe >> eA0#ot('^2*'*1)
(22-(-1
'; %' %; '

A ne4 4indo4 4ill open and graph 4ill be displa)ed. +o cop) ; the figure to a te(t file. go to E<+t and choose Co03 6+,/"e. 2' 2; +hen place cursor to the place in the 4ord file 4here )ou 4ant 1' the figure to be pasted and choose E<+t and Baste. 1;
'

Ee can specif) the different scale on x and y a(is. +o do this. the command a'+s is used.
/%/

; /@ /F /% /2 ; 2 % F @

$t has the follo4ing form a'+s((xmin, xmax, ymin, ymax))


(22-(-1

+his command parallels the commands in menu E$>#GE on the +$@ calculators. For e(ample. to see the above graph bet4een (/values /1; and 1; and )/values ; and F;. )ou can enter >> a'+s((-10 10 0 ?0)) +he result 4ill be3 >ote that the domain of function did not change b) command a(is. +o see the graph on the entire domain 7in this case H/1;. 1;I8. add that domain after the F; function in the command eJplot3 '; eA0#ot(function& (xmin, xmax)) $n this case. >> eA0#ot('^2*'*1& (-10& 10)) 4ill give )ou the desired graph.
%; ; 2; 1; ;

F; '; %; ; 2; 1; ; /1; /@ /F /% /2 ; 2 % F @ 1;

(22-(-1

/1'

/1;

/'

'

1;

1'

3. CALC7L7S
.2 #ifferentiation Start b) declaring ( for a variable. +he command for differentiation is <+--. $t has the follo4ing form <+--(function) For e(ample. >> s3.s ' >> <+--('^3-2*'*@) gives us the ans4er ans = 3*'^2-2 +o get n/th derivative use

/'/

<+--(function, n) For e(ample. to get the second derivative of x /2x-'. use3 >> <+--('^3-2*'*@& 2) ans = ?*' Similarl). the 2 rd derivative of sin7(8 is obtained as follo4s. >> <+--(s+n(')& 23) ans =-1os(') +o evaluate derivative at a point. 4e need to represent the derivative as a ne4 function. For e(ample. to find the slope of a tangent line to x?- x/2 at point 2. 4e need to find the derivative and to evaluate it at x:2. >> <+--('^2*3*'-2) 7first 4e find the derivative8 ans = 2*'*3 >> - = +n#+ne(%2*'*3%& %'%) 7then 4e representative the derivative as a function8 -= 8n#+ne -/n1t+on9 -(') = 2*'*3 >> -(2) 7and. finall). 4e evaluate the derivative at 28 ans = >

Recall the steps needed in order to find minimum or ma(imum values of a given function 7using second derivative test8 1. 2. . %. Find first derivative Solve it for Jeros. +he (/values )ou obtain are called critical Find second derivative &lug critical points in second derivative. $f )our ans4er is negative. the function has a ma(imum value at a critical point used. $f )our ans4er is positive. the function has a minimum value at a critical point used. '. &lug critical points in )our function. +he )/values )ou obtain are )our ma(imum or minimum values. $n *A+LAB. start 4ith s)ms (.

/F/

Finding derivative3 <+--(function) Finding critical points3 so#$e(%copy-paste the answer from step 1=0%& %'%) Finding second derivative3 <+--(function, 2) 5valuating second derivative at critical points3 ,=+n#+ne(second derivative& %'%) follo4ed b) ,(critical value) '. 5valuating function at critical points3 -=+n#+ne(function& %'%) follo4ed b) -(critical value)
1. 2. . %.

For e(ample. let us find ma(imum or minimum value of x /2x-'. Start b) finding first derivative3 >> <+--('^3-2*'*@) ans = 3*'^2-2 +hen find critical point7s83 >> so#$e(%3*'^2-2=0%& %'%) ans = (?^(152)53) (-?^(152)53) or. using $0a(ans& 3) (.41?) (-.41?) Find second derivative >> <+--('^3-2*'*@& 2) ans = ?*' 5valuate this at critical points. >> ,=+n#+ne(%?*'%& %'%) ,(')= ?*' >>,(.41?) ans = .4=? &ositive ans4er means that the function has minimum at (:.@1F >> ,(-.41?) ans = - .4=? >egative ans4er means that the function has ma(imum at (:.@1F Finding )/values of ma(imum and minimum3 >> -=+n#+ne(%'^3-2*'*@%& %'%) -(')= '^3-2*'*@ >>-(.41?) ans = 3.=11 +his is the local minimum value. >>-(.41?) ans = ?.044 +his is the local ma(imum value. .2 $ntegration Ee can use *A+LAB for computing both definite and indefinite integrals using the command +nt. For the indefinite integrals. start 4ith s3.s ' follo4ed b) the command

/K/

+nt(function) For e(ample. the command >> +nt('^2) evaluates the integral and gives us the ans4er ans = 153*'^3 For definitive integrals. the command is +nt(function, lower bound, upper bound) For e(ample. >> +nt('^2& 0& 1) evaluates the integral ans = 153 +he ans4er is

. BRACT8CE BROBLEMS
1. 5valuate the follo4ing e(pressions using *A+LAB. a8 sin7BF8 b8 c8 log27'8 2. Solve the follo4ing equations for ( and e(press )our ans4ers as decimal numbers. a8 x /2x-':; b8 log27x2-L8:% x 3 +x+ 1 x . Let f7(8: a8 5valuate f7(8 for (: and (:/2. b8 Find (/values that corresponds to )/value of 2. c8 !raph f7(8 on domain H/% %I. %. Let f7(8: . a8 Find first and second derivative of f7(8. b8 Find the slope of the tangent line to f7(8 at (:1. c8 Find the critical points of f7(8. '. Find the e(treme values of a8 x /%x-@ b8 F. 5valuate the integral K. 5valuate the integral So#/t+ons. . .

/@/

1. a8 sin7piBF8. ans:.' b8 7sqrt7'8- 8B7sqrt7 8/18. ans:K.1'2 c8 log7'8Blog728. ans:2. 21L 2. a8 solve7M(^ /20(-':;M. M(M8. onl) real ans4er is /2.;L. b8 solve7Mlog7(22/L8Blog728:%M.M(M8. ans: '. /'. . a8 f:inline7M7(2 -(-18B(M. M(M8. f7 8:1;. . f7/28:%.' b8 solve7M7(2 -(-18B(:2M.M(M8 (:/1. 2%K is the onl) real solution. c8 eJplot77(2 -(-18B(. H/%.%I8 %. a8 diff7e(p7 0(22-188. ans:F0(0e(p7 0(22-18N diff7e(p7 0(22-18. 28. ans:F071-F0(2280 e(p7 0(22-18. b8 g:inline7MF0(0e(p7 0(22-18M.M(M8 g718:F0e(p7%8: 2K.'L. c8 solve7MF0(0e(p7 0(22-18:;M.M(M8. ans:;. '. a8 ma( 7/1.1'. 11.;KL8. min 71.1'. %.L28. b8 ma( 7. . .122F8. no min. F. int7(0e(p7/ 0(88. ans:/1B 0(0e(p7/ 0(8/1BL0e(p7/ 0(8 K. int7(0e(p7/ 0(8. ;.18. ans:/%BL0e(p7/ 8-1BL:.;@@L@

@. ABBENC8D
'.1 Solving s)stems of equations ,ou can solve more than one equation simultaneousl). For e(ample suppose that 4e need to solve the s)stem x?- x- y? : 2 and 2x-y : 2. Ee use3 >> ('&3) = so#$e( %'^2* '* 3^2 = 2%& %2*'-3 = 2%) And get the solutions '= 1 25@ 3= 0
-?5@

>ote that command so#$e has to be preceded 4ith (list of all variables). ,ou can solve an equation in t4o variables for one of them. For e(ample3 >> so#$e(%3^2-@*'*3-3*?*'^2*'=2%& %3%) ans = 3*'*2 2*'-1 '.2 !raphing multiple functions on the same plot +o graph multiple curves on the same 4indo4. )ou can use the eA0#ot command in combination 4ith hold on and hold off on the follo4ing 4a)3 eA0#ot(1st function)

/L/

;o#< on eA0#ot(2nd function) eA0#ot( rd function! ... eA0#ot(n-th function) ;o#< o-For e(ample. to graph and on the same plot. use >> eA0#ot(e'0('^252)-153) >> ;o#< on >> eA0#ot(#o,( *'^2*13)5#o,(10)) >> ;o#< o-+he result is displa)ed on the follo4ing graph. '. Limits

log(4 x 2+13)/log(10) 1.8

1.6

1.4

1.2

0.8

0.6 -6 -4 -2 0 x 2 4 6

,ou can use #+.+t to compute limits. left and right limits as 4ell as infinite limits. For e(ample. x 24 to evaluate the limit 4hen x O 2 of the function . 4e have3 x 2 >> s3.s ' >> #+.+t(('^2- )5('-2)& '& 2) ans = For left limits. add %#e-t%. >> #+.+t(a:s(')5'& '& 0& %#e-t%) ans = -1 Similarl) for right limits3 >> #+.+t(a:s(')5'& '& 0& %"+,;t%) ans = 1 8n- denotes the infinit) s)mbol follo4s3 >> #+.+t(e'0(-'^2-@)*3& '& 8n-) ans = 3 in *A+LAB. +hus. limits at infinit) can be evaluated as

/ 1; /

You might also like