Upload
siow-shung-churn
View
219
Download
0
Embed Size (px)
Citation preview
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 1/34
-Class ECES-304
Class ESA251
resen e y : u am a
Presented by : Elmi Abu Bakar
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 2/34
Content s
Polynomials
Functions
Programming
2 2
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 3/34
I. Polynomials - Funct ions
Polynomial funct ions : in the MATLAB polyfun dir ectoryT e ‘ hel ol fun ’
3 3
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 4/34
I. Polynomials –Represent at ion & Evaluat ion
Representation : polynomials as row vectors containingcoefficients ordered by descending powers.
va uat on : po yva unct on eva uates a po ynom a at aspecified value.
Possible to evaluate a polynomials in a matrix sense.
I : ident it y mat rix
4 4
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 5/34
I. Polynomials –Root s
‘roots ’ funct ion calculates t he root s of a polynomial:
‘ ’
‘ ’ ‘ ’ ,
5 5
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 6/34
I. Polynomials –Derivat ives
‘polyder ’ funct ion computes t he derivat ive of any polynomial.
derivative of the product a*b by ‘ polyder ’ with a single output
derivat ive of t he quot ient a/ b by ‘ polyder ’ with two output
6 6
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 7/34
I. Polynomials –Convolut ion
‘conv ’ computes convolut ion operat ion
‘deconv ’ computes deconvolut ion operat ion
7 7
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 8/34
I. Polynomials –Part ial Fract ion Expansions
‘residue ’ f inds t he par t ial f ract ion expansion of t he rat io of twopolynomials
so, res ue conver s ac opolynomial form
8 8
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 9/34
I. Polynomials –Polynomial Curve Fit t ing
‘polyfit ’ f inds t he coeff icient s of a polynomial t hat f it s a set ofdata in a least -squares sense
x an y are vec ors con a n ng e x an y a a o e e , an n sthe degree of t he polynomial t o return
A third degree polynomial t hat approximately fit s t he dat a
9 9
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 10/34
I. Polynomials - Examples
Example
10 10
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 11/34
I. Polynomials - Examples
Example
1111
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 12/34
I. Polynomials
Example
12 12
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 13/34
II. Funct ions –Work wit h M-f iles
M-fileWhen ou writ e a MATLAB function or scri t , ou save it t o a f ilecalled an M-f ile (named aft er i t s .m file extension)
Simple f unct ions in M-f ile
Funct ion Definit ion Line
13 13
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 14/34
II. Funct ions –Work wit h M-f iles
Simple f unct ions in M-f ileThe Funct ion or Scri t Bod : all the MATLAB code that erformscomputations and assigns values to output arguments.
14 14
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 15/34
II. Funct ions –Work wit h M-f iles
Creat ing a Simple M-f ile
15 15
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 16/34
II. Funct ions –Work wit h M-f iles
Example (sind.m)
16 16
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 17/34
II. Funct ions –Work wit h M-f iles
Example (sind.m)
* fplot : Plot funct ion
17 17
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 18/34
II. Funct ions –Work wit h M-f iles
Primary M-file funct ionsThe first funct ion in an M-fi le
Nested Funct ions
18 18
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 19/34
II. Funct ions –Work wit h M-f iles
SubfunctionsM-f iles can contain code for more t han one funct ionM-f iles can contain code for more t han one funct ion
Each subfunction begins with it s own funct ion definit ion line
19 19
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 20/34
III. Programming –Operat ors
Arit hmetic operatorsFor numeric com utat ions, element s of arra s wit h e ual dimensions
20 20
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 21/34
III. Programming –Operat ors
Relational operatorsRelat ional o erators com are o erands uant it at ivel
2121
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 22/34
III. Programming –Operat ors
Logical OperatorsElement -wise —o erat e on corres ondin element s of lo ical arra s.
Short -circuit —operate on scalar, logical expressions.
22 22
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 23/34
III. Programming –Operat ors
Example
23 23
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 24/34
III. Programming –Cont rol St at ement s
Condit ional cont rol : if , elseif , else‘if’ evaluat es a lo ical ex ression and executes a rou of st at ement swhen the expression is t rue.The opt ional ‘ elseif ’ and ‘ else ’ provide for t he execut ion of alt ernat e
rou s of st at ement s.
24 24
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 25/34
III. Programming –Cont rol St at ement s
Example of ‘ if ’
25 25
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 26/34
III. Programming –Cont rol St at ement s
Condit ional cont rol : swi t ch, case, otherwiseswitch executes certain statements based on the value of a variable orexpression
26 26
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 27/34
III. Programming –Cont rol St at ement s
Example : switch, case,
27 27
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 28/34
III. Programming –Cont rol St at ement s
Loop cont rol : for, while, cont inue, break‘ for’ : t o loo a s ecific number of t imes.‘ whil e’ : more suit able for basing the loop execut ion on how long acondition continues to be true or false.‘ ’ ‘ ’ .
28 28
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 29/34
III. Programming –Cont rol St at ement s
Example : for
29 29
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 30/34
III. Programming –Cont rol St at ement s
Example : for
30 30
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 31/34
III. Programming –Cont rol St at ement s
Example : for
3131
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 32/34
III. Programming –Cont rol St at ement s
Example : for
32 32
8/2/2019 Tutorial Matlab2
http://slidepdf.com/reader/full/tutorial-matlab2 33/34
III. Programming - Development
Planning t he ProgramUsin Pseudo-CodeSelect ing t he Right Data St ructures
General Coding PracticesNaming a Function UniquelyThe Importance of Comments
Making Modificat ions in St epsFunct ions with One Call in Funct ionTesting the Final Program
33 33