You are on page 1of 34

MATLAB-Tutorial 2

Class ECES-304 Class ESA251 Presented b: : Sh bh P t d Shubham Bh Bhat Presented bybyElmi Abu Bakar t

Contents
Polynomials Functions Programming

I. Polynomials - Functions
Polynomial functions : in the MATLAB polyfun directory
yp pp y Type help polyfun

I. Polynomials Representation & Evaluation


Representation : polynomials as row vectors containing coefficients ordered by descending powers.

E l i l function evaluates a polynomial at a i l l i l Evaluation : l polyval f specified value.

Possible to evaluate a polynomials in a matrix sense.

I : identity matrix

I. Polynomials Roots
roots function calculates the roots of a polynomial:

roots function calculates the roots of a polynomial:

poly and roots are inverse functions poly roots functions,

I. Polynomials Derivatives
polyder function computes the derivative of any polynomial.

derivative of the product a*b by polyder with a single output

derivative of the quotient a/b by polyder with two output

I. Polynomials Convolution
conv computes convolution operation

deconv computes deconvolution operation

I. Polynomials Partial Fraction Expansions


residue finds the partial fraction expansion of the ratio of two polynomials

Also, id converts b k t Al residue t back to polynomial form

I. Polynomials Polynomial Curve Fitting


polyfit finds the coefficients of a polynomial that fits a set of data in a least-squares sense

d t t i i the d data to be fitted, d is x and y are vectors containing th x and y d t t b fitt d and n i the degree of the polynomial to return

A third degree polynomial that approximately fits the data

I. Polynomials - Examples
Example

10

I. Polynomials - Examples
Example

11

I. Polynomials
Example

12

II. Functions Work with M-files


M-file
y p , you When you write a MATLAB function or script, y save it to a file called an M-file (named after its .m file extension)

Simple functions in M-file

Function Definition Line

13

II. Functions Work with M-files


Simple functions in M-file
p y The Function or Script Body : all the MATLAB code that p performs computations and assigns values to output arguments.

14

II. Functions Work with M-files


Creating a Simple M-file

15

II. Functions Work with M-files


Example (sind.m)

16

II. Functions Work with M-files


Example (sind.m)

* fplot : Plot function

17

II. Functions Work with M-files


Primary M-file functions
y The first function in any M-file

Nested Functions
one or more functions within another function in MATLAB

18

II. Functions Work with M-files


Subfunctions
M-files can contain code for more than one function M-files can contain code for more than one function Each subfunction begins with its own function definition line

19

III. Programming Operators


Arithmetic operators
p , y q For numeric computations, elements of arrays with equal dimensions

20

III. Programming Operators


Relational operators
p p p y Relational operators compare operands q quantitatively

21

III. Programming Operators


Logical Operators
p p g g y Element-wise operate on corresponding elements of logical arrays.

Short-circuit operate on scalar, logical expressions.

22

III. Programming Operators


Example

23

III. Programming Control Statements


Conditional control : if, elseif, else
g p if evaluates a logical expression and executes a g p of statements group when the expression is true. The optional elseif and else provide for the execution of alternate groups of statements. g p

24

III. Programming Control Statements


Example of if

25

III. Programming Control Statements


Conditional control : switch, case, otherwise
switch executes certain statements based on the value of a variable or expression

26

III. Programming Control Statements


Example : switch, case,

27

III. Programming Control Statements


Loop control : for, while, continue, break
p p for : to loop a specific number of times. while : more suitable for basing the loop execution on how long a condition continues to be true or false. continue and break : more control on exiting the loop. loop continue break

28

III. Programming Control Statements


Example : for

29

III. Programming Control Statements


Example : for

30

III. Programming Control Statements


Example : for

31

III. Programming Control Statements


Example : for

32

III. Programming - Development


Planning the Program g Using Pseudo-Code Selecting the Right Data Structures General Coding Practices Naming a Function Uniquely The Importance of Comments Coding in Steps Making Modifications in Steps Functions with One Calling Function g Testing the Final Program

33

Thank you

34

You might also like