Está en la página 1de 14

Lecture14: Polynomials, Curve Fitting

Table of Contents
........................................................................................................................................ 1
Value of a polynomial ......................................................................................................... 1
Roots of a Polynomial ......................................................................................................... 2
More with Polynomials ........................................................................................................ 3
Derivatives of Polynomials ................................................................................................... 4
Curve Fitting ...................................................................................................................... 5
Curve Fitting with Polynomials ............................................................................................. 5
Curve Fitting with other Functions ......................................................................................... 8
How to Guess Appropriate Function ? .................................................................................... 9
Example: Fit the function for the given data ............................................................................ 9
Example 2: Temperature dependance of Viscosity ................................................................... 12
Several functions are provided in MATLAB to handle polynomials.
%Polynomials are the functions that have the form:
% $f(x) = c_n\,x^n + c_{n-1}\,x^{n-1}+\cdots + c_1\,x + c_0$
%where $c_n, c_{n-1}$ etc. are real numbers and n is a non-negative
%integer, called degree or order of the polynomial.
%In MATLAB, polynomials are represented by a row vector in which the
%elements are the coefficients, placed in a decreasing order of power. The
%vector should include all the coefficients including zeros.
%Examples:
%
%
%

f(x)
f(x)
f(x)
f(x)

=
=
=
=

8x + 5
:
2x^2 -4x
:
5x^5 - 6x^2 +7 :
6
:

p
d
k
c

=
=
=
=

[8 5]
[2 -4 0]
[5 0 0 -6 0 7]
[6]

Value of a polynomial
%syntax :

polyval(p,x) Evaluates the polynomial at x.

%p is a row vector with the coefficients of the polynomial.


%x is a number, or a variable that has an assigned value or a computable
%expression. x can also be a vector or a matrix.
%f(x) = x^5 - 12*x^4 + 40.5* x^3 -17.3 *x^2 + 34.24; Calculate f(9) and
%plot for -1.5 <= x <= 6.7.
p = [1 -12 40.5 -17.3 0 34.24];
polyval(p,9)
polyval(p,[2 5 7])

Lecture14: Polynomials, Curve Fitting

% To plot the above function


x = -1.5:0.1:6.7;
y = polyval(p,x);
plot(x,y)

ans =
8474.4

ans =
129.04

289.24

1073

Roots of a Polynomial
%roots of a polynomial are those values of the independent variable for
%which the polynomial is equal to zero.
%syntax: r = roots(p) where p is a vector of polynomial coefficients.
%r is a column vector representing the root or roots of that polynomial.
% f(x) = x^2 - 2x - 3

Lecture14: Polynomials, Curve Fitting

q = [1 -2 -3];
r = roots(q)
%g(x) = 4x^2 + 10x -8
s = roots([4 10 -8])
% When the roots are known, the coeficients of a polynomial can be found :
q2 = poly(s)

r =
3
-1

s =
-3.1375
0.63746

q2 =
1

2.5

-2

More with Polynomials


Addition
%Two polynomials can be aded by adding the vectors of the coefficients.
%f1 = 3x^6 + 15x^5 - 10x^3 - 3x^2 - 40 and f2 = 3x^3 - 2x - 6
p1 = [3 15 -10 -3 0 -40];
p2 = [3 -2 -6];
padd = p1 + [0 0 0 p2]

padd =
3

15

-10

-2

-46

Multiplication
%Two polynomials, a and b can be multiplied using a MATLAB function conv,
%syntax:

c = conv(a,b)

conv(p1,p2)

Lecture14: Polynomials, Curve Fitting

ans =
9

39

-78

-79

66

-102

80

240

Division
%A polynomial can be divided by another polynomial using MatLAB fun. deconv
%syntax: [q,r] = deconv(u,v) where u is vector with coeffs. of numerator
%polynomial and v is for denominator polynomial. q is a vector with the
%coefficients of a quotient polynomial and d for remainder polynomial.
% Dividing 2x^3 + 9x^2 + 7x - 6 by x+3
u = [2 9 7 -6]; v = [1 3];
[q,r] = deconv(u,v)
% Annother example:
w = [2 -3 0 5 2];
z = [1 0 -5];
[g,h] = deconv(w,z)
% so that the result is w = conv(z,g) + h

q =
2

-2

-3

10

r =
0

g =

h =
-10

52

Derivatives of Polynomials
%The built-in function polyder can be used for calculating the derivative

Lecture14: Polynomials, Curve Fitting

%of a polynomial, or a product of two polynomials or a ratio of two


%polynomials.
% l= polyder(p);
% l = polyder(a,b);
% [n,d]= polyder(u,v)

%for a single polynomial


%for a product of two polynomials
%for a ratio of two polynomials

%g1(x) = 3x^2 - 2x + 4 and g2(x) = x ;


g1 = [3 -2 4]; g2 = [1 0];
dp1 = polyder(g1)
dpp = polyder(g1,g2)
[pn pd] = polyder(g1,g2)

dp1 =
6

-2

dpp =
9

-4

-4

pn =

pd =

Curve Fitting
%The process of fitting a function to a set of data points. (regression).
%Function can be used as a mathematical model of the data.
%Function can be either linear or polynomial, or power, or exponential etc.
%Then how should one fit the function to the data? which function to fit ?
% what are its coefficients ?

Curve Fitting with Polynomials


% Polynomials can be used to fit the data in two ways:
% (i) Polynomials that pass through all the points
% For 2 points, the linear equation y = mx + c passes through the points.
% For n points, a polynomial of order n-1 passes through all points.

Lecture14: Polynomials, Curve Fitting

%The coefficients of the polynomial are determined by substituting each point


%in the polynomial and then solving the n-equations for the coefficients.
% (ii) Polynomials that do not necessarily pass any of the points.
%The Method of Least Squares is the most common way of fitting the data.
%Here the coefficients of the polynomial are determined by minimizing the
%sum of the squares of the residuals at all the data points.
%The residual at each point is defined as the difference between the value
%of the polynomial and the value of the data.
%Let the given points are: (x1,y1), (x2,y2), (x3,y3) and (x4,y4). Let us
%consider that a straight line best fits the data. i.e, $f(x) = a_1 x + a_0$.

