0% found this document useful (0 votes)
55 views22 pages

Lecture 7 Polynomials DR Nahid Sanzida

The document discusses solving systems of linear equations, polynomials, and nonlinear equations in MATLAB. It covers finding the inverse of a matrix to solve linear systems, using matrix division, solving polynomial roots with the roots command, differentiating and integrating polynomials with polyder and polyint, and using relational and logical operators like ==, >=, &, and |.

Uploaded by

Pulak Kundu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views22 pages

Lecture 7 Polynomials DR Nahid Sanzida

The document discusses solving systems of linear equations, polynomials, and nonlinear equations in MATLAB. It covers finding the inverse of a matrix to solve linear systems, using matrix division, solving polynomial roots with the roots command, differentiating and integrating polynomials with polyder and polyint, and using relational and logical operators like ==, >=, &, and |.

Uploaded by

Pulak Kundu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

ChE 208

Computer Programming and


Applications
Lecture 7
What we will learn in this
session

„ Solving systems of Linear Equations


„ Solving Polynomials/ Nonlinear Equations
„ More on Polynomials
Solutions to Systems of Linear
Equations
„ Example: a system of 3 linear equations with 3
unknowns (x1, x2, x3):
3x1 + 2x2 – x3 = 10
-x1 + 3x2 + 2x3 = 5
x1 – x2 – x3 = -1
Let :
⎡3 2 1⎤ ⎡ x1 ⎤ ⎡10 ⎤
A = ⎢⎢ − 1 3 2 ⎥⎥ x = ⎢ x2 ⎥ b=⎢5 ⎥
⎢ ⎥ ⎢ ⎥
⎢⎣ 1 − 1 − 1⎥⎦ ⎢⎣ x3 ⎥⎦ ⎢⎣ − 1⎥⎦

Then, the system can be described as:


Ax = b
Solutions to Systems of Linear Equation

„ Solution by Matrix Inverse:


Ax = b
A-1Ax = A-1b
x = A-1b
„ MATLAB:
>> A = [ 3 2 -1; -1 3 2; 1 -1 -1];
>> b = [ 10; 5; -1];
>> x = inv(A)*b
x=
-2.0000
Answer:
5.0000
-6.0000 x1 = -2, x2 = 5, x3 = -6
Solutions to Systems of Linear Equations (con’t…)

• Solution by Matrix Division:


The solution to the equation
Ax = b
can be computed using left division.
„ MATLAB:
>> A = [ 3 2 -1; -1 3 2; 1 -1 -1];
>> b = [ 10; 5; -1];
>> x = A\b Answer:
x= x1 = -2, x2 = 5, x3 = -6
-2.0000
5.0000 NOTE:
-6.0000 left division: A\b Î b ÷ A
right division: x/y Î x ÷ y
Solving Polynomial/Nonlinear Equations
A polynomial in MATLAB is represented by its coefficient
vector. For example, to define the cubic polynomial
=0
we type
we can solve polynomial equations in MATLAB via the
command roots:

% compute roots of polynomial


% with coefficients in c
>> plot (r, 'bx')
% see distribution of roots
Solving Nonlinear Equations

If y is a function of x, so that, y = f(x).


or, y =

we type,

To evaluate that polynomial at any point x, we


apply the command polyval as follows

>> y=polyval(c,5)
y=
303.5500
Plotting with Polynomial Equations

If x is a vector, then y will be the vector with


corresponding function values at the points in x.
i.e. Y = f(X)

To evaluate that polynomial at a number of points X,


we apply the command polyval as follows:

This last feature is useful to make a plot of the polynomial.


Plotting with Polynomial Equations

>> c = [ 3 -2.23 -5.1 9.8 ]


>> x=-1:0.1:1
>> y=polyval(c,x);
>> plot(x,y)
>> plot(x,y,'-*G')
Differentiating Polynomial Equations

MATLAB also supports the symbolic differentiation of


polynomials via the command polyder:

