You are on page 1of 13

# Introduction to Matlab 9

Polynomial

## Omed Ghareb Abdullah

Sulaimani University
College of Sciences
Physics Department

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.

1
Polynomials
y Example:

250

## x = linspace (-5, 5, 100); 150

100

Y(x)
Y = x.^3+3.*x.^2+3.*x+1; 50

## plot (x, Y, 'r.-') 0

xlabel ('x')
-50

ylabel ('Y(x)')
-100
-5 -4 -3 -2 -1 0 1 2 3 4 5
title('Y(x)=x^3+3x^2+3x+1'); x

Polynomials
y Example:

250

## x = linspace (-5, 5, 100); 150

p = [ 1 3 3 1]; 100
Y(x)

## plot (x, Y, 'r.-') 0

xlabel ('x')
-50

ylabel ('Y(x)')
-100
-5 -4 -3 -2 -1 0 1 2 3 4 5
title('Y(x)=x^3+3x^2+3x+1'); x

2
Polyval: Evaluate polynomial
y = 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,
vector the polynomial is evaluated at
all points in x, using polyvalm for evaluation in a matrix
sense.
y Order n+1 (= number of coefficients)
y Degree n
5

Let we have two polynomials:
y1 = 3x 3 − 2.5 x 2 + 1.2 x1 + 15
y2 = 5 x 3 + 1.8 x 2 + 2.1x1 − 18
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.
p1=[3 -2
p1=[3, 2.5,
5 1.2,
1 2 15];
p2=[5, 1.8, 2.1, -18];
sum=p1+p2
sum =
8.0000 -0.7000 3.3000 -3.0000
6

3
Polynomials Multiplication
Let we have two polynomials:
y1 = 3x 3 − 2.5 x 2 + 1.2 x1 + 15
y2 = 5 x 3 + 1.8 x 2 + 2.1x1 − 18
The product of polynomial y1 and y2 can be calculated by using
'conv' commend.
.
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.0000
7

Polynomials Division
Let we have two polynomials:
y1 = 3x 3 − 2.5 x 2 + 1.2 x1 + 15
y2 = 5 x 3 + 1.8 x 2 + 2.1x1 − 18
The division of polynomial y1 and y2 can be calculated by using
'deconv' commend.
.
p1=[3 -2
p1=[3, 2.5,
5 1.2,
1 2 15];
q is the quotient polynomial
p2=[5, 1.8, 2.1, -18]; coefficient, and r is the remainder
polynomial coefficient.
[q,r]= deconv (p1,p2)
q= r=
0.6000 -0.0000 -3.5800 -0.0600 25.8000
8

4
Polynomials Derivatives
Let we have two polynomials:
y1 = 3x 3 − 2.5 x 2 + 1.2 x1 + 15
y2 = 5 x 3 + 1.8 x 2 + 2.1x1 − 18
The derivation of polynomial y1 or y2 can be calculated by using
'polyder' commend.
.
p1=[3 -2
p1=[3, 2.5,
5 1.2,
1 2 15];
p2=[5, 1.8, 2.1, -18];
d= polyder(p1)
d=
9.0000 -5.0000 1.2000
9

Roots of Nonlinear equation
Solutions of the nonlinear equations,
can be found using Newton
Newton’ss method

f(xn )
xn+1 = xn −
f '(xn )

10

5
Roots of Nonlinear equation
Find the solution of the following non‐linear
i i hi h l ( 8)
equation, with in the tolerance (10
x4 −30
f ( x) = e + 5 sin(x2 − 3)
f(xn )
xn+1 = xn − xn+1 − xn ≤ Tol
f '(xn )
x4 −30
f ' ( x) = 4 x e 3
+ 10x cos(x2 − 3)
11

Newton Method
% Newton's method for solving equations
clc;clear all
xo = 2;
tol = 1e-3;
iter = 0;
done = 0;
while ~ done,
iter = iter + 1;
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

6
Newton Method
y=exp(x.4-30)+5*sin(x. 2-3)
5

## % ploting function y=exp(x.^4-30)+5*sin(x.^2-

3

3) 2

-1.7321 1.7321
x=linspace(-2,2);
1

y=exp(x.^4-30)+5*sin(x.^2-3);
y-axies

plot(x,y,'r.-')
-1

xlabel('x-axies');
-2

ylabel('y-axies');
-3

title('y=exp(x.^4-30)+5*sin(x.^2-3)');
-4

-5
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
x-axies

13

Newton Method newton.m
function [x,iter] = newton(xo,tol,maxit)
% [x,iter] = NEWTON(xo,tol,maxit)
% Newton
Newton's
s method for solving equations
% x: the root, n: iteration number
% xo:first guess for the root
% tol: tolerance, maxit: maximum iteration
iter = 0; done=0;
while ~done,
iter = iter + 1;
f=exp(xo^4-30)+5*sin(xo^2-3);
fd
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

>> [x,iter]=newton(2,1e-3,10) 14

7
Polynomial Roots: Newton’s Method
¾ Find the root of:
f(x)= x3 – 3x2 + 3x – 1 = 0
f’(x)=3x2 – 6x + 3

## Answer (root) found after n iterations

15

Matlab Implementation: Newton’s Method
% Newton's method for solving equations
clc;clear all
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
end
16

8
Polynomial Roots
x=linspace(‐2,4)
30
y=(x ^3‐3
y=(x. 3‐3.*xx.^2+3*x‐1);
2+3 x‐1);
plot(x,y,'.r‐','LineWidth',2);
20

roots([1 ‐3 3 ‐1]) 10

-10

-20

-30
-2 -1 0 1 2 3 4

17

Polynomial Roots
¾Draw graph of polynomial:

100

p = [1 0 -9];
x = -10:0.01:10; 80

y=polyval(p,x); 60

plot(x,y)
40

grid
id on
20

## Note that the Roots are

-20
-10 -8 -6 -4 -2 0 2 4 6 8 10
+3 and - 3
18

9
Polynomial Roots
y The roots function can be used to calculates the roots of a polynomial.

p = [1 0 -9];
r=roots(p)

r=3
-3

## y By convention, MATLAB stores roots in column vectors. The function poly

returns the polynomial coefficients given the roots.
p2 = p
p poly(r)
y( )
p2 = 1 0 -9

y poly and roots are inverse functions, up to ordering, scaling, and roundoff
error.
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
g side.
2. Write the equation in matrix form.
3. Solve the resulting equation.

20

10
Systems of Linear Equations
Gaussian Substitution:
3x + 2y – 1z = 10
-3x + 9y + 6z = 15
x - y - z = -1

3x + 2y – 1z = 10
-3x + 9y + 6z = 15
+ _______________
11y + 5z = 25 11y + 5z = 25
-10y - 5z = -20
-x + 3y + 2z = 5 + _____________
x - y - z = -1 y = 5
+ _______________
2y + z =4 Etc…
21

Systems of Linear Equations
• Create a matrix of the
coefficients
• Create a column vector of the
values
• Use the inverse command to
tell MATLAB to solve the
system of equations.

clear all;clc
A=[3,2,-1;-1,3,2;1,-1,-1];
B=[10;5;-1]; S1 =
S1=inv(A)*B
‐2.0000
5.0000
‐6.0000 22

11
Systems of Linear Equations
• Create a matrix of the
coefficients
• Create a column vector of the
values
• Use the left division ( \ )
operator to tell MATLAB to
solve the system of
equations.

clear all;clc
A=[3,2,-1;-1,3,2;1,-1,-1];
B=[10;5;-1]; S2 =
S2=A\B
‐2.0000
5.0000
‐6.0000 23

Systems of Linear Equations
• Create a matrix of the
coefficients
• Create a column vector of the
values
• Use rref (reduced row
echelon form) function to
solve.

clear all;clc
A=[3,2,-1;-1,3,2;1,-1,-1];
B=[10;5;-1]; S3 =
C=[A B];
S3=rref(C) 1     0     0    ‐2
0     1     0     5
0     0     1    ‐6 24

12
RREF   Reduced row echelon form
R = RREF(A) produces the reduced row echelon form of A.

25

13