Title: Curve Fitting and Interpolation2
1 Curve Fitting and Interpolation-2
2APPROXIMATION
OF DATA
Least Squares Method of Curve Fitting
3Data Approximation
- 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.
4Multi-Valued Data Points
y
x
5Fitting 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.
6Linear Least Squares Algorithm
- Choice of fitting function (linear)
- Errors between function and data points
- Sum of the squares of the errors
7Linear Least Squares Algorithm-cont.
- Our goal is to determine the values of a and b
that will minimize z, the sum of the squares of
the errors.
To find the minimum value for z, Matlab uses the
same technique that we would use analytically
(i.e., setting the derivative of z to zero and
solving for a and b)
8Plot of Linear Fit using Matlab
70
60
50
40
30
20
10
0
0
1
2
3
4
5
6
7
8
9
10
9Plot of Linear Fit using Matlab
- What about
- this point?
- Is it really a
- good data point?
- What do you know about the data?
70
60
50
40
30
20
10
0
0
1
2
3
4
5
6
7
8
9
10
10A Linear Fit Ignoring One Data Point
70
60
50
40
30
20
- But what if we
- "convicted" the
- wrong data point?
10
0
0
1
2
3
4
5
6
7
8
9
10
11Second 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
12Choosing the Right Polynomial
- The degree of the correct approximating
function depends on the type of data being
analyzed.
When a certain behavior is expected, 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.
13Data Approximation with Matlabs polyfit
- The steps used in Matlab for approximating
data are very similar to those used for
interpolation
The degree of the approximating function depends
on the type of fit desired, rather than on the
number of data points.
14Example Ohms Law
- This graph shows the amount of current through an
unknown resistor, plotted against the voltage
applied to the resistor. - Knowing Ohms Law (I V/R), find the resistance,
and plot an approximating function.
15Example-cont.
- Ohms Law shows a linear dependence between
current and voltage, so we will use a first
degree fit. - Once again, there is what appears to be an
"outsider" in the data set (0.08 A at 20 V) that
is most likely a data collection error, and will
be ignored.
30
16IN MATLAB
- Rewrite I V/R as I (1/R)V, which is a
linear relationship between V and I, with
coefficient 1/R.
Do not include data point V20 xdata 0 5 10
15 25 ydata 0.001 0.881 2.1637 3.1827
4.961 degree 1 Linear
relationship coef polyfit(xdata, ydata,
degree) xx -5 0.5 30 Range for
plotting yy polyval(coef, xx) plot(xdata,
ydata, 'o', xx, yy) resistance 1 / coef(1)
Output the answer
17IN MATLAB-The Plot
plot(xdata, ydata, 'o', xx, yy) resistance 1 /
coef(1) Output the answer
- The output from Matlab is
- resistance
- 4.9515
- Thus, the resistance is 4.95 O.
18Exponential 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.
19Relationship between an Exponential 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.
20Population Example-cont.
- 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.
21Population Example-cont.
- 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.
22Relationship between an Exponential Linear
Form
- 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.
23Using Matlab to Approximate Exponential Data
An Example
- A radiation detector is used to record counts of
the decay of an unknown material according to - A(t)A0 e-?t
- where A0 is the count rate at t 0 and ? is the
decay constant. - Using data taken at one-minute intervals, find A0
and ? and plot a function in Matlab that
approximates the decay of the material.
24Example-Cont.
- A(t)A0 e-?t
- ln(A(t)) ln(A0) -?t
- t 010
- cr 2095 1252 766 452 266 162 98 70 39 21 12
- crlog log(cr)
- coefs polyfit(t, crlog, 1) find ln(a) and
b - A0 exp(coefs(2)) coefs(2)ln(a) so
aexp(coefs(2)) - lambda -coefs(1) b -lambda so lambda -b
- tt 00.110 the range over which to
approximate - approxcr A0exp(-lambdatt)
- plot(t,cr,'o', tt,approxcr)
25Example-Cont.
- The plot of the exponential approximation
26Interpolation vs. Approximation
- Splines are used
- To produce smooth surfaces, shapes, and motions.
- When interpolation best used
- When you wish to find an unknown intermediate
point within a dataset with well-defined
behavior. - When a relatively small data set exists.
- When is approximation best used
- When evaluating fluctuating data.
- To create a mathematical model of a process.
27Fundamental to Engineering Practice and Research
- Analysis of data in engineering research involves
relating data to theory or a model. - This starts with fitting the data to
mathematical functions so that relationships can
be explored. - The next step is to compare the approximating
(data) function with the theoretical function. - The theoretical function is often the solution to
a set of differential equations. - For a given theory, when the data function is
similar to the theoretical function, we say that
we have a good theory or model of the phenomenon.
- If the functions are not similar, the theoretical
function may need to be modified, or completely
new equations may be needed.