>> dc=polyder(c)
dc =
9.0000 -4.4600 -5.1000
Fitting Polynomial/Nonlinear Equations

Suppose, you have a set of data x vs. y


x y
1 5.5
2 43.1
3 128
4 290.7
5 498.4

You need to plot it!


Fitting Polynomial/Nonlinear Equations
ƒ For a given data set (x; y), where x and y are vectors of
the same size.
ƒ we can ask MATLAB to compute a polynomial that best fits
(in the least squares sense) these data with the command
polyfit.
ƒ We must supply to polyfit the degree d of the fitted
polynomial. For example,

x = [1 2 3 4 5];
y = [5.5 43.1 128 290.7 498.4];
>> p = polyfit(x,y,3)
p = -0.1917 31.5821 -60.3262 35.3400
-0.1917x3 +31.5821x2-60.3262x+35.3400
Fitting Polynomial/Nonlinear Equations

Brainstorming session!!

x2 = 1:0.1:5;
y2 = polyval(p,x2);
plot(x,y,'o',x2,y2)
grid on
Adding and Subtracting Polynomials
Polynomials can be added or subtracted
Ex: f1(x) + f2(x)
f1(x) = 3x6 + 15x 5 - 10x3 - 3x2 +15x - 40
f2(x) = 3x3 - 2x - 6
---------------------------------------------
= 3x6 + 15x 5 - 7x3 -3x2 +13x - 46
by just adding or subtracting the coefficient vectors
Both vectors must be of the same size
the shorter vector must be padded with zeros
We write,
p1 = [3 15 0 -10 -3 15 -40];
p2 = [0 0 0 3 0 -2 -6];
p = p1+p2
p = 3 15 0 -7 -3 13 -46
%f(x) = 3x6 + 15x5 -7x3 -3x2 +13x -46
Multiplying Polynomials

Polynomials can be multiplied


Ex: f1(x) * f2(x)
f1(x) = 2x2+ x -3
f2(x) = x + 1

We type
a = [2 1 -3];
b = [1 1];
c = conv(a, b)
c = 2 3 -2 -3
% 2x3+ 3x2-2x -3
Dividing Polynomials

Matlab can also divide polynomials


• [q,r ] = deconv(u, v)
– u is the coefficient vector of the numerator
– v is the coefficient vector of the denominator
– q is the coefficient vector of the quotient
– r is the coefficient vector of the remainder
Ex: (x2- 9x -10) ÷ (x + 1)
u = [1 -9 -10];
v = [1 1];
[q, r] = deconv(u,v)
q =1 -10 % quotient is (x - 10)
r = 0 0 0 % remainder is 0
Integral of Polynomials
3
Integrate Quartic Polynomial, I = ∫ (3 x 4 − 4 x 2 + 10 x − 25)dx
−1
• Matlab can calculate the integral of a polynomial
g = polyint(h,k)

h is the coefficient vector of the polynomial


g is the coefficient vector of the integral
k is the constant of integration – assumed to be 0 if not present
we type, p = [3 0 -4 10 -25 ];
q = polyint (p)
q = 0.600 0 - 1.333 5.000 - 25.000 0
Integral of Polynomials

Find the value of the integral, I, by evaluating q at


the limits of integration.
a=-1;
b=3;
I = diff (polyval (q, [a b]))
We got,
I =
49.0667
Operators (relational, logical)

„ == Equal to
„ ~= Not equal to
„ < Strictly smaller
„ > Strictly greater
„ <= Smaller than or equal to
„ >= Greater than equal to
„ & And operator
„ | Or operator
Operators (relational, logical)
X = 5;
Example X >= [1 2 3; 4 5 6; 7 8 10]
ans =

1 1 1
1 1 0
X = 5*ones(3,3); 0 0 0
X >= [1 2 3; 4 5 6; 7 8 10]
ans =
1 1 1
1 1 0
0 0 0
Truth Table

and or
Inputs A and B
A&B A|B
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1

You might also like