This document discusses several MATLAB functions for working with polynomials, including:
- Using polyfit to find the polynomial that best fits a set of data points
- Evaluating polynomials with polyval
- Storing and running MATLAB code from text files called M-files
- Performing operations on polynomials like multiplication with conv and division with deconv
3. Solving linear equation
Suppose that you would like to solve the following system of equations:
2X+Y-Z=6
X-Y-Z=-3
X+2Y-3Z=-9
In matrix form, you can write:
5. Quiz
• Write a vector start from zero and increment with 5 and end with 43.
• Write a vector with the following sequence[1 3 5 7 9 13 17 21 -2 -5 -7 -9]
• Create a vector and sort it.
• Create matrix [1 2 3;4 5 6;7 9 8] and delete second row.
• Solve the following equations
2X+Y-Z=6
X-Y-Z=-3
X+2Y-3Z=-9
Write a vector [1 2 3] and get the transpose.
6. Curve fitting
Given a set of Inputs and outputs for a particular system; MATLAB can
find a polynomial function the fit the output to input.
This is done using the polyfit instruction.
p = polyfit(x,y,n) finds the coefficients of a polynomial p(x) of degree
n that fits the data, p(x(i)) to y(i), in a least squares sense.
7. Curve fitting
Polynomial evaluation.
y = polyval(p,x)
Returns the value of a polynomial of degree n evaluated at x.
The input argument p is a vector of length n+1 whose elements are
the coefficients in descending powers of the polynomial to be
evaluated.
12. M-Files
• Text files containing MATLAB code.
• Useful for complex tasks.
we can store the typed input into a file and
tell MATLAB to get its input from that file.
Extension: .m
We can put comments on program to improve program quality and
enable to another programmer to edit.
13. M-Files
If an m-le contains MATLAB statements just as you would type them
into MATLAB, theyare called scripts.
M-les can also accept input and produce output, in which case they
are called functions.
The MATLAB executes the instructions, just as if you had typed them in
the command window.
14. Polynomials
Sometimes you need to:
Write a polynomial,
Find its roots,
Multiply it by another one,
Divide it by another polynomial,
Differentiate it,
Integrate it,
Substitute by a value in it, or
Put it in partial fractions form.
15. Polynomials
MATLAB provides us with a complete set to handle polynomials.
Polynomials are identified by their coefficients or by its roots.
N th order polynomial is defined by a row array of length N+1
containing the coefficients of the polynomial ordered by descending
powers.
Example:
p = [1 0 -2 -5];
18. Convolution and deconvolution
Polynomial multiplication convolution
Polynomial division deconvolution.
Example:
C = conv(a,b) a*b (a,b are polynomials)
[Z,r] = deconv(C,a) Z = C/a = b, r = [0]
19. Partial fraction expansion
residue finds the partial fraction expansion of the ratio of two
polynomials.
r is a column vector of residues,
p is a column vector of pole locations, and
k is a row vector of direct terms.
[r,p,k] = residue(b,a)
20. Partial fraction expansion
Note: Given three input arguments (r, p, k), residue converts back to
polynomial form.
[b2,a2] = residue(r,p,k)
Try this: