MATH2089 NM Lectures Topic3
MATH2089 NM Lectures Topic3
6
Lots of data with possible measurement
5
errors 4
1
−2 0 2 4 6 8 10 12
Polynomial interpolation
Approximating a complicated function by 20
polynomial interpolation 0
−100
−3 −2 −1 0 1 2 3 4
Key concepts.
Least squares: overdetermined linear system Au = y (A is m×n with m > n)
Method 1: normal equations (A⊤ A) u = A⊤ y
Cholesky or LU fact. + forward & back subs. = O(m n2 ) flops
Issue: squaring condition number
Method 2:
QR factorization + back substitution = O(m n2 ) flops
Benefit: numerically preferable
Matlab backslash command: u = A \ y
Polynomial interpolation:
Standard representation: p(x) = a0 + a1 x + a2 x2 + · · · + an xn
Lagrange polynomials: p(x) = y0 ℓ0 (x) + y1 ℓ1 (x) + · · · + yn ℓn (x)
Equally spaced points – large error near end points
Chebyshev points – minimize the error
1
Data fitting
Given data points (xj , yj ) for j = 1, . . . , m
Consider a polynomial of degree n − 1 =⇒ n unknown coefficients
y(x) = a0 + a1 x + a2 x2 + · · · + an−1 xn−1
8
0
−20
6
−40 5
4
−60
−80
2
−100 1
−3 −2 −1 0 1 2 3 4 −2 0 2 4 6 8 10 12
m > n =⇒ linear least squares: find a curve of best fit to the data by
minimizing the sum of squared residuals
m m
X X 2
rj2 := y(xj ) − yj = krk22 = kAu − yk22
j=1 j=1
2
Example. Use Matlab to find the line of best fit y(x) = α + β x to the data
j 1 2 3 4 5
xj 0 0.5 1 1.5 2
yj 1.0 0.6 0.4 0.1 0.08
Answer. In this case, we have m = 5, n = 2, and the linear system Au = y, with
1 x1 y1
1 x2 y2
α
A= 1 x3 ,
u= , y= y3 .
1 x4 β y4
1 x5 y5
xdat = [0:0.5:2];
ydat = [1.0 0.6 0.4 0.1 0.08];
xdat = xdat(:);
ydat = ydat(:);
A = [ones(size(xdat)) xdat];
u = A \ ydat
u =
0.9040
-0.4680
Thus α = 0.904 and β = −0.468. The line of best fit is y(x) = 0.904 − 0.468 x.
x = linspace(0,2,100);
y = u(1) + u(2)*x;
plot(x,y,’b-’,xdat,ydat,’m*’);
0.8
0.6
0.4
0.2
−0.2
0 0.5 1 1.5 2
3
Example. Use Matlab to find the quadratic of best fit y(x) = α + βx + γ x2 to
the data in the previous example.
Answer. In this case, we have m = 5, n = 3, and the linear system Au = y, with
1 x1 x21
y1
2
1 x2 x2 α y2
2
A= 1 x3 x3 , u = β , y = y3 .
1 x4 x24
γ y4
2
1 x5 x5 y5
u =
0.9983
-0.8451
0.1886
x = linspace(0,2,100);
y = u(1) + u(2)*x + u(3)*x.^2;
plot(x,y,’b-’,xdat,ydat,’m*’);
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 0.5 1 1.5 2
4
Example. Modify the Matlab commands in the previous example to obtain a
polynomial interpolation to the data. What is the degree of the polynomial?
Answer. We need a polynomial of degree 4 to interpolate 5 points. So we have
m = n = 5, and the linear system Au = y, with
1 x1 x21 x31 x41
a0 y1
1 x2 x22 x32 x42 a1 y2
2 3 4
A= 1 x3 x3 x3 x3 , u = a2
, y = y3 .
1 x4 x24 x34 x44
a3 y4
2 3 4
1 x5 x5 x5 x5 a4 y5
u =
1.0000
-1.5400
2.2467
-1.7600
0.4533
The interpolating polynomial is y(x) = 1 − 1.54 x + 2.2467 x2 − 1.76 x3 + 0.4533 x4 .
x = linspace(0,2,100);
y = u(1) + u(2)*x + u(3)*x.^2 + u(4)*x.^3 + u(5)*x.^4;
plot(x,y,’b-’,xdat,ydat,’m*’);
Polynomial interpolation
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0 0.5 1 1.5 2
5
Example. Approximate the data from the previous examples by an exponential
function y(x) = λ eµ x .
Answer. The data values yield a system of nonlinear equations in λ and µ:
λ eµ x1 = y1
λ eµ x2 = y2
..
.
µ xm
λe = ym
Instead of solving this nonlinear least squares problem (which requires iterative
methods), we can convert this into a linear least squares problem by taking loga-
rithms:
y(x) = λ eµ x =⇒ ln(y(x)) = ln(λ eµ x ) = ln(λ) + µ x
We obtain the linear system
ln(λ) + µ x1 = ln(y1 )
ln(λ) + µ x2 = ln(y2 )
..
.
ln(λ) + µ xm = ln(ym )
Thus
1 x1 ln(y1 )
1 x2 ln(y2 )
ln(λ)
A = . .. , u= , b = . .
.. . µ ..
1 xm ln(ym )
Define xdat and ydat as before. Then compute
A = [ones(size(xdat)) xdat];
u = A \ log(ydat)
lambda = exp(u(1))
mu = u(2)
u =
0.1176
-1.3686
lambda =
1.1247
mu =
-1.3686
6
Thus λ = e0.1176 = 1.1247 and µ = −1.3686.
The exponential approximation is y(x) = 1.1247 e−1.3686 x .
x = linspace(0,2,100);
y = lambda * exp(mu*x);
plot(x,y,’b-’,xdat,ydat,’m*’);
Exponential approximation
1.4
1.2
0.8
0.6
0.4
0.2
0
0 0.5 1 1.5 2
Note that we obtained the least squares solution for the converted linear problem,
which is not the same as the least squares solution for the original nonlinear
problem.
1
−2 0 2 4 6 8 10 12
7
Two n × 1 vectors u and v are orthogonal ⇐⇒ u⊤ v = u · v = 0
An m × n matrix Q is orthogonal ⇐⇒ Q⊤ Q = In
Q = [q 1 q 2 · · · q n ] has orthonormal columns q i , i.e.,
(
q⊤
i q j = 0 for all i 6= j (the column vectors are orthogonal)
2
q⊤
i q i = kq i k2 = 1 for all i (the column vectors have unit length)
kQk2 = 1
kQAk2 = kAk2 for any A
If Q is not square, then QQ⊤ 6= Im in general.
If Q is square, then Q⊤ Q = In = QQ⊤ , and so Q−1 = Q⊤ .
2 2 − 23 − 13 − 23 −3 −6 − 23 − 13
" #
2 −3 −6
−2 −8 = − 23 − 13 0 6 = 2
− 23
3 3
0 6
1 −2 − 13 − 23 2
3 0 0 − 13 − 23 R
A Q Y
8
The singular values σ1 ≥ σ2 ≥ · · · ≥ σn ≥ 0 of an m × n real matrix A are the
square-roots of the eigenvalues of A⊤ A .
A has full column rank ⇐⇒ columns of A are linearly independent ⇐⇒
σn > 0
r
Recall kAk2 := max |λi (A⊤ A)| = σ1
1≤i≤n
9
Linear least squares
m data points and n unknowns, m > n (more equations than unknowns)
Au = y
1 x1 x21 ··· xn−1
a0
y 1
1
1 x2 x22 ··· xn−1 a1
y 2
2
. .. .. .. .. =
..
..
.. . . . . . .
1 xm x2m ··· n−1
xm an−1 ym
A u y
A is an m × n matrix
A has full column rank
Residual r := Au − y
Pm
Find u to minimize krk22 = r ⊤ r = 2
j=1 rj
Au = y =⇒ (A⊤ A) u = A⊤ y
Define n × n matrix B = A⊤ A
A full column rank =⇒ B symmetric and positive definite
Solve Bu = A⊤ y by Cholesky or LU factorization with forward & back
substitutions
Cost: O(m n2 ) flops (dominated by computing B)
Issue: κ2 (B) = κ2 (A⊤ A) = [κ2 (A)]2 , squaring condition number!
Solution method 2: use QR factorization
R R
A=Q = [Y Z] =YR
0 0
Au = y =⇒ (Y R) u = y =⇒ (Y ⊤ Y )Ru = Y ⊤ y =⇒ Ru = Y ⊤ y
A full column rank ⇐⇒ R non-singular
Solve Ru = Y ⊤ y using back substitution
Cost: O(m n2 ) flops (dominated by QR factorization)
Benefit: κ2 (R) = κ2 (A), condition number unchanged, so numerically
preferable
Concerns: QR factorization is more costly than Cholesky or LU factoriza-
tion; Q (and therefore Y ) is generally not sparse
Matlab u = A \ y
10
Exercise. Outline the steps in the two equivalent methods for finding the least
square solution to Au = y and count the flops in each step.
11
Polynomial interpolation
Pn
Polynomial of degree n: p(x) = i=0 ai xi =⇒ n + 1 coefficient
Basis functions: xi for i = 0, . . . , n
n + 1 data points: (xj , yj ) for j = 0, . . . , n
Interpolation property : p(xj ) = yj for j = 0, . . . , n
Pn i
Linear system: i=0 ai xj = yj for j = 0, . . . , n, or in matrix form
Example. Linear interpolation: find the polynomial (line) that interpolates the
data (x0 , y0 ) and (x1 , y1 ).
Answer. Let p(x) = a0 + a1 x. Then solve the simultaneous equations
( (
p(x0 ) = y0 a0 + a1 x0 = y0
=⇒
p(x1 ) = y1 a0 + a1 x1 = y1
to obtain a0 and a1 .
Alternatively, a quick informal approach is to equate the ratios
p(x) − y0 y1 − y0
= ,
x − x0 x1 − x0
and then rearrange to obtain
y1 − y0
p(x) = y0 + (x − x0 ).
x1 − x0
12
Polynomial interpolation in Lagrange form
n + 1 data points: (xj , yj ) for j = 0, . . . , n
Lagrange polynomials of degree n as basis functions:
n
Y x − xj
ℓi (x) := for i = 0, . . . , n
xi − xj
j=0
j6=i
13
Exercise. Matlab expolint.m (using horner.m) produces the following plot for
the interpolating polynomial through the 5 data points
(−2, 4), (−1, 2), (0, −3), (1, 5), (3, 4).
Polynomial interpolation
20
−20
−40
−60
−80
−100
−3 −2 −1 0 1 2 3 4
14
Equally spaced points (or nodes)
b−a
On [a, b], xj = a + jh for j = 0, . . . , n, with h =
n
1
Carl Runge (1856 - 1927) example: f (x) = on [−1, 1]
1 + 25x2
Large error near end points
Error grows as degree increases!
Chebyshev points
n
Y
Choose xj to minimize max (x − xj )
x∈[−1,1]
j=0
2n + 1 − 2j
On [−1, 1], tj = cos π for j = 0, . . . , n
2n + 2
a+b b−a
On [a, b], xj = + tj for j = 0, . . . , n
2 2
Chebyshev nodes are the zeros of the Chebyshev polynomial Tn+1 (x) of the
first kind of degree n + 1 (details omitted)
Interpolation error is minimized by choosing Chebyshev nodes
We can also represent the interpolating polynomial using Chebyshev poly-
nomials Ti (x) for i = 0, . . . , n as basis functions (details omitted)
0.5
0.5
15
Polynomial of degree n
n
X
p(x) = ak xk = an xn + an−1 xn−1 + · · · + a2 x2 + a1 x + a0 , an 6= 0
k=0
2n = O(n) flops.
Matlab p = horner(a,x)
Allows a vector x of inputs
Computes p(x) = a1 + a2 x + · · · + an xn−1 + an+1 xn for each input
Returns a vector p of outputs of the same length as x
Elementwise multiplication .*
Matlab vector/array indices always start with 1
Matlab p = polyval(b,x)
Coefficients in reverse order p(x) = b1 xn + b2 xn−1 + · · · + bn x + bn+1
Degree n implied by length n + 1 of input vector b
16