lecture 11 chap. 15. outline-1 15.1 interpolation – 15.1.1 linear interpolation – 15.1.2 cubic...

27
Lecture 11 Chap. 15

Upload: pamela-summers

Post on 18-Dec-2015

233 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Lecture 11

Chap. 15

Page 2: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Outline-1

• 15.1 Interpolation– 15.1.1 Linear Interpolation– 15.1.2 Cubic Spline Interpolation– 15.1.3 Extrapolation

• 15.2 Curve Fitting– 15.2.1 Linear Regression– 15.2.2 Polynomial Regression

Page 3: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Outline-2

• 15.3 Numerical Integration– 15.3.1 Determination of the Complete Integral– 15.3.2 Continuous Integration Problems

• 15.4 Numerical Differentiation– 15.4.1 Difference Expressions– 15.4.2 MATLAB Implementation

Page 4: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Interpolation

• We have at least two points with independent (think, horizontal axis) and dependent (think, vertical axis) components.

• We have another independent component,whose value is in between the smallest and the greatest of the previous independent components.

• We would like to know the corresponding dependent component.

Page 5: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Methods of Estimating the Result of Interpolation

• Linear• Piecewise-linear• Polynomial • Cubic Spline• Interpolation via Gaussian Processes• Other

Page 6: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Quality Considerations

• Accuracy of interpolated value• Amount of work to obtain an interpolated

value• Amount of initial data needed• …

Page 7: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Example Interpolation

• From http://en.wikipedia.org/wiki/Interpolation

Page 8: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Linear Interpolation

• If the initial points all lie on a line (y = mx + b) then use the equation of the line on newX.

• If there are only two initial points, they define a line.

Page 9: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Piecewise Linear

• Use adjacent pairs of initial points to define lines.

• With the point whose value is to be estimated by interpolation, choose the relevant defined line. Y = m(i) x(i) + b(i).

Page 10: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Polynomial

• If we had a polynomial for y in terms of x, – then, with an x to be interpolated, – we would evaluate the polynomial

• So, what’s left is to obtain a polynomial– For n points, there is one polynomial of degree n-1

that touches all of the points– Can be computationally complex to find it

Page 11: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Spline

• Piecewise – polynomial– Low degree polynomials for intervals

between furnished points– Connection from one interval to the next smooth

• Smaller error than linear interpolation (if linear is not perfect)

• Less work than perfect polynomial (if “real” polynomial is of higher degree than that of spline)

Page 12: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Cubic Spline

• The curve between each pair of points is a third-degree polynomial. There is a parameter t that varies from 0 to 1, and the distance from the starting point to the ending point goes from 0 to 100%.

• X = at 3 + b t 2 + c t + d• Y = et 3 + f t 2 + g t + h• So, need to know the values of a, b, …h

Page 13: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

How to … Linear

• X = [ the independent axis values]• Y = [the dependent axis values]• Only works when X, Y pairs are all on the same

line.• answer_y = interp1(x,y, target_x)

Page 14: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

How to…Piecewise Linear

• X = [ the independent axis values]• Y = [the dependent axis values]• whichInterval = 0;• for i = 1:length(X)• if target_x > X(i)

– whichInterval = whichInterval+1;• end• Interval_x = [ x(whichInterval) x(whichInterval+1)]• Interval_y = [ y(whichInterval) y(whichInterval+1)]• answer_y = interp1(Interval_x, Interval_y, target_x)

Page 15: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

How to…Cubic Spline

• answer_y = spline(x, y, target_x)

Page 16: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

15.1.3 Extrapolation

• This is where one tries to guess beyond the limits of the available data.

• Be aware that error in this process can be significant.

• Cannot use interp1• Can use spline

Page 17: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

15.2 Curve Fitting

• In polynomial interpolation we observed that for n points there is a polynomial of degree n-1 that includes all of the points.

• Suppose we believe that the points are not perfect, as in measured data.

• Suppose we choose a degree of polynomial less than n, and then choose some coefficients for the polynomial, such that the difference between the measured data and the resulting polynomial is “minimized”.

Page 18: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Polynomial Curve Fitting-1

• Degree of polynomial is the largest exponent• Polynomial in x:– f(x) = ax n + bx n-1 + cx n-2 … dx + e – has degree n– has n+1 coefficients– so, with n+2 choices, the polynomial is

determined• Choose/guess the degree from information

about the problem.

Page 19: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Polynomial Curve Fitting -2

• What constitutes a “good” fit, for a particular choice of degree, n?

• Least squares is one kind of measure of how good the fit is. If for each point x,y, we take the difference between the y of the point and the y of the polynomial curve, square the difference (yields a positive number) and add up all those squared differences, we get a measure of goodness of fit called least squares.

Page 20: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Linear Regression

• If in curve fitting, we choose the degree n to be 1, we are fitting by linear regression.

• To get MATLAB to determine the coefficients, we call polyfit with n=1, which looks like

polyfit(x,y,1)• Polyfit returns the coefficients in linearly

decreasing order of power of the variable in the polynomial. If n = 1, f(x)= ax+b, and the first coefficient returned is a, and the second is b.

Page 21: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Polynomial Regression

• Suppose we choose a degree of polynomial, called n, > 1.

• There will be more coefficients.• For example, n=3, the number of coefficients

will be 4.• coef = polyfit (x, y, n)• Now that coef is set, it can be used.• New_y = polyval(coef, new_y)

Page 22: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

More degrees of freedom yields closer fit

function viewFits()x = 0:5;fine_x = 0:.1:5;y = [0 20 60 68 77 110];for order = 2:5 y2=polyval(polyfit(x,y,order), fine_x); subplot(2,2,order-1) plot (x,y,'o', fine_x, y2) axis([-1 7 -20 120]) ttl = sprintf('Degree %d Polynomial Fit',... order); title(ttl) xlabel('Time (sec)') ylabel('Temperature (degrees F)')end

Page 23: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

15.3 Numerical Integration

• Using piecewise linear technique– Add areas from trapezoidal rule– KT = (b – a)/2n)(f(a) + 2f(x1) + 2f(x2)… + 2f(xn-1) + f(b))

• Using piecewise parabolic technique– Add areas from Simpson’s rule– KS = (b – a)/2n)(1/3)(f(a) + 4f(x1) + 2f(x2) + 4f(x3)+ … +

2f(xn-2) + 4f(xn-1) + f(b))

Page 24: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Numerical Integration Code

function K = trapezoid(v,a,b)%h = trapezoid(v,a,b)K=(b-a)*(v(1)+v(end)+... 2*sum(v(2:end-1)))/(2*(length(v)-1));

function K = simpson(v,a,b)%h = simpson(v,a,b)K=(b-a)*(v(1)+v(end)+… 4*sum(v(2:2:end-1))+… 2*sum(v(3:2:end-2)))/ (3*length(v)-1));

Page 25: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Numerical Differentiation

• Forward difference:• f’(xk) = (f(x k ) – f( x k-1 ))/(x k – x k-1)• Backward difference:• f’(xk) = (f(x k+1 ) – f( x k ))/(x k+1 – x k)• Centered difference:• f’(xk) = (f(x k+1 ) – f( x k-1 ))/(xk+1 – x k-1)

• Both forward and backward differences are first-order. They take differences of adjacent terms.

Page 26: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

MATLAB function diff

• function diff returns a vector of differences between adjacent terms

• v(2)-v(1), v(3)-v(2), …

• Which of forward, backward is this? Could it be used for either one?

• Suppose we wanted centered. Could we use diff? Would we need to prepare by a step?

Page 27: Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic Spline Interpolation – 15.1.3 Extrapolation 15.2 Curve

Forward Difference Code

x=-7:0.1:9;f = polyval([0.0333,-0.3,-1.3333,16,0,-187.2,0],x);plot(x,f)hold ondf = diff(f)./diff(x)plot(x(2:end),df, ‘g’)gridlegend({‘f(x)’,’f ‘’(x)’})