ENG-102 2567 LinearEquationsPolynomial

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Week 10.

Linear Equations and Polynomial


ENG-102 Computer Programming for Engineer
Semester 1/2567

Lecturer : Prawet Ueatrongchit


Email : [email protected]
Phone Number & Line ID : 081-936-9340
Desk : C304
• Linear Algebraic Equation : It occurs in various engineering applications for
instance.
• Linear 3x1 − 4x2 = 7
Equation 2x1 + 9x2 = 5

Linear Equation

2 9 x1 5 x1 5 2 9 x1 2 9 5
= = / = \
3 −4 x2 7 x2 7 3 −4 x2 3 −4 7
Matrix notation Solving Solving
𝐀𝐱 = 𝐛 𝐱 = 𝐀/𝐛 𝐱 = 𝐀\𝐛
Size : 2x2 * 2x1 = 2x1 Size : 2x1 / 2x2 = 2x2 Size : 2x2 \ 2x1 = 2x1

The size of matrix notation corresponds to a number


of variable appearing in the set of linear equations 2
• Solving : The method of previously mentioned equation can be done in 2
different manners in MATLAB Programming.
• Linear
Equation 1. Using the left-division
>> A = [2,9;3,-4];
x1 2 9 5 >> b = [5;7];
= \
x2 3 −4 7 >> x = A\b;

2. Using matrix inversion


−1 >> x = A^-1 * b;
x1 2 9 5
= ∗ or
x2 3 −4 7 >> x = inv(A) * b;

** The property of a matrix inverse is determined from determinant value.


The determinant can be calculated from a square matrix
If det <= 0, the matrix IS NOT invertible.
3
• Example : 2 method for solving equation in MATLAB Programming.
>> A = [2 9 3; 3 -1 10; 0 2 -9];
• Linear >> b = [5;-7;9];
Equation >> x = A\b >> if det(A) > 0 >> if det(A) > 0
x = A^-1 * b , end x = inv(A) * b , end
x =
0.7238 x = x =
0.6778 0.7238 0.7238
-0.8494 0.6778 0.6778
-0.8494 -0.8494

>> A = [0 0 0; 0 0 0; 0 0 0];

>> x = A\b >> x = A^-1 * b >> x = inv(A) * b


Warning: Matrix is singular to working precision.
x = x = x =
Inf NaN NaN
NaN NaN NaN
NaN NaN NaN

Inf : Infinity NaN : Not-a-Number 4


• Polynomial : we will focus on polynomial on MATLAB

• Linear
Equation A polynomial is a mathematical
expression consisting of variables,
• Polynomial co-efficients, and the operations of
Analysis addition, subtraction, multiplication,
and non-negative integer exponents.

MATLAB represents a polynomial by row of its coefficients as following to


example
2x2 + 3y + 2 7x3 – x2 + 5x – 1 6x2 – 3

>> p = [2 3 2]; >> p = [7 -1 5 -1]; >> p = [6 0 -3];

5
• Polynomial (Root) : You can used roots() for convert to polynomial roots
and poly() for convert to polynomial.
• Linear
Equation Roots function results value of polynomial when equation equals to 0.
• Polynomial
a(x) : ( x2 + 3x = 0 )  ( x2 + 3x + 0 )
Analysis
>> a = [1,3,0];
>> r = roots(a)
x2 + 3x = 0
r =
0 02 + 3*0 = 0
-3 -32 + 3*-3 = 0

>> p = poly(r)
p =
1 3 0
6
• Polynomial Evaluation : A step of evaluation is simply substituting x value
and computing a result with function : polyval(p,x)
• Linear
Equation y = x2 + 2x + 1  p = [ 1 2 1 ]
• Polynomial
Analysis
>> polyval(p,0)
32 + 2*3 + 1
ans = = 16
1
02 + 2*0 + 1 = 1

>> x = [1,2,3,4];
>> y = polyval(p,x);
>> plot(x,y,'O')
>> axis([0 5 0 30])
x

7
• Example : Assume that the following polynomial represents the altitude (h(t))
in meters during the first 48 hours following the launch of a balloon:
• Linear
Equation h(t) = -0.12t4 + 12t3 – 380t2 + 4100t + 220
• Polynomial
Analysis where the units of t are hours. The corresponding polynomial model for the
velocity (v(t)) in meters per hour of the balloon is the following:

v(t) = -0.48t3 + 36t2 – 760t + 4100

Generate plots of the altitude and the velocity for this balloon using units of
meters and meters/sec. Also determine and print the peak altitude and its
corresponding time.

8
• Example : Scrip Code (data)
%-------------------------------------------------------
• Linear % This program generates altitude and velocity plots
% using polynomial models for the altitude and velocity
% of a balloon.
Equation % ------------------------------------------------------
• Polynomial clear, clf, clc;

Analysis t = 0:0.1:48;
sph = 60 * 60; % sec per hour

alt_coef = [-0.12 12 -380 4100 220];


vel_coef = [-0.48 36 -760 4100];

alt = polyval(alt_coef, t);


vel = polyval(vel_coef, t) / sph;

[peak_alt,time] = findpeaks(alt);
max_alt = max(peak_alt);
max_time = t(find(alt == max_alt));

9
• Example : Scrip Code (Plot)
subplot(211), plot(t,alt,max_time,max_alt,'o')
• Linear title('Balloon Altitude')
Equation xlabel('t,hours')
ylabel('meters')
• Polynomial
Analysis Msg1 = ['Max Altitude: ',num2str(max_alt,'%.2f')];
text(25,16000,Msg1,'HorizontalAlignment','center')

Msg2 = ['at Time: ',num2str(max_time)];


text(25,14000,Msg2,'HorizontalAlignment','center')

subplot(212), plot(t,vel);
title('Balloon Velocity')
xlabel('t,hours')
ylabel('m/s')
grid

10
• Example : Result (Figure)

• Linear
Equation
• Polynomial
Analysis

11
• Curve Fitting : The process of constructing a curve or mathematical function
that best fits the data.
• Linear
Equation pN = polyfit(x,y,N)

• Polynomial
− Fits a polynomial of degree N to data described
Analysis by the vector x and y, where x is the independent
variable.
− x and y are vectors of the same size.
− pN is a row vector of length N+1 that contains
the coefficients of the optimal polynomial in
order of descending powers.

y = x2

12
• Curve Fitting :

• Linear h(t) = -0.12t4 + 12t3 – 380t2 + 4100t + 220


degree = 4
order = 5
Equation
• Polynomial >> x = 0:0.1:48;

Analysis >> p = [-0.12 12 -380 4100 220];


>> y = polyval(p,x);
>> yN = polyfit(x,y,length(p)-1)
yN =
1.0e+03 *
-0.0001 0.0120 -0.3800 4.1000 0.2200

13
• Example : Find and plot the first-degree and second-degree polynomials for
the following data:
• Linear x
-1
y
0
Equation x = [-1 0 2] and y = [0 -1 3] 0 -1

• Polynomial >> x = [-1 0 2];


2 3

Analysis >> y = [0 -1 3];


>> p1 = polyfit(x,y,1) % first order polynomial (linear)
p1 =
1.1429 0.2857

>> p2 = polyfit(x,y,2) % second order polynomial


p2 =
1.0000 -0.0000 -1.0000

>> p3 = polyfit(x,y,3) % third order polynomial


Warning: Polynomial is not unique; degree >= number of data points.
p3 =
0.0000 1.0000 0.0000 -1.0000 14
• Example : Plotting

• Linear >> plot(x,y,'o','MarkerSize', 20), hold on;

Equation >> xp = -2:3/100:3;

• Polynomial >> y1 = polyval(p1,xp);


>> y2 = polyval(p2,xp);
Analysis >> plot(xp,y1,'g');
x y
-1 0
>> plot(xp,y2,'r-.');
0 -1
>> xp = -2:3/10:3; 2 3
>> y3 = polyval(p3,xp);
>> plot(xp,y3,'k.');

NOTE: The first-degree polynomial (p1) is 1.1429x + 0.2857


and the second-degree polynomial (p2) is x2 – 1
15
• Polynomial Arithmetic (Addition and Subtraction) : You can be used normal
add(+) and minus (-)
• Linear
Equation u(x) + v(x)

• Polynomial ( x4 – 3x2 – x + 24 ) + ( 4x3 – 2x2 + 5x – 16 )


Analysis
>> u = [1,0,-3,-1,24];
>> v = [0,4,-2,5,-16];
>> u + v
ans =
1 4 -5 4 8

>> u - v
ans =
1 -4 -1 -6 40

16
• Polynomial Arithmetic (Multiplication) : You can be use function convolution :
conv(u,v)
• Linear
Equation u(x) * v(x)

• Polynomial ( x4 – 3x2 – x + 24 ) * ( 4x3 – 2x2 + 5x – 16 )


Analysis
>> u = [1,0,-3,-1,24];
>> v = [0,4,-2,5,-16];
>> conv(u,v)
ans =
0 4 -2 -7 -14 83 -5 136 -384

>> conv(u,[4,-2,5,-16])
ans =
4 -2 -7 -14 83 -5 136 -384

The length of result coefficient vector is length(a) + length(b) - 1 17


• Polynomial Arithmetic (Division) : You can be used function convolution :
[q,r] = deconv(u,v)
• Linear
Equation u(x) / v(x)

• Polynomial ( x4 – 3x2 – x + 24 ) / ( 4x3 – 2x2 + 5x – 16 )


Analysis
>> u = [1,0,-3,-1,24];
>> v = [0,4,-2,5,-16];
>> [q,r] = deconv(u,v)
q =
0.2500 0.1250 % ( 0.25x + 0.125 )

r =
0 0 -4.0000 2.3750 26.0000

q : Quotient of coefficient vector


r : Remainder of coefficient vector
18
• Polynomial Arithmetic (Division vs Multiplication) :

• Linear
Equation u(x) * v(x)

• Polynomial ( x4 – 3x2 – x + 24 ) * ( 4x3 – 2x2 + 5x – 16 )


Analysis
>> u = [1,0,-3,-1,24];
>> v = [4,-2,5,-16];
>> c = conv(u,v); % size 1x8
>> [q,r] = deconv(c,v)
q =
1 0 -3 -1 24
r =
0 0 0 0 0 0 0 0

c = u * v  q = u = c / v , r = [0 ...] % 1x8
19

You might also like