Documentos de Académico
Documentos de Profesional
Documentos de Cultura
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 :
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
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
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)
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
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 ?
%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
clf;
close all;
:
:
:
:
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)
10
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
b =
5.9889
12
%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
14