Upload
omed-ghareb
View
524
Download
0
Embed Size (px)
DESCRIPTION
Lecture (9): Matlab Polynomial - Sulaimani University - College of Science - Physics Department
Citation preview
1
Polynomial
Introduction to Matlab 9
Omed Ghareb AbdullahSulaimani UniversitySulaimani UniversityCollege of SciencesCollege of SciencesPhysics DepartmentPhysics Department
1
Polynomials• The polynomials are represented by their coefficients in
MATLAB.• Consider the following polynomial:
Y(x) = x3 + 3x2 + 3x + 1• For x is scalar: use scalar operations
Y = x^3 + 3*x^2 + 3*x + 1;
• For x is a vector or a matrix: use array or element by element operation
Y = x.^3 + 3*x.^2 + 3.*x + 1;• function polyval(p,x): evaluates a polynomial with
coefficients in vector p for the values in x.
2
2
Example:Example:
Y(x) = x3 + 3x2 + 3x + 1
Polynomials
50
100
150
200
250
Y(x
)
Y(x)=x3+3x2+3x+1
Y(x) x + 3x + 3x + 1
clear all;clcx = linspace (-5, 5, 100);
Y = x.^3+3.*x.^2+3.*x+1;
-5 -4 -3 -2 -1 0 1 2 3 4 5-100
-50
0
x
plot (x, Y, 'r.-')xlabel ('x')ylabel ('Y(x)')title('Y(x)=x^3+3x^2+3x+1');
3
Example:Example:
Y(x) = x3 + 3x2 + 3x + 1
Polynomials
50
100
150
200
250
Y(x
)
Y(x)=x3+3x2+3x+1
Y(x) x + 3x + 3x + 1
clear all;clcx = linspace (-5, 5, 100);p = [ 1 3 3 1];Y = polyval (p, x);
-5 -4 -3 -2 -1 0 1 2 3 4 5-100
-50
0
x
plot (x, Y, 'r.-')xlabel ('x')ylabel ('Y(x)')title('Y(x)=x^3+3x^2+3x+1');
4
3
Polyval: Evaluate polynomialy = polyval (p,x), when p is a vector of length n+1 whose
elements are the coefficients of a polynomial, is the value of the polynomial evaluated at x.
If x is a matrix or vector the polynomial is evaluated atIf x is a matrix or vector, the polynomial is evaluated at all points in x, using polyvalm for evaluation in a matrix sense.
Order n+1 (= number of coefficients)Degree n
5
Polynomials AdditionLet we have two polynomials:
152.15.23 1231 ++−= xxxy
The sum of polynomial y1 and y2 can be calculated by sum of the coefficients of two polynomials:the coefficient vectors must be the same length.
181.28.15 1232
1
−++= xxxy
y
p1=[3 -2 5 1 2 15];p1=[3, 2.5, 1.2, 15];
p2=[5, 1.8, 2.1, -18];
sum=p1+p2sum =
8.0000 -0.7000 3.3000 -3.00006
4
Polynomials MultiplicationLet we have two polynomials:
152.15.23 1231 ++−= xxxy
The product of polynomial y1 and y2 can be calculated by using 'conv' commend..
181.28.15 1232
1
−++= xxxy
y
p1=[3, -2.5, 1.2, 15];
p2=[5, 1.8, 2.1, -18];
prod= conv (p1,p2)prod=
15.0000 -7.1000 7.8000 17.9100 74.5200 9.9000 -270.00007
Polynomials DivisionLet we have two polynomials:
152.15.23 1231 ++−= xxxy
The division of polynomial y1 and y2 can be calculated by using 'deconv' commend..
181.28.15 1232
1
−++= xxxy
y
p1=[3 -2 5 1 2 15];p1=[3, 2.5, 1.2, 15];
p2=[5, 1.8, 2.1, -18];
[q,r]= deconv (p1,p2)q= r=
0.6000 -0.0000 -3.5800 -0.0600 25.8000
q is the quotient polynomial coefficient, and r is the remainder polynomial coefficient.
8
5
Polynomials DerivativesLet we have two polynomials:
152.15.23 1231 ++−= xxxy
The derivation of polynomial y1 or y2 can be calculated by using 'polyder' commend..
181.28.15 1232
1
−++= xxxy
y
p1=[3 -2 5 1 2 15];p1=[3, 2.5, 1.2, 15];
p2=[5, 1.8, 2.1, -18];
d= polyder(p1)d=
9.0000 -5.0000 1.20009
Roots of Nonlinear equation
Solutions of the nonlinear equations, can be found using Newton’s methodcan be found using Newton s method
)(xf)f(xx=x n
n+n '1 −)(xf n
10
6
Roots of Nonlinear equation
Find the solution of the following non‐linear i i h i h l ( 8)equation, with in the tolerance (108)
)f(x
)3sin(5)( 2304
−+− xe=xf x
)(xf)f(xx=x
n
nn+n '1 − Tolxx nn ≤−+1
)3cos(104)(' 2303 4
−+− xxex=xf x
11
Newton Method% Newton's method for solving equationsclc;clear allclc;clear allxo = 2;tol = 1e-3;iter = 0; done = 0;while ~ done,iter = iter + 1;f=exp(xo^4-30)+5*sin(xo^2-3);f=exp(xo^4-30)+5*sin(xo^2-3);fd=4*xo^3*exp(xo^4-30)+10*xo*cos(xo^2-3);x = xo - f / fd;done=( abs(x-xo)<tol );xo=x
end
12
7
4
5y=exp(x.4-30)+5*sin(x.2-3)
Newton Method
% ploting function y=exp(x.^4-30)+5*sin(x.^2-3)
x=linspace(-2,2);
y=exp(x.^4-30)+5*sin(x.^2-3);
plot(x,y,'r.-')-1
0
1
2
3
y-ax
ies
-1.7321 1.7321
xlabel('x-axies');
ylabel('y-axies');
title('y=exp(x.^4-30)+5*sin(x.^2-3)');
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-5
-4
-3
-2
x-axies
13
function [x,iter] = newton(xo,tol,maxit)% [x,iter] = NEWTON(xo,tol,maxit) % Newton's method for solving equations
newton.mNewton Method
% Newton s method for solving equations% x: the root, n: iteration number% xo:first guess for the root% tol: tolerance, maxit: maximum iterationiter = 0; done=0;while ~done,iter = iter + 1;f=exp(xo^4-30)+5*sin(xo^2-3);fd ( ) ( )
>> [x,iter]=newton(2,1e-3,10)
fd=4*xo^3*exp(xo^4-30)+10*xo*cos(xo^2-3);x = xo - f / fd;done=(iter>=maxit) | ( abs(x-xo)<tol );xo=x;
end
14
8
Find the root of:
f(x)= x3 – 3x2 + 3x – 1 = 0
Polynomial Roots: Newton’s Method
f’(x)=3x2 – 6x + 3
Answer (root) found after n iterations15
% Newton's method for solving equations
clc;clear all
xo 3;
Matlab Implementation: Newton’s Method
xo = 3;
tol = 1e-3;
iter = 0;
done = 0;
while ~ done,
iter = iter + 1;
f=xo^3 3*xo^2+3*xo 1;f=xo^3-3*xo^2+3*xo-1;
fd=3*xo^2-6*xo+3;
x = xo - f / fd;
done=( abs(x-xo)<tol );
xo=x
end16
9
30
x=linspace(‐2,4)
y=(x ^3‐3 *x ^2+3*x‐1);
Polynomial Roots
0
10
20
y=(x. 3‐3. x. 2+3 x‐1);
plot(x,y,'.r‐','LineWidth',2);
roots([1 ‐3 3 ‐1])
-2 -1 0 1 2 3 4-30
-20
-10
17
Polynomial RootsDraw graph of polynomial:
40
60
80
100
p = [1 0 -9]; x = -10:0.01:10; y=polyval(p,x);plot(x,y)
id
-10 -8 -6 -4 -2 0 2 4 6 8 10-20
0
20
Note that the Roots are +3 and - 3
grid on
18
10
Polynomial RootsThe roots function can be used to calculates the roots of a polynomial.
p = [1 0 -9]; r=roots(p)
r = 3 - 3
By convention, MATLAB stores roots in column vectors. The function polyreturns the polynomial coefficients given the roots.
p2 = poly(r)p p y( )p2 = 1 0 -9
poly and roots are inverse functions, up to ordering, scaling, and roundofferror.
Find Roots roots(p) returns the roots of the polynomial p in column vector form.
Find Polynomials poly(r) returns the coefficient vector of the polynomial having roots r.19
Systems of Linear Equations• Recall solution of simple Systems of Linear
Equations.
• MATLAB’s built-in matrix capabilities allow for easy solutions as well.
General Approach
1. Rearrange the equation with all unknown quantities on the left-hand side and all known on the right-hand side.g
2. Write the equation in matrix form.
3. Solve the resulting equation.
20
11
Systems of Linear EquationsGaussian Substitution:
3x + 2y – 1z = 10
3x + 2y – 1z = 10
-3x + 9y + 6z = 15
+ _______________
11y + 5z = 25 11y + 5z 25
-3x + 9y + 6z = 15
x - y - z = -1
11y + 5z = 25
-x + 3y + 2z = 5
x - y - z = -1
+ _______________
2y + z = 4
11y + 5z = 25
-10y - 5z = -20
+ _____________
y = 5
Etc…21
Systems of Linear Equations
• Create a matrix of the coefficients
• Create a column vector of the
clear all;clc
A=[3,2,-1;-1,3,2;1,-1,-1];
• Create a column vector of the values
• Use the inverse command to tell MATLAB to solve the system of equations.
B=[10;5;-1];
S1=inv(A)*BS1 =
‐2.0000
5.0000
‐6.0000 22
12
Systems of Linear Equations
• Create a matrix of the coefficients
• Create a column vector of the
clear all;clc
A=[3,2,-1;-1,3,2;1,-1,-1];
• Create a column vector of the values
• Use the left division ( \ ) operator to tell MATLAB to solve the system of equations.
B=[10;5;-1];
S2=A\BS2 =
‐2.0000
5.0000
‐6.0000 23
Systems of Linear Equations
• Create a matrix of the coefficients
• Create a column vector of the
clear all;clc
A=[3,2,-1;-1,3,2;1,-1,-1];
• Create a column vector of the values
• Use rref (reduced row echelon form) function to solve.
B=[10;5;-1];
C=[A B];
S3=rref(C)
S3 =
1 0 0 ‐2
0 1 0 5
0 0 1 ‐6 24
13
RREF Reduced row echelon formR = RREF(A) produces the reduced row echelon form of A.
[R,jb] = RREF(A,TOL) uses the given tolerance in the rank tests.
25