Numerical Techniques - Dalton State College

advertisement
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
Linear Interpolation
 Assume the
10
8
y-axis
function
between
two points
is a straight
line
A Data Plot
12
What is the
corresponding
value of y for
this x?
6
4
2
0
0
1
2
3
x-axis
4
5
6
How do you find a point
in between?
X=2, Y=?
Linear Interpolation – Connect the points with a
straight line to find y
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)
Measured Data
16
14
12
y-axis
10
8
6
4
2
0
-1
0
1
2
x-axis
3
4
5
6
Measured and Interpolated Data
16
14
12
y-axis
10
8
6
4
2
0
-1
0
1
2
3
4
5
6
x-axis
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.
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
Cubic Spline Interpolation
16
14
12
y-axis
10
8
6
4
2
0
-1
0
1
2
3
4
5
6
x-axis
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.
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
This line is just a “best guess”
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
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
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
Coefficients of the
first order
polynomial
describing the best
fit line
y  2.9143* x  14.2857
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
Second Order Fit
A fifth order polynomial gives a perfect fit
to 6 points
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
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
Some Data
120
Temperature, degrees F
100
y = 21*x + 3.8
y = 1.1*x 3 - 9.3*x 2 + 41*x - 3.1
80
60
data 1
linear
cubic
40
20
0
-20
-1
0
1
2
3
4
Time, seconds
5
6
7
Plot generated using the Basic Fitting Window
Some Data
120
Temperature, degrees F
100
80
y = 21*x + 3.8
y = 1.1*x 3 - 9.3*x 2 + 41*x - 3.1
60
40
20
0
-20
-1
0
1
2
3
Time,seconds
4
5
6
7
residuals
10
5
0
-5
-10
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5
Residuals are the difference between the actual and calculated data
points
Basic Fitting Window
You can also access the
data statistics window
from the figure menu bar.
Select
Tools->Data Statistics
from the figure window.
Data Statistics 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.
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
Sample Data
16
14
slope 
y2  y1
x2  x1
12
slope 
y-axis
10
8
slope 
y3  y2
x3  x2
y4  y3
x4  x3
6
slope 
4
y5  y4
x5  x4
slope 
2
0
-1
0
1
2
3
4
5
y6  y5
x6  x5
6
x-axis
The derivative of a data set can be approximated by finding the slope of a
straight line connecting each data point
The slope is an approximation of
the derivative – in this case based
on data measurements
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
y-axis
y=x 2
Slope of y=x 2
4
4
3
2
2
0
1
-2
0
-2
-1
0
(a)
1
2
-4
-2
-1
y-axis
y=x 2
4
3
2
2
0
1
-2
-1
0
(c)
1
2
-4
-2
-1
y-axis
y=x 2
4
3
2
2
0
1
-2
-1
0
x-axis
(e)
2
0
(d)
1
The slope of a
function is
approximated
more accurately,
when more
points are used
to model the
function
2
Slope of y=x 2
4
0
-2
1
Slope of y=x 2
4
0
-2
0
(b)
1
2
-4
-2
-1
0
x-axis
(f)
1
2
Numerical Differentiation
 The derivative of a function is equal to the
rate of change of f(x) with respect to x
Differentiating f(x)
Graph the derivative
 Consider the function

f(x) = 0.0333x6 – 0.3x5 – 1.3333x4 + 16x3 – 187.2x
Slope of f(x) and f’(x)
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
Numerical Integration
 MATLAB handles numerical integration with
two different quadrature functions
 quad
 quadl
An integral is often thought of as
the area under a curve
An integral can be approximated
by the area under a curve
A Trapazoid rule approximation
10
10
y-axis
15
y-axis
15
5
0
5
0
2
4
x-axis
6
0
0
2
4
x-axis
The area under a curve can be approximated
using the trapezoid rule.
6
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
Evaluation of Data using the Trapazoid Rule
1
0.9
data
calculated midpoints
0.8
0.7
y-axis
0.6
0.5
0.4
0.3
0.2
0.1
0
0
0.1
0.2
0.3
0.4
0.5
x-axis
0.6
0.7
0.8
0.9
1
Download