Curve-Fitting and Interpolation
Curve-Fitting and Interpolation
Unit III
• Curve fitting
Curve-Fitting and Interpolation – linear least squares fitting problem
– transformations to linearize nonlinear problems
– three solution techniques:
• normal equations
• QR decomposition
• SVD
• Interpolation
– polynomial interpolation
– piecewise polynomial interpolation
– cubic splines
Unit III - Curve fitting and interpolation 1 Unit III - Curve fitting and interpolation 2
• finding an analytical function which approximates a • consider a single variable problem initially
set of data points • start with m data points (xi,yi), i=1,...,m
• statistics can quantify the relationship between the • choose n basis functions f1(x),..., fn(x)
fit and errors in data points (e.g. from experiment) • define a fit function F(x) = c1f1(x) +...+ cnfn(x) as a
• numerical methods is concerned with the linear combination of the basis functions
calculation of the fitting curve itself • the problem:
– find the unknown coefficients c1,..., cn so that F(xi) ! yi
– provide a means to quantify the fit
Unit III - Curve fitting and interpolation 3 Unit III - Curve fitting and interpolation 4
Unit III - Curve fitting and interpolation 5 Unit III - Curve fitting and interpolation 6
Least squares problem Geometry or algebra?
Unit III - Curve fitting and interpolation 7 Unit III - Curve fitting and interpolation 8
• differentiating gives (sums are i=1,...,m) • organizing in matrix form gives the normal
equations for the least squares fit:
Unit III - Curve fitting and interpolation 9 Unit III - Curve fitting and interpolation 10
Unit III - Curve fitting and interpolation 11 Unit III - Curve fitting and interpolation 12
Digression: vector derivatives Over-determined system
• now evaluate: #!/#c = 0
• how to differentiate with respect to a vector? • #!/#c = #/#c [yTy - 2yTAc + cTATAc]
• we need some properties of vector derivatives ... = -2ATy + [ATAc + (ATA)Tc]
– #(Ax)/#x = AT
– #(xTA)/#x = A = -2ATy + 2ATAc
– #(xTx)/#x = 2x =0
– #(xTAx)/#x = Ax + ATx
• the notation convention for vector derivative #/#x is
NOT standardized with respect to transposes:
– disagrees with Jacobian matrix definition
Unit III - Curve fitting and interpolation 13 Unit III - Curve fitting and interpolation 14
Unit III - Curve fitting and interpolation 15 Unit III - Curve fitting and interpolation 16
Unit III - Curve fitting and interpolation 19 Unit III - Curve fitting and interpolation 20
Unit III - Curve fitting and interpolation 21 Unit III - Curve fitting and interpolation 22
Unit III - Curve fitting and interpolation 23 Unit III - Curve fitting and interpolation 24
Why QR factorization? Why QR factorization?
• why doesn’t this approach give the LS solution? • however an orthogonal matrix Q preserves norms:
• the elementary row ops applied to A are ||Qx||2 = [(Qx)T(Qx)]1/2 = [xTQTQx]1/2 = [xTx]1/2 = ||x||2
• so to minimize ||y - Ac||2 we can ...
– look for an orthogonal Q so that ....
– minimizing ||QTy - QTAc||2 is an easy problem to solve
Unit III - Curve fitting and interpolation 25 Unit III - Curve fitting and interpolation 26
QR factorization QR Factorization
• can apply the Gramm-Schmidt process to the columns • each u can be expressed using the orthonormal basis as:
of A ui = (ui·q1)q1 + (ui·q2)q2 + ...+ (ui·qn)qn i = 1,2,...,n
– converts a basis {u1,...,un} into an orthonormal basis {q1,...,qn} • in matrix form this is
– the most direct approach to finding QR
– not numerically stable though
– other more sophisticated algorithms are used in practice
• consider a full rank n%n matrix A
• the columns of A are
– linearly independent • A = Q R
– form a basis of the column space of A
• this is the QR factorization required
• what is the relationship between
• why is R upper triangular?
A = [u1|u2|...|un] and Q = [q1|q2|...|qn]? – for j$2 the G-S ensures that qj is orthogonal to all u1, u2, ... uj-1
Unit III - Curve fitting and interpolation 27 Unit III - Curve fitting and interpolation 28
• then
• so
Unit III - Curve fitting and interpolation 29 Unit III - Curve fitting and interpolation 30
Full vs skinny QR factorization QR Factorization in Matlab
• can also have a full QR factorization with extra stuff • the Matlab function [Q,R] = qr(A) provides the QR
– Q m%m factorization of A
– R m%n • the function [Q,R] = qr(A,0) provides the skinny QR
• last m-n rows of R are zero factorization
• first n columns of Q span the column space of A – we’ll always use this one
• last m-n columns of Q span the orthogonal complement • example:
of the column space of A – calculate the skinny QR factorization of the A in the simple
– this is the nullspace of A 3-point fitting example on slide 23
– not needed to solve the LS problem – check that the residuals are small
– so the skinny QR factorization is good enough for LS problems
• in general if A has rank k<n only the first k columns of Q
are needed
Unit III - Curve fitting and interpolation 31 Unit III - Curve fitting and interpolation 32
Unit III - Curve fitting and interpolation 33 Unit III - Curve fitting and interpolation 34
Unit III - Curve fitting and interpolation 35 Unit III - Curve fitting and interpolation 36
QR vs Cholesky: example solution
QR vs normal equations
• in the example Cholesky solution of the normal
equations fails due to rounding error
• normal equations can be very inaccurate for ill-
conditioned systems where cond(ATA) is large
• but when m » n the normal equations involve
– half the arithmetic as compared to QR
– less storage than QR
• if A is ill-conditioned and ...
– residual is small then the normal equations are less
accurate than QR
– residual is large then both methods give an inaccurate LS
solution
• choosing the right algorithm is not an easy decision
Unit III - Curve fitting and interpolation 37 Unit III - Curve fitting and interpolation 38
Unit III - Curve fitting and interpolation 39 Unit III - Curve fitting and interpolation 40
Unit III - Curve fitting and interpolation 41 Unit III - Curve fitting and interpolation 42
Polynomial curve fitting
SVD and LS problem: zeroing
• consider curve-fitting with mononomial basis
• use a zero entry in S+ if "j = 0 (to machine functions {1, x, x2, ..., xk}
precision) • can solve the least-squares problem with any of the
• this forces a zero coefficient in the linear previous techniques (normal equations, QR, SVD)
combination of basis functions that gives the fitting • the simplicity of the basis functions allows some
equation .... trickery when setting up the equations
• ... instead of a random large coefficient that has to – to fit known data in column vectors x and y to the quadratic
y = c1x2 + c2x + c3 requires solving the over-determined
delicately cancel with another one system
• if the ratio "j / "1 ~ n&m then zero the entry in the
pseudo-inverse matrix since the value is probably
corrupted by roundoff anyway
Unit III - Curve fitting and interpolation 43 Unit III - Curve fitting and interpolation 44
Basic ideas
Interpolation
• given: support points (xi,yi) i = 1, ... ,n that are
• interpolation is the ‘exact’ form of curve-fitting
supposed to result from a function y = f(x)
• for given data points (xi,yi)
• to find: an interpolating function y = F(x) valid for a
– curve-fitting finds the solution to the over-determined
system that is closest to the data (least-squares solution) range of x values that includes the xi’s
– interpolation finds a function that passes through all the • requirements
points and ‘fills in the spaces’ smoothly – F(xi) = f(xi), i=1, ...,n
• piece-wise linear interpolation is the simplest, but – F(x) should be a good approximation of the f(x)
lacks smoothness at the support points (xi,yi) • the function f(x) may
• to approximate a function outside the range of – not be known at all
values of xi use extrapolation – not be easy (or possible) to express symbolically
– not easy to evaluate
– known only in tabular format
Unit III - Curve fitting and interpolation 47 Unit III - Curve fitting and interpolation 48
Basis functions Mononomial basis functions
• n basis functions '1, '2, ... , 'n can be used to • n basis functions are x0, x1, x2, ... , xn-1
define the interpolating function • there is a unique polynomial of degree n-1 passing
F(x) = a1'1(x)+ a2'2(x)+ ... + an'n(x) through n support points
• polynomial basis functions are common • so no matter how we get the interpolating
F(x) = a1+ a2x2+ ... + anxn polynomial it will be unique
• .....so are Fourier interpolations • however it can be expressed in multiple ways with
different coefficients multiplying the basis functions
F(x) = a1+ a2eix + ... + ane(n-1)ix
– for instance the offset monomials (x-a)0, (x-a)1, (x-a)2, ... ,
• polynomials are easy to evaluate but there are (x-a)n-1 could be used
numerical issues ... – we develop techniques to use alternative basis functions
for the interpolating polynomial (Lagrange, Newton)
• but first look at simple mononomials....
Unit III - Curve fitting and interpolation 49 Unit III - Curve fitting and interpolation 50
Unit III - Curve fitting and interpolation 51 Unit III - Curve fitting and interpolation 52
– serious problems with roundoff error in calculating the • system not ill-conditioned now !!
coefficients [see the oscillations in the plot] • coefficients vary over only 5 orders of magnitude
– no spurious oscillations now from roundoff
Unit III - Curve fitting and interpolation 53 Unit III - Curve fitting and interpolation 54
Lagrange polynomials Lagrange polynomials
• now the interpolating polynomial is expressed as
• an alternative basis for polynomials of degree n p1(x) = y1 L1(x) + y2 L2(x)
• first degree polynomial first.... • the basis functions L1(x) & L2(x) are called first-
• the (linear) poly passing through (x1,y1) & (x2,y2) is: degree Lagrange interpolating polynomials
• continuing to second degree we get...
L1 L2
• find this representation by ...
– writing the Vandermonde system
L1(x) L2(x) L3(x)
– solving for c1 and c2 and ...
– re-arranging the polynomial in the form above
Unit III - Curve fitting and interpolation 55 Unit III - Curve fitting and interpolation 56
Lagrange polynomials
Lagrange polynomials
• both Vandermonde and Lagrange can be
improved on
– too much arithmetic involved
– data points cannot be added or deleted without starting
the calculations again from scratch
– we don’t know what degree polynomial to use up front
and we can’t adjust that later without starting again
• an alternative is ....
Unit III - Curve fitting and interpolation 59 Unit III - Curve fitting and interpolation 60
Newton basis functions Newton basis functions
• Newton basis functions are 1, (x-x1), (x-x1)(x-x2), • first consider the quadratic version...
(x-x1)(x-x2)(x-x3), ..., (x-x1)(x-x2)...(x-xn) p2(x) = c1 + c2(x-x1) + c3(x-x1)(x-x2)
• express the interpolating polynomial pn(x) in terms • apply the support point constraints pn(xi) = yi
of this basis:
Unit III - Curve fitting and interpolation 61 Unit III - Curve fitting and interpolation 62
• continuing with the elimination we get... • in general, for nth degree poly interpolation we
get...
Unit III - Curve fitting and interpolation 63 Unit III - Curve fitting and interpolation 64
Divided differences
....nth order divided differences
• find the 3rd degree Newton interpolating poly for
first four data points in the table below
Unit III - Curve fitting and interpolation 67 Unit III - Curve fitting and interpolation 68
Unit III - Curve fitting and interpolation 69 Unit III - Curve fitting and interpolation 70
Unit III - Curve fitting and interpolation 71 Unit III - Curve fitting and interpolation 72
Piecewise interpolants Why cubic splines?
• piecewise linear on sub-intervals • piecewise linear interpolation provides....
– solve for coefficients using Lagrange interpolating – a smooth y'(x) everywhere and...
polynomials – zero y''(x) inside the sub-intervals but...
• piecewise quadratic: conic splines – undefined (or infinite) y''(x) at the breakpoints xj
– exact representation of lines, circle, ellipses, parabolas and
hyperbolas
• piecewise cubic: cubic splines
• what’s a spline?
• cubic splines improve the behaviour of y''(x) ....
– carries some element of slope or curve shape constraint
– represent the curve of minimum strain energy (abstraction of
beam theory, e.g. f''''= zero at break points)
– drafting device from old times
Unit III - Curve fitting and interpolation 73 Unit III - Curve fitting and interpolation 74
Unit III - Curve fitting and interpolation 75 Unit III - Curve fitting and interpolation 76
Unit III - Curve fitting and interpolation 77 Unit III - Curve fitting and interpolation 78
Cconstructing cubic splines Constructing cubic splines
• the second derivative is • we now get an equation to solve for these
unknown (assumed) yj'' values
• the trick is to demand continuity of the first
derivative at the breakpoints
• use eqn [2] to evaluate it at
• A = 1 and B = 0 when x=xj and opposite for xj+1 – x = xj the right endpoint of (xj-1,xj), and
so y''(xj) = yj'' and y''(xj+1) = yj+1'' – x = xj the left endpoint of (xj,xj+1) and
• this ensures that – equate these two for continuity
– the second derivative actually agrees with the tabulated • after some [messy] simplification you get the n-2
assumed values cubic spline equations (j= 2, ...,n-1) [eqn 3]
– the continuity condition for the second derivatives at the
breakpoints is satisfied
• so now what do we do about our assumed yj'' ??
Unit III - Curve fitting and interpolation 79 Unit III - Curve fitting and interpolation 80
Unit III - Curve fitting and interpolation 81 Unit III - Curve fitting and interpolation 82
Unit III - Curve fitting and interpolation 83 Unit III - Curve fitting and interpolation 84
Cubic splines: computational issues Cubic splines: example
• have to solve a linear system for the n unknowns • find the equations for the four natural cubic
yj'' consisting of splines which interpolate the data points:
– n-2 spine equations (-2,4), (-1,-1), (0,2), (1,1), (2,8)
– two boundary conditions
• all hj = xj+1 - xj = 1 for this data [nice]
• this is a (sparse) tri-diagonal linear system • the spline equations
• only the main diagonal and its neighbours have
non-zero entries
• this reflects the fact that each sub-interval in the
interpolation problem is coupled only to its two are (j = 2,3,4):
nearest neighbours
• special efficient algorithms are available for
solving tri-diagonal systems
Unit III - Curve fitting and interpolation 85 Unit III - Curve fitting and interpolation 86
Unit III - Curve fitting and interpolation 87 Unit III - Curve fitting and interpolation 88
..... S3 .... S4
• finally, for the 4th interval 1 < x < 2 we have j = 4: • these equations (S1-S4) are the four natural
cubic spline equations for y over the interval
-2 < x < 2
• to evaluate y(x) you decide which interval x lies
in and use the applicable equation
Unit III - Curve fitting and interpolation 93 Unit III - Curve fitting and interpolation 94