Tải bản đầy đủ (.pdf) (10 trang)

Polynomials, Interpolation, and integralon

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (99.82 KB, 10 trang )


118
17. Polynomials, Interpolation,
and Integration
Polynomial functions are frequently used by numerical
methods, and thus MATLAB provides several routines
that operate on polynomials and piece-wise polynomials.
17.1 Representing polynomials
Polynomials are represented as vectors of their
coefficients, so f(x)=x
3
-15x
2
-24x+360 is simply
p = [1 -15 -24 360]
The roots of this polynomial (15, √24, and -√24):
r = roots(p)
Given a vector of roots
r
,
poly(r)
constructs the
coefficients of the polynomial with those roots. With a
little bit of roundoff error, you should see the original
polynomial. Try it.
The
poly
function also computes the characteristic
polynomial of a matrix whose roots are the eigenvalues of
the matrix. The polynomial f(x) was chosen as the
characteristic equation of the


magic(3)
matrix. Try:


A = magic(3)
s = poly(A)
roots(s)
eig(A)
f = poly(sym(A))
solve(f)
eig(sym(A))

119
17.2 Evaluating polynomials
You can evaluate a polynomial at one or more points with
the
polyval
function.
x = -1:2 ;
y = polyval(p,x)
Compare
y
with
x.^3-15*x.^2-24*x+360
. You can
construct a symbolic polynomial from the coefficient
vector
p
and back again:
syms x

f = poly2sym(p)
sym2poly(f)
17.3 Polynomial interpolation
Polynomials are useful as easier-to-compute
approximations of more complicated functions, via a
Taylor series expansion or by a low-degree best-fit
polynomial using the
polyfit
function. The statement:
p = polyfit(x, y, n)
finds the best-fit
n
-degree polynomial that approximates
the data points
x
and
y
. Try this example:
x = 0:.1:pi ;
y = sin(x) ;
p = polyfit(x, y, 5)
figure(1) ; clf
ezplot(@sin, [0 pi])
hold on
xx = 0:.001:pi ;
plot(xx, polyval(p,xx), 'r-')
Piecewise-polynomial interpolation is typically better
than a single high-degree polynomial. Try this example:

120

n = 10
x = -5:.1:5 ;
y = 1 ./ (x.^2+1) ;
p = polyfit(x, y, n)
figure(2) ; clf
ezplot(@(x) 1/(x^2+1))
hold on
xx = -5:.01:5 ;
plot(xx, polyval(p,xx), 'r-')
As
n
increases, the error in the center improves but
increases dramatically near the endpoints. The
spline

and
pchip
functions compute piecewise-cubic
polynomials which are better for this problem. Try:
figure(3) ; clf
yy = spline(x, y, xx) ;
plot(xx, yy, 'g')
Alternatively, with two inputs,
spline
and
pchip
return
a
struct
that contains the piecewise polynomial, which

can be later evaluated with
ppval
. Try:
figure(4) ; clf
pp = spline(x, y)
yy = ppval(pp, xx) ;
plot(xx, yy, 'c')
The
spline
function computes the conventional cubic
spline, with a continuous second derivative. In contrast,
pchip
returns a piecewise polynomial with a
discontinuous second derivative, but it preserves the
shape of the function better than
spline
.
Polynomial multiplication and division (convolution and
deconvolution) are performed by the
conv
and
deconv
functions. MATLAB also has a built-in fast Fourier
transform function,
fft
.

121
17.4 Numeric integration (quadrature)
The

quad
and
quadl
functions are the numeric
equivalent of the symbolic
int
function, for computing a
definite integral. Both rely on polynomial
approximations of subintervals of the function being
integrated.
quadl
is a higher-order method that can be
more accurate. The syntax
quad(@f,a,b)
computes an
approximate of the definite integral,

b
a
dxxf )(

Compare these examples:

quad(@(x) x.^5, 1, 2)
quad(@log, 1, 4)
quad(@(x) x .* exp(x), 0, 2)
quad(@(x) exp(-x.^2), 0, 1e6)
quad(@(x) sqrt(1 + x.^3), -1, 2)
quad(@(x) real(airy(x)), -3, 3)
with the same results from the Symbolic Toolbox:

int('x^5', 1, 2)
int('log(x)', 1, 4)
int('x * exp(x)', 0, 2)
int('exp(-x^2)', 0, inf)
int('sqrt(1 + x^3)', -1, 2)
int('real(airy(x))', -3, 3)
Symbolic integration (
int
) can find a simple closed-form
solution to the first four examples, above. The next is not
in closed form, and the last example cannot be solved by
int
at all. It can only be computed numerically, with
quad
.

122
The function
f
provided to
quad
and
quadl
must operate
on a vector
x
and return
f(x)
for each component of the
vector. An optional fourth argument to

quad
and
quadl

modifies the error tolerance. Double and triple integrals
are evaluated by
dblquad
and
triplequad
. Array-
valued functions are integrated with
quadv
.
18. Solving Equations
Solving equations is at the core of what MATLAB does.
Let us look back at what kinds of equations you have seen
so far in the book. Next, in this chapter you will learn
how MATLAB finds numerical solutions to nonlinear
equations and systems of differential equations.
18.1 Symbolic equations
The Symbolic Toolbox can solve symbolic linear systems
of equations using backslash (Section 16.9), nonlinear
systems of equations using the
solve
function (Section
16.11), and systems of differential equations using
dsolve
(Section 16.12). The rest of MATLAB focuses
on finding numeric solutions to equations, not symbolic.
18.2 Linear systems of equations

The pervasive and powerful backslash operator solves
linear systems of equations of the form
A*x=b
(Sections
3.3, 15.3, and 16.9). The expression
x=A\b
handles the
case when
A
is square or rectangular (under- or over-
determined), full-rank or rank-deficient, full or sparse,
numeric or symbolic, symmetric or unsymmetric, real or
complex, and all but one reasonable combination of this
extensive list (backslash does not work with complex
rectangular sparse matrices). It efficiently handles
triangular, permuted triangular, symmetric positive-

×