%The residual at each point, $R_i = f(x_i) - y_i$. The sum of the squares of
%residuals is $R = \sum_{i=1}^4 (f(x_i) - y_i)^2 = \sum_{i=1}^4 (a_1 x_i + a_0 - y_
%Here, R is a function of a_1 and a_0. The minimum of R can be determine by
%taking the partial derivative of R wrt a_1 and a_0 equating them to zero.
%This results in 2 equations with 2 unknowns, a_1 and a_0. The solution of
%these equations gives the values of the coefficients of the polynomial that
%best fits the data. This procedure is called Least Square Method.
%MATLLAB has a built-in function called 'polyfit' which uses the LSM.
%syntax : p = polyfit(x,y,n) where p is the vector of the coefficients of
%the polynomial that fits the data, x is a vector of independent variable
%and y is a vector of a dependent variable of the data and n is the order
%of the polynomial.
%For a set of m points, a polynomial of order up to m-1 can be used for fit.
x = [0.9 1.5 3
4
6
8
9.5];
y = [0.9 1.5 2.5 5.1 4.5 4.9 6.3];
p = polyfit(x,y,3)
xp = 0.9:0.1:9.5;
yp = polyval(p,xp);
plot(x,y,'o',xp,yp)
xlabel('x'); ylabel('y')

p =
0.022043

-0.40047

2.6138

-1.4158

Lecture14: Polynomials, Curve Fitting

%Fitting the data with polynomials of different order.


x = [0.9 1.5 3
4
6
8
9.5];
y = [0.9 1.5 2.5 5.1 4.5 4.9 6.3];
for kk = 1:6
[xpk ypk] = fitpoly(x,y,kk);
subplot(2,3,kk);
plot(x,y,'o',xpk,ypk)
end

Lecture14: Polynomials, Curve Fitting

clf;
close all;

Curve Fitting with other Functions


%The commonly the functions used for curve fitting are:
%power, exponential, logarithmic, reciprocal. These can
%be fitted to the data with polyfit function by rewriting
%them as linear functions so as to fit with polyfit fun.
%The
% $y
% $y
% $y
% $y

forms of these functions are the following:


= bx^m$, i.e., $ln(y) = m ln(x) + ln(b)$ (power)
= b e^{mx}$ i.e., $ln(y) = mx + ln(b)$ (exponential)
= m ln(x)+b$ (logarithmic)
= 1/(mx+b)$ i.e., $1/y = mx + b$ (reciprocal)

%MATLAB function form for the above are:


%power
%exponential
%logarithmic
%reciprocal

:
:
:
:

p
p
p
p

=
=
=
=

polyfit(log(x),log(y),1)
polyfit(x,log(y),1)
polyfit(log(x),y,1)
polyfit(x,1./y,1)

Lecture14: Polynomials, Curve Fitting

How to Guess Appropriate Function ?


%
%
%
%
%

Exponential functions can not pass thro' origin.


Expo. funs. fit data which has either all y+'s or y-'s.
Log. funs. can NOT model x = 0 or negative values of x.
For the power fun. y = 0 when x = 0.
The reciprocal fun. can not model y = 0.

Example: Fit the function for the given data


%Determine the function w = f(t) which best fits the data
t = 0.0:0.5:5.0;
w = [6.00 4.83 3.70 3.15 2.41 1.83 1.49 1.21 0.96 0.73 0.64];
%Solution :
%Step 1: Plot the data with linear scales on both axis.

plot(t,w,'o'); xlabel('\bf t -->'); ylabel('\bf w -->'); title('\bf linear function

Step 2: Analysize the Plot. Guess the appropriate function to fit.


% It indicates that linear function (straight line) can not be a best fit.
% Logarithmic function is excluded (since function value is defined at t=0)

Lecture14: Polynomials, Curve Fitting

% Power function is excluded (since at t=0 w ~= 0).


% The other possible functions left are: exponential and reciprocal. Plot.
figure(2);
plot(t,log(w),'ro'); xlabel('\bf t '); ylabel('log(w)');title('exp. function');
figure(3);
plot(t,1./w,'ko'); xlabel('\bf t'); ylabel('1/w'); title('reciprocal function');
% The exponential function of form, y = be^{mx} seems to fit the data well.

10

Lecture14: Polynomials, Curve Fitting

Step 3: Fit the given data with chosen function and obtain coefficients.
% The constants b and m for the exp. fun. are determined as the following:
p = polyfit(t,log(w),1)
m = p(1), b = exp(p(2)) %since p(2) = log(b)
% Overlay Plot the data and the best fit function
ti = 0:0.1:5;
wi = b*exp(m*ti);
plot(t,w,'o',ti,wi)
xlabel('t'); ylabel('w');title('data with the fit');
%Note : Several other functions can also be used with polyfit function.
% Consult MATLAB books for more such problems.

p =
-0.45801

1.7899

m =
-0.45801

11

Lecture14: Polynomials, Curve Fitting

b =
5.9889

Example 2: Temperature dependance of Viscosity


%Viscosity of gases and fluids is their resistance to flow. It is sensitive
%to temperature. The table below, gives viscosity of oil at different temp.
%Determine an equation that can be fitted to the data.
T = -20:20:120;
mu = [4 0.38 0.095 0.032 0.015 0.0078 0.0045 0.0032];
plot(T,log(mu),'o')
%The plot appears that data points do not appear to fall in a straight
%line. It implies that simple exponential function of the form, y = be^(mx),
%which models a straight line with these axes, will not provide the best fit.

12

Lecture14: Polynomials, Curve Fitting

%Since the points lie along a curved line, possible function could be:
% log(mu) = a2 T^2 + a1 T + a0. It can be fitted with polyfit(x,y,2).
% mu = exp(a2 T^2 + a1 T + a0) = exp(a0) exp(a1 T) exp(a2 T^2)
TK = T+273;
p = polyfit(TK,log(mu),2)
Tplot = 273+[-20:120];
muplot = exp(p(1)*Tplot.^2 + p(2).*Tplot+p(3));
semilogy(TK,mu,'o',Tplot,muplot);
xlabel('Temperature (K)'); ylabel('Viscosity (N*s/m^2)');
title('Temperature dependance of Viscosity')
%The plot generated shows that the equation correlates well to the data points.

p =
0.00034154

-0.26847

13

47.167

Lecture14: Polynomials, Curve Fitting

Published with MATLAB R2013b

14

También podría gustarte