Lecture 11 Chap. 15. Outline-1 15.1 Interpolation – 15.1.1 Linear Interpolation – 15.1.2 Cubic...

Preview:

Citation preview

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 Fitting– 15.2.1 Linear Regression– 15.2.2 Polynomial Regression

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

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.

Methods of Estimating the Result of Interpolation

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

Quality Considerations

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

value• Amount of initial data needed• …

Example Interpolation

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

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.

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).

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

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)

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

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)

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)

How to…Cubic Spline

• answer_y = spline(x, y, target_x)

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

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”.

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.

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.

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.

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)

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

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))

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));

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.

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?

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)’})

Recommended