Class Prep - PowerPoint PPT Presentation

1 / 51
About This Presentation
Title:

Class Prep

Description:

It has important applications in areas such as image processing (smoother jpg's) ... Plot the original points as black circles, 'nearest' values with dotted line in ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 52
Provided by: davidh67
Category:
Tags: area | circle | class | of | prep

less

Transcript and Presenter's Notes

Title: Class Prep


1
Class Prep
  • Bring to Class
  • In-Class Exercises
  • Paper for Classroom Printer
  • Copy Files to MATLAB Directory
  • Run MATLAB

2
Copy Files
  • While we are waiting for class to start, copy
    beam1.m, beam2.m, beam3.m, interp2_Demo.m,
    PolynomialRoots.m from week 10 in the course
    folder to your username subfolder in My Documents
  • Run MATLAB, set correct Current Directory

3
Week 10-a(15.1-15.2)
  • Interpolation
  • Curve Fitting
  • Roots of Polynomials

4
The Interpolation Problem
5
Interpolated Data
6
Interpolation
  • Interpolation is a process for estimating values
    that lie between known data points. It has
    important applications in areas such as image
    processing (smoother jpg's).
  • Also recall shading interp of surf plots
  • MATLAB provides a number of interpolation
    techniques that let you balance the smoothness of
    the data fit vs. speed of execution and memory
    usage.

7
Cantilever Beam Deflection Model
8
Hands-On DEMO Linear Interpolation
  • Interpolate missing values in Cantilever Beam
    Deflection
  • See beam1.m then beam2.m

9
Hands-On DEMO interp1 Function(that's a one not
an L)
  • Add the following to beam2 (uncomment)
  • interp1(x,y,400)
  • (NOTE x, y variables are force and deflection)
  • interp1 performs one-dimensional interpolation,
    an important operation for data analysis and
    curve fitting.

10
Cubic Spline Interpolation
11
Polynomial Interpolation
  • interp1 uses polynomial techniques, fitting the
    supplied data with polynomial functions between
    data points and evaluating the appropriate
    function at the desired interpolation points.
  • Its most general form is
  •  yi interp1(x, y, xi, method)

12
Polynomial Interpolation Methods
  • method is an optional string specifying an
    interpolation method
  • Nearest neighbor interpolation (method
    'nearest'). This method sets the value of an
    interpolated point to the value of the nearest
    existing data point.
  • Linear interpolation (method 'linear'). This
    method fits a different linear function between
    each pair of existing data points, and returns
    the value of the relevant function at the points
    specified by xi. This is the default method for
    the interp1 function.
  • Cubic-Spline interpolation (method 'spline').
    This method fits a different cubic function
    between each pair of existing data points, and
    uses the spline function to perform cubic spline
    interpolation at the data points.

13
Hands-On DEMO Comparing Methods
  • This example compares two-dimensional
    interpolation methods on a matrix of data.
  • As opposed to vectors in previous examples.
  • Generate the peaks function at low resolution.
  •     gtgt x,y meshgrid(-313)
  •     gtgt z peaks(x,y)
  •     gtgt surf(x,y,z)

14
Example
15
DEMO interp2 for 2-Dimensional
Interpolation(see interp2_Demo.m)
  • Generate a finer mesh for interpolation.
  • xi,yi meshgrid(-30.253)  
  • Interpolate using nearest neighbor interpolation.
  • zi1 interp2(x,y,z,xi,yi,'nearest')  
  • Interpolate using bi-linear interpolation zi2
    interp2(x,y,z,xi,yi,'bilinear')
  • Interpolate using bi-cubic interpolation.zi3
    interp2(x,y,z,xi,yi,'bicubic')  
  • Compare the surface plots for the different
    interpolation methods.

16
Hands-On DEMO Surface Plots
17
Hands-On DEMO Contour Plots
18
Data Approximation with a Function
  • Data points (xi, yi) can be approximated by a
    function y f(x) such that the function passes
    close to the data points but does not
    necessarily pass through them.
  • Data fitting is necessary to model data with
    fluctuations such as experimental measurements.
  • The most common form of data fitting is the
    least squares method.

19
Fitting Polynomials to Data
  • Fitting analytical curves to experimental data is
    a common task in engineering.
  • The simplest is linear interpolation (straight
    lines between data points). Polynomials are
    often used to define curves, and there are
    several options
  • Fit curve through all data points
    (polynomialspline interpolation),
  • Fit curve through some data points,
  • Fit curve as close to points as possible
    (polynomial regression, also called least square
    fitting)

polynomial interpolation
polynomial regression
20
Multi-Valued Data Points

21
Fitting Data with a Linear Function
y

x
Using the least squares algorithm, ensure that
all the data points fall close to the straight
line/function.
22
Linear Curve Estimate
23
Linear Curve Fit
24
Higher-Order Polynomials
25
Linear Least Squares Algorithm
  • Data points
  • Interpolating function (linear)
  • Errors between function and data points
  • Sum of the squares of the errors
  • Compact notation

26
Why Least Squares?
  • Why not just add distances that approximation is
    from actual measured data points?
  • Eliminates the need for absolute value function
  • Squaring differences makes one point that is3
    off worth more than three points that are 1 off.
  • Consider the case of computer dating
  • 3 questions such as music preferences are off by
    only one point from a potential date less
    important than responses to one question that is
    far off

27
Plot of Linear Fit
70
60
50
40
30
20
10
0
0
1
2
3
4
5
6
7
8
9
10
28
Choosing the Right Polynomial
  • The degree of the correct approximating function
    depends on the type of data being analyzed.
  • When a certain behavior is expected, such as the
    linear response of beam deflection under
    increasing loads, we know what type of function
    to use, and simply have to solve for its
    coefficients.
  • When we dont know what sort of response to
    expect, ensure your data sample size is large
    enough to clearly distinguish which degree is the
    best fit.

29
polyfit Function
  • Polynomial curve fitting
  • coef polyfit(x,y,n)
  • A least-square fit with polynomial of degree n.
  • x and y are data vectors, and coef is vector of
    coefficients of the polynomial,
  • coef an, an-1, , a1, a0

30
Hands-On DEMO Polynomial Curve Fitting
  • x and y are vectors containing the x and y data
    to be fitted, and n is the order of the
    polynomial to return. For example, consider the
    x-y test data.
  •     x 1 2 3 4 5
  • y 5.5 43.1 128 290.7 498.4
  • A third order (n3) polynomial that approximately
    fits the data is
  • p polyfit(x,y,3)
  • p -0.1917 31.5821 -60.3262 35.3400
  • In other words, the polynomial function would be
  • Y -0.1917X3 31.5821X2 -60.3262X 35.34

31
Polynomial Curve Fitting, Graph
32
Hands-On DEMO Polynomial Evaluation
  • Going the other way, if p(x) x3 - 2x - 5
  • To enter this polynomial into MATLAB, use
  •            gtgt p 1 0 -2 -5
  • The polyval function evaluates a polynomial at a
    specified value or vector.To evaluate p at x
    5, use
  • gtgt polyval(p,5)
  •     ans 110

33
Hands-On DEMO Polynomial Roots(see
PolynomialRoots.m)
  • Draw graph of p 1 0 -9
  • x -100.0110, ypolyval(p,x), plot(x,y), grid
    on
  • Note that the Roots are /- 3. Try r roots(p)
  • The roots function calculates the roots of a
    polynomial.
  • gtgt p 1 0 -2 -5
  • gtgt r roots(p)                  
  • r 2.0946
  • -1.0473 1.1359i
  •      -1.0473 - 1.1359i  
  • By convention, MATLAB stores roots in column
    vectors. The function poly returns the polynomial
    coefficients given the roots.
  • gtgt p2 poly(r)
  • p2   1  8.8818e-16   -2   -5
  • poly and roots are inverse functions, up to
    ordering, scaling, and roundoff error.

34
Hands-On DEMO Cantilever Beam Deflection(see
beam3.m)
  • Uses polyfit() to fit a straight line.
  • Hold the plot and add the fitted line to your
    graph.
  • Note that with we can now extrapolate as well as
    interpolate
  • The program extracts the coefficients for the
    various powers of x, and calculates the function
  • The program also uses polyval to evaluate the
    polynomial directly

35
beam3 Results
36
Hands-On DEMO Basic Curve-Fitting Tools
  • Given x05, y0,1,60,40,41,47.
  • Plot the data, then from within Figure, select
    Tools ? Basic Fitting
  • Try fitting polynomials of various degrees...

37
Solution
38
Curve Fitting First thru Fourth Degree
  • Notice that the fit looks better the higher the
    order you can make it go through more points.
  • Use the fitted curves to estimate y at x 10.
    Which order polynomial do you trust more out at x
    10? Why?

39
Interpolation vs. Approximation
  • INTERPOLATION
  • When you wish to find an unknown intermediate
    point within a dataset with well-defined
    behavior.
  • When a relatively small data set exists.
  • APPROXIMATION
  • When evaluating fluctuating data.
  • To create a mathematical model of a process.
  • Deciding between interpolation and approximation
    depends on the type of data, not on the data
    values themselves.
  • Recall that, by definition, multi-valued data
    points cannot be interpolated.

40
Homework 10a Spacecraft Accelerometer(Problem
8, page 417)
  • NEVER COLLECTED
  • The guidance and control system for a spacecraft
    often uses a sensor called an accelerometer,
    which is an electromechanical device that
    produces an output voltage proportional to the
    applied acceleration. Assume that the experiment
    has yielded the data on the exercise sheet.
  • Determine the linear equation that best fits this
    set of data. Plot the data points and the linear
    equation.
  • Determine the sum of squares of the distances of
    these points from the line of best fit determined
    above.
  • Compare the error sum for linear fit (above) with
    the same error sum computed from the best
    quadratic fit. What do these sums tell you about
    the two models for the data?

41
In-Class Exercise 10a Interpolation (interp1)
  • Generate f(x) x2 for x -3 -1 0 2 5 6
  • Compute and plot the linear and cubic-spline
    interpolation of the data points over the range
    -30.056
  • Compute the value of f(4) using linear
    interpolation and cubic-spline interpolation.
    What are the respective errors when the answer is
    compared to the actual value or f(4) 42?
  • Plot the original points as black circles,
    'nearest' values with dotted line in red,
    'linear' with dashed line in blue, and 'spline'
    with a solid line in green

42
END
43
EXTRA MATERIAL
44
Bad Data
  • What about
  • this point?
  • Is it really a
  • good data point?
  • What do you know about the data?
  • Is it monotonic?

70
60
50
40
30
20
10
0
0
1
2
3
4
5
6
7
8
9
10
45
A linear fit ignoring one data point
70
60
50
40
30
20
10
0
0
1
2
3
4
5
6
7
8
9
10
46
Second degree polynomial fit, ignoring a
different data point
  • Ignoring a different data point allows us to
    approximate the data pretty well with a second
    degree polynomial.

70
60
50
40
30
20
10
0
0
1
2
3
4
5
6
7
8
9
10
47
Exponential fit
  • Not all experimental data can be approximated
    with polynomial functions.
  • Exponential data can be fit using the least
    squares method by first converting the data to a
    linear form.

48
Relationship between exponential and linear form
  • This graph shows a population that doubles every
    year. Notice that for the first fifteen years,
    growth is almost imperceptible at this scale. It
    would be difficult to approximate this raw data.

49
Relationship between exponential and linear form
  • What if we plot the same data with the y-axis
    against a logarithmic scale?
  • Notice that the change in population is not
    simply perceptible, but is clearly linear.
  • If we can transform the numeric exponential data,
    it would be simple to approximate with the
  • least-squares method.

50
Relationship between exponential and linear form
  • This graph shows what happens if we take the
    natural logarithm of each y value.
  • Notice that the shape of the graph did not change
    from the last example, only the scaling of the
    y-axis.

51
Relationship between Exponential and Linear
  • An exponential function,
  • y aebx
  • can be rewritten as a linear polynomial by
    taking the natural logarithm of each side
  • ln y ln a bx
  • By finding ln yi for each point in a data set, we
    can solve for a and b using the least squares
    method.
Write a Comment
User Comments (0)
About PowerShow.com