cmps1371 introduction to computing for engineers numerical methods

40
CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Upload: dorthy-bradford

Post on 16-Jan-2016

259 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

CMPS1371Introduction to

Computing for Engineers

NUMERICAL METHODS

Page 2: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Interpolation

When you take data, how do you predict what other data points might be?

Two techniques are : Linear Interpolation Cubic Spline Interpolation

Page 3: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Linear Interpolation

Assume the function between two points is a straight line

0 1 2 3 4 5 60

2

4

6

8

10

12A Data Plot

x-axis

y-ax

isWhat is the corresponding value of y for this x?

Page 4: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

How do you find a point in between?

X=2, Y=?

Linear Interpolation – Connect the points with a straight line to find y

Page 5: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

MATLAB Code

interp1 is the MATLAB function for linear interpolation

First define an array of x and y Now define a new x array, that includes the x

values for which you want to find y values new_y = interp1(x,y,x_new)

Page 6: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

-1 0 1 2 3 4 5 6

0

2

4

6

8

10

12

14

16Measured Data

x-axis

y-ax

is

Page 7: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS
Page 8: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

-1 0 1 2 3 4 5 6

0

2

4

6

8

10

12

14

16

x-axis

y-ax

is

Measured and Interpolated Data

Both measured data points and interpolated data were plotted on the same graph. The original points were modified in the interactive plotting function to make them solid circles.

Page 9: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Cubic Spline

A cubic spline creates a smooth curve, using a third degree polynomial

We can get an improved estimate by using the spline interpolation technique

Page 10: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS
Page 11: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

-1 0 1 2 3 4 5 6

0

2

4

6

8

10

12

14

16Cubic Spline Interpolation

x-axis

y-ax

is

Cubic Spline Interpolation. The data points on the smooth curve were calculated. The data points on the straight line segments were measured. Note that every measured point also falls on the curved line.

Page 12: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Curve Fitting

There is scatter in all collected data We can estimate the equation that represents

the data by “eyeballing” a graph There will be points that do not fall on the line

we estimate

Page 13: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

This line is just a “best guess”

Page 14: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Least Squares

Finds the “best fit” straight line Minimizes the amount each point is away

from the line It’s possible none of the points will fall on the

line Linear Regression

Page 15: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Polynomial Regression

Linear Regression finds a straight line, which is a first order polynomial

If the data doesn’t represent a straight line, a polynomial of higher order may be a better fit

Page 16: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

polyfit and polyval

polyfit finds the coefficients of a polynomial representing the data

polyval uses those coefficients to find new values of y, that correspond to the known values of x

Page 17: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Coefficients of the first order polynomial describing the best fit line

2.9143* 14.2857y x

Page 18: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Linear Regression (First Order)

Evaluate how close a fit you’ve achieved by taking the difference between the measured and calculated points, and adding them up

Page 19: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Second Order Fit

Page 20: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

A fifth order polynomial gives a perfect fit to 6 points

Page 21: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Using the Interactive Curve Fitting Tools

MATLAB 7 includes new interactive plotting tools.

They allow you to annotate your plots, without using the command window.

They include basic curve fitting, more complicated curve fitting statistical tools

Page 22: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Use the curve fitting tools…

Create a graph Making sure that the figure window is the

active window select Tools-> Basic Fitting The basic fitting window will open on top of the

plot

Page 23: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS
Page 24: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

-1 0 1 2 3 4 5 6 7-20

0

20

40

60

80

100

120Some Data

Time, seconds

Tem

pera

ture

, de

gree

s F

y = 21*x + 3.8

y = 1.1*x3 - 9.3*x2 + 41*x - 3.1

data 1

linear cubic

Plot generated using the Basic Fitting Window

Page 25: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

-10

-5

0

5

10

residuals

-1 0 1 2 3 4 5 6 7-20

0

20

40

60

80

100

120Some Data

Time,secondsT

empe

ratu

re,

degr

ees

F

y = 21*x + 3.8

y = 1.1*x3 - 9.3*x2 + 41*x - 3.1

Residuals are the difference between the actual and calculated data points

Page 26: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Basic Fitting Window

Page 27: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Data Statistics Window

You can also access the data statistics window from the figure menu bar.

Select

Tools->Data Statistics

from the figure window.

This window allows you to calculate statistical functions interactively, such as mean and standard deviation, based on the data in the figure, and allows you to save the results to the workspace.

Page 28: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Differences and Numerical Differentiation

We can use symbolic differentiation to find the equation of the slope of a line

The diff function is easy to understand, even if you haven’t taken Calculus

It just calculates the difference between the points in an array

Page 29: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

-1 0 1 2 3 4 5 60

2

4

6

8

10

12

14

16Sample Data

x-axis

y-ax

is

2 1

2 1

y yslope

x x

3 2

3 2

y yslope

x x

4 3

4 3

y yslope

x x

5 4

5 4

y yslope

x x

6 5

6 5

y yslope

x x

The derivative of a data set can be approximated by finding the slope of a straight line connecting each data point

Page 30: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

The slope is an approximation of the derivative – in this case based on data measurements

Page 31: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Approximating Derivatives when you know the function

If we know how y changes with x, we could create a set of ordered pairs for any number of x values. The more values of x and y, the better the approximation of the slope

Page 32: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

-2 -1 0 1 20

1

2

3

4y=x2

y-ax

is

(a)-2 -1 0 1 2

-4

-2

0

2

4Slope of y=x2

(b)

-2 -1 0 1 20

1

2

3

4y=x2

y-ax

is

(c)-2 -1 0 1 2

-4

-2

0

2

4Slope of y=x2

(d)

-2 -1 0 1 20

1

2

3

4y=x2

y-ax

is

x-axis(e)

-2 -1 0 1 2-4

-2

0

2

4Slope of y=x2

x-axis(f)

The slope of a function is approximated more accurately, when more points are used to model the function

Page 33: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Numerical Differentiation

The derivative of a function is equal to the rate of change of f(x) with respect to x

Differentiating f(x)

Page 34: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Graph the derivative

Consider the function f(x) = 0.0333x6 – 0.3x5 – 1.3333x4 + 16x3 – 187.2x

Page 35: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Slope of f(x) and f’(x)

Page 36: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Critical Points

We can compute the compute the location of the functions critical points (points of zero slope)

>> product = df(1:end-1) .* df)2:end);

>> critical = xd(find(product < 0));

critical =

-5.2000 -1.9000 2.6000 5.8000 6.2000

Page 37: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Numerical Integration

MATLAB handles numerical integration with two different quadrature functions

quad quadl

Page 38: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

0 2 4 60

5

10

15

An integral can be approximated by the area under a curve

y-ax

is

x-axis0 2 4 6

0

5

10

15A Trapazoid rule approximation

y-ax

isx-axis

The area under a curve can be approximated using the trapezoid rule.

An integral is often thought of as the area under a curve

Page 39: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

Quadrature functions

quad uses adaptive Simpson quadrature quadl uses adaptive Lobatto quadrature

Both functions require the user to enter a function in the first field. called out explicitly as a character string defined in an M-file anonymous function.

The last two fields in the function define the limits of integration

Page 40: CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

x-axis

y-a

xis

Evaluation of Data using the Trapazoid Rule

data

calculated midpoints