1, finish the question with all the steps, the file I already provided below.
2, when you finish the MATLAB code, please explain why you did this step with %% in MATLAB so that I can understand why you did that step.
3, important! Please finish on your own.4
4, pay attention to the ‘Hint 1 to 3’ in the file, this is the way you need to use.
5, you must use a quadratic spline polynomial, and you are not allowed to use built-in Matlab commands for interpolation or polynomial generation.
6, do a bonus if you can.
7, here is an example I provide to you. ”topic_3 page 51-67, problem 6 uses the similar method.
CHEE 3602, Topic 3: Interpolating polynomials
Fall 2023
Lab 6: Spline interpolation
Background
As discussed in the introductory lecture to interpolation, the T-xy diagram represents an
example of a function where it is relatively easy to calculate the fraction of a compound
(such as benzene) in the liquid or vapour phase given a temperature, but hard to do the
reverse (calculating the dew or bubble point of a mixture given only the liquid or vapour
fraction). One way to deal with this challenge is to calculate a set of liquid/vapour fraction
values over a range of temperatures, and then use an interpolating function to generate a
polynomial that expresses liquid/vapour fraction as a function of temperature.
In this lab, we will focus on just the liquid fraction of a binary benzene/toluene mixture.
Recall the process for calculating a liquid fraction from temperature:
1. Given an input pressure value ๐๐ก๐๐ก , calculate the boiling point temperature for pure
components
โ use Antoineโs law log10 (๐โ ) = ๐ด โ ๐ต/(๐ + ๐ถ)
2. With the overall temperature range established, choose a set of temperature values
within this range and calculate the vapour pressure of the two components for each
temperature
โ once again, use Antoineโs law log10 (๐โ ) = ๐ด โ ๐ต/(๐ + ๐ถ)
โ but reverse it to calculate ๐โ from ๐
3. Then, calculate liquid fraction benzene (๐ฅ๐ต ) based on these vapour pressures
โ
โ
โ use Raoultโs and Daltonโs law, i.e., ๐๐ก๐๐ก = ๐๐ต
๐ฅ๐ต + ๐๐
๐ฅ๐
โ
โ
โ recall that the mixture is binary, so ๐ฅ๐ = 1 โ ๐ฅ๐ต and ๐๐ก๐๐ก = ๐๐ต
๐ฅ๐ต + ๐๐
(1 โ ๐ฅ๐ต )
โ and remember that ๐๐ก๐๐ก is given as an input
4. The result is a set of temperature (๐) and liquid fraction benzene (๐ฅ๐ต ) values, which
can be used to set up an interpolating function with ๐ as the ๐ฆ values and ๐ฅ๐ต as the x
values.
This labโs task will be to estimate the bubble point temperature of a benzene/toluene mixture using a quadratic interpolating spline. Given a set of (๐ฅ๐ , ๐๐ ) points, recall the quadratic
spline fit process:
Page 1 of 4
CHEE 3602, Topic 3: Interpolating polynomials
Fall 2023
1. Given ๐ points, there should be ๐ โ 1 total spline polynomials
2. ๐๐ is set to the value of ๐๐
3. ๐๐ is solved from the following system of linear equations:
2( ๐2 โ ๐1 )
โก1
โข0
โข
โข0
โข
โขโฎ
โข0
โข
โฃ0
4. ๐๐ is solved from
1
1
0
โฎ
0
0
0
1
1
โฎ
0
0
… 0
… 0
… 0
โฑ โฎ
… 1
… 0
โก
โค
โ1
0โคโก ๐1 โค โข 2( ๐3 โ ๐2 ) โฅ
โข
โฅ
โ2
โฅ
0 โฅโข ๐ 2 โฅ โข
โฅโข
โฅ โข 2( ๐4 โ ๐3 ) โฅ
0 โฅโข ๐ 3 โฅ โข
โฅ
โ3
โฅโข
โฅ=โข
โฅ
โฎ โฅโข โฎ โฅ โข
โฅ
โฎ
โข๐
โฅ โข 2( ๐ โ ๐ ) โฅ
1โฅ
โฅโข ๐โ2 โฅ โข ๐โ1 ๐โ2 โฅ
โฅ
โ๐โ2
1โฆโฃ๐๐โ1 โฆ โข
โข ๐๐โ ๐๐โ1 โฅ
โฃ
โฆ
โ๐โ1
๐๐+1 โ๐๐
for [๐ = 1, ๐ โ 2]
2โ๐
5. ๐๐โ1 is set to 0
Once all polynomial terms are calculated, ๐(๐ฅ) is found by first, identifying which polynomial (๐) ๐ฅ belongs to, and then plugging ๐ฅ into the polynomial equation of ๐(๐ฅ) =
๐๐ + ๐๐ (๐ฅ โ ๐ฅ๐ ) + ๐๐ (๐ฅ โ ๐ฅ๐ )2 .
Task
Write a function to estimate the bubble point temperature of a benzene/toluene mixture
given total pressure, liquid fraction benzene, and the number of points that should be
used to build a quadratic interpolating polynomial. To make sure everyone uses the same
values, the spline function data must set the second derivative of the last polynomial to
zero (using pure toluene, i.e., no benzene, as the first point). Use the following data for
Antoineโs equation (with the temperature in Celsius and pressure in mmHg):
Compound
A
B
C
Benzene
6.89272
1203.531
219.888
Toluene
6.95805
1346.773
219.693
You must use a quadratic spline polynomial and you are not allowed to use built-in Matlab
commands for interpolation or polynomial generation.
Page 2 of 4
CHEE 3602, Topic 3: Interpolating polynomials
Fall 2023
Core requirements
Function name
bt_txy
Input
Total pressure (in mmHg), liquid fraction benzene (๐ฅ๐ต ), and the
total number of points used to perform the spline interpolation
(๐)
Output
A single temperature value corresponding to the bubble point of
the mixture
Errors
If pressure input is less than 0, fraction benzene is not a valid fraction, or the total number of points is less than 3
Example
bt_txy(760, 0.5, 4) should give 91.9104
There are several hints for this lab:
Hint 1
You will need to solve a system of linear equations to get all the ๐๐ values.
You do not need to program your own Gauss elimination to do this. Just
use Matlabโs linsolve command. Given a system of equations of the
form ๐ด๐ฅ = ๐, you would use x = linsolve(A, b).
Hint 2
The system of equations that appears in the spline equation only has nonzero values on 2 diagonal lines. You can use Matlabโs diag command to
help construct this matrix. Check the diag help page for more information
Hint 3
It is possible to meet core requirements without any looping whatsoever.
You can use loops if you want, but remember Matlabโs array math.
Bonus requirements
Expand the original bt_txy function to also display a figure plotting the original data as
points and the interpolating spline as a line.
Page 3 of 4
CHEE 3602, Topic 3: Interpolating polynomials
Function name
Fall 2023
bt_txy
Input
Total pressure (in mmHg), liquid fraction benzene (๐ฅ๐ต ), and the
total number of points used to perform the spline interpolation
(๐)
Output
A single temperature value corresponding to the bubble point of
the mixture
Side-effect
Generate a figure plotting the T-xy data as points with the interpolating spline as a line as a pop-up
Errors
Example
If pressure input is less than 0, fraction benzene is not a valid fraction, or the total number of points is less than 3
bt_txy(760, 0.5, 4) should give 91.9104 as well as pop up a
plot similar to the example below
Submission checklist
Use the following checklist to help ensure you are meeting the core lab requirements:
You are submitting a single file called bt_txy.m which defines a function called bt_txy
Your function returns a value:
bt_txy(760, .5, 4)/2 should not cause problems
Your function should work correctly for any value of ๐ โฅ 3 and liquid fraction 0 โฅ ๐ฅ๐ต โฅ
1
Page 4 of 4
CHEE 3602, Topic 3: Interpolating polynomials
Fall 2023
Lab 6: Spline interpolation
Background
As discussed in the introductory lecture to interpolation, the T-xy diagram represents an
example of a function where it is relatively easy to calculate the fraction of a compound
(such as benzene) in the liquid or vapour phase given a temperature, but hard to do the
reverse (calculating the dew or bubble point of a mixture given only the liquid or vapour
fraction). One way to deal with this challenge is to calculate a set of liquid/vapour fraction
values over a range of temperatures, and then use an interpolating function to generate a
polynomial that expresses liquid/vapour fraction as a function of temperature.
In this lab, we will focus on just the liquid fraction of a binary benzene/toluene mixture.
Recall the process for calculating a liquid fraction from temperature:
1. Given an input pressure value ๐๐ก๐๐ก , calculate the boiling point temperature for pure
components
โ use Antoineโs law log10 (๐โ ) = ๐ด โ ๐ต/(๐ + ๐ถ)
2. With the overall temperature range established, choose a set of temperature values
within this range and calculate the vapour pressure of the two components for each
temperature
โ once again, use Antoineโs law log10 (๐โ ) = ๐ด โ ๐ต/(๐ + ๐ถ)
โ but reverse it to calculate ๐โ from ๐
3. Then, calculate liquid fraction benzene (๐ฅ๐ต ) based on these vapour pressures
โ
โ
โ use Raoultโs and Daltonโs law, i.e., ๐๐ก๐๐ก = ๐๐ต
๐ฅ๐ต + ๐๐
๐ฅ๐
โ
โ
โ recall that the mixture is binary, so ๐ฅ๐ = 1 โ ๐ฅ๐ต and ๐๐ก๐๐ก = ๐๐ต
๐ฅ๐ต + ๐๐
(1 โ ๐ฅ๐ต )
โ and remember that ๐๐ก๐๐ก is given as an input
4. The result is a set of temperature (๐) and liquid fraction benzene (๐ฅ๐ต ) values, which
can be used to set up an interpolating function with ๐ as the ๐ฆ values and ๐ฅ๐ต as the x
values.
This labโs task will be to estimate the bubble point temperature of a benzene/toluene mixture using a quadratic interpolating spline. Given a set of (๐ฅ๐ , ๐๐ ) points, recall the quadratic
spline fit process:
Page 1 of 4
CHEE 3602, Topic 3: Interpolating polynomials
Fall 2023
1. Given ๐ points, there should be ๐ โ 1 total spline polynomials
2. ๐๐ is set to the value of ๐๐
3. ๐๐ is solved from the following system of linear equations:
2( ๐2 โ ๐1 )
โก1
โข0
โข
โข0
โข
โขโฎ
โข0
โข
โฃ0
4. ๐๐ is solved from
1
1
0
โฎ
0
0
0
1
1
โฎ
0
0
… 0
… 0
… 0
โฑ โฎ
… 1
… 0
โก
โค
โ1
0โคโก ๐1 โค โข 2( ๐3 โ ๐2 ) โฅ
โข
โฅ
โ2
โฅ
0 โฅโข ๐ 2 โฅ โข
โฅโข
โฅ โข 2( ๐4 โ ๐3 ) โฅ
0 โฅโข ๐ 3 โฅ โข
โฅ
โ3
โฅโข
โฅ=โข
โฅ
โฎ โฅโข โฎ โฅ โข
โฅ
โฎ
โข๐
โฅ โข 2( ๐ โ ๐ ) โฅ
1โฅ
โฅโข ๐โ2 โฅ โข ๐โ1 ๐โ2 โฅ
โฅ
โ๐โ2
1โฆโฃ๐๐โ1 โฆ โข
โข ๐๐โ ๐๐โ1 โฅ
โฃ
โฆ
โ๐โ1
๐๐+1 โ๐๐
for [๐ = 1, ๐ โ 2]
2โ๐
5. ๐๐โ1 is set to 0
Once all polynomial terms are calculated, ๐(๐ฅ) is found by first, identifying which polynomial (๐) ๐ฅ belongs to, and then plugging ๐ฅ into the polynomial equation of ๐(๐ฅ) =
๐๐ + ๐๐ (๐ฅ โ ๐ฅ๐ ) + ๐๐ (๐ฅ โ ๐ฅ๐ )2 .
Task
Write a function to estimate the bubble point temperature of a benzene/toluene mixture
given total pressure, liquid fraction benzene, and the number of points that should be
used to build a quadratic interpolating polynomial. To make sure everyone uses the same
values, the spline function data must set the second derivative of the last polynomial to
zero (using pure toluene, i.e., no benzene, as the first point). Use the following data for
Antoineโs equation (with the temperature in Celsius and pressure in mmHg):
Compound
A
B
C
Benzene
6.89272
1203.531
219.888
Toluene
6.95805
1346.773
219.693
You must use a quadratic spline polynomial and you are not allowed to use built-in Matlab
commands for interpolation or polynomial generation.
Page 2 of 4
CHEE 3602, Topic 3: Interpolating polynomials
Fall 2023
Core requirements
Function name
bt_txy
Input
Total pressure (in mmHg), liquid fraction benzene (๐ฅ๐ต ), and the
total number of points used to perform the spline interpolation
(๐)
Output
A single temperature value corresponding to the bubble point of
the mixture
Errors
If pressure input is less than 0, fraction benzene is not a valid fraction, or the total number of points is less than 3
Example
bt_txy(760, 0.5, 4) should give 91.9104
There are several hints for this lab:
Hint 1
You will need to solve a system of linear equations to get all the ๐๐ values.
You do not need to program your own Gauss elimination to do this. Just
use Matlabโs linsolve command. Given a system of equations of the
form ๐ด๐ฅ = ๐, you would use x = linsolve(A, b).
Hint 2
The system of equations that appears in the spline equation only has nonzero values on 2 diagonal lines. You can use Matlabโs diag command to
help construct this matrix. Check the diag help page for more information
Hint 3
It is possible to meet core requirements without any looping whatsoever.
You can use loops if you want, but remember Matlabโs array math.
Bonus requirements
Expand the original bt_txy function to also display a figure plotting the original data as
points and the interpolating spline as a line.
Page 3 of 4
CHEE 3602, Topic 3: Interpolating polynomials
Function name
Fall 2023
bt_txy
Input
Total pressure (in mmHg), liquid fraction benzene (๐ฅ๐ต ), and the
total number of points used to perform the spline interpolation
(๐)
Output
A single temperature value corresponding to the bubble point of
the mixture
Side-effect
Generate a figure plotting the T-xy data as points with the interpolating spline as a line as a pop-up
Errors
Example
If pressure input is less than 0, fraction benzene is not a valid fraction, or the total number of points is less than 3
bt_txy(760, 0.5, 4) should give 91.9104 as well as pop up a
plot similar to the example below
Submission checklist
Use the following checklist to help ensure you are meeting the core lab requirements:
You are submitting a single file called bt_txy.m which defines a function called bt_txy
Your function returns a value:
bt_txy(760, .5, 4)/2 should not cause problems
Your function should work correctly for any value of ๐ โฅ 3 and liquid fraction 0 โฅ ๐ฅ๐ต โฅ
1
Page 4 of 4
CHEE 3602 โ Topic 3:
Interpolating polynomials
Stanislav Sokolenko
Fall 2023
Motivation
Interpolating polynomials
Motivation
Why interpolate?
Many important mathematical operations are only defined for functions (equations)
e.g. the concept of derivative/integral only makes sense for functions โ what is โthe rate of changeโ of a set of data points?
Collected empirical data must be converted into functional form so
that it can be processed/analyzed
e.g. calculating the rate of reaction given a set of concentrations
requires fitting a function to the concentration data
3/118
Interpolating polynomials
Motivation
Numerical methods
Interpolation is the process of fitting individual data points to
a function โ enabling analysis that would not be possible using discrete points.
Many common numerical methods are derived using interpolation, even if there is no apparent interpolation taking place.
4/118
Interpolating polynomials
Motivation
Numerical methods
This topic is divided into two specific tasks:
Building interpolating polynomials
Applying interpolating polynomials to develop methods for differentiation/integration
We will start by going through a practical example of interpolation and
justifying the use of polynomials for interpolation.
5/118
Interpolating polynomials
Motivation
6/118
Defining interpolation
Interpolation refers to the approximation of a function
using a series of tabulated values.
๐ (๐ฅ)
Interpolating polynomials
Motivation
Reading tables
In effect, interpolation is used to estimate values between table rows.
T (ยฐC)
P
*
(Hg)
90
525.76
92
566.99
94
610.91
96
657.62
98
707.27
100
760.00
Given the table above, what is the vapour pressure of water at 96.5 ยฐC?
7/118
Interpolating polynomials
Motivation
8/118
Reading tables
Given that there is no entry for 96.5 ยฐC, there are a couple of options
T (ยฐC)
P
*
(Hg)
90
525.76
92
566.99
94
610.91
96
657.62
98
707.27
100
760.00
The simplest is to assume that
96.5 โ 96
Therefore,
P
(96.5) โ 657.62
*
…
Interpolating polynomials
Motivation
Reading tables
This is a very rough approximation. How can it be improved?
T (ยฐC)
P
*
(Hg)
90
525.76
92
566.99
94
610.91
96
657.62
98
707.27
100
760.00
96.5 is between 96 and 98
(96.5 โ 96)/(98 โ 96) = 0.25
So the pressure should be higher
P
(96.5) โ 657.62 + (707.27 โ 657.62)(0.25)
*
= 670.03
9/118
Interpolating polynomials
Motivation
Mathematically
Both approximations can be expressed as interpolation:
1.
The 657.62 estimate used a 0 order polynomial
2.
The 670.03 estimate used a 1st order polynomial
The 1st order polynomial captures more information around
the surrounding table entries than the 0 order.
the only option?
But is this is
10/118
Interpolating polynomials
Motivation
11/118
Interpolating functions
The goal of interpolation is to define a function that can fill in the
gaps between data table entries (or data points)
If the function that generated the original data is unknown, it makes
sense to choose something convenient
A good interpolating function should be easy to
generate (formulate)
evaluate
differentiate
integrate
Interpolating polynomials
Motivation
Polynomials
Polynomials satisfy every single one of the requirements!
12/118
Interpolating polynomials
Motivation
Polynomials
An nth-order polynomial is defined as
๐๐ (๐ฅ) = ๐0 + ๐1 ๐ฅ + ๐2 ๐ฅ2 + … + ๐๐ ๐ฅ๐
An nth-order polynomial
requires
๐๐ (๐ฅ) has ๐ + 1 coefficients and therefore
๐ + 1 points to estimate.
13/118
Interpolating polynomials
Motivation
Back to the table
Consider estimating vapour pressure at 96.5 one more time,
T (ยฐC)
P
*
(Hg)
90
525.76
92
566.99
94
610.91
96
657.62
98
707.27
100
760.00
Three values can be used to fit a quadratic.
14/118
Exumple
Tefx
)
x *> ( x X 3 )
L
R ๅถ)
โ
(x *) ( x ร7) fr
–
–
* *^) <
L
-
x
-
,
โฝๅจ
*
-
f
โ
ร )
3
-
t
,
่ฟ
wn,
+
xx
0
0 )
)xc . y ( Lx
t โผ
-
"
.
3
)1
-
c
)
9 ((1 0)
t
-
JM L JC . }
)
-
132 01 399 68
^+
284 ,
:
-
.
=
13 66 x
.
^
-
.
+
44 18x + 110 62
.
.
-
c
ไธโ
qi
0
57 91 399 .68
.
+
t
)
ฯ
L
) (
0 .
0
.
39 )
391 . 96 ) ร+ 110 . 62
-
Interpolating polynomials
Motivation
Formal justification
A nice feature of using polynomials is that they can be used to
approximate a subset of any continuous function.
More formally, the Weierstrass approximation theorem states
๐ (๐ฅ) is a continuous function in the closed interval
๐ โค ๐ฅ โค ๐, then for every ๐ > 0, there exists a polynomial
๐๐ (๐ฅ), where the value of ๐ depends on the value of ๐ such that
for all x in the closed interval ๐ โค ๐ฅ โค ๐, |๐๐ (๐ฅ) โ ๐ (๐ฅ)| < ๐.โ
that: โIf
15/118
Interpolating polynomials
Motivation
Polynomial uniqueness
It is only possible to define one polynomial of order
through
fined.
๐ that passes
๐ + 1 points, regardless of how the polynomial is de-
This means that while some methods for polynomial
construction may be more convenient than others, the final
output is often identical.
16/118
Interpolating polynomials
Motivation
Chemical engineering example
Temperature
Recall the T-xy diagram:
Vapour fraction
Liquid fraction
Fraction benzene
Although there is an algorithm for getting
straightforward way of getting
๐ = ๐ (๐ฅ, ๐ฆ)
๐ฅ, ๐ฆ = ๐ (๐), there is no
17/118
Interpolating polynomials
Motivation
Interpolation
One practical option is to generate a set of
๐ฅ, ๐ฆ points using
๐ , and then interpolate to find the reverse
relation: ๐ = ๐ (๐ฅ, ๐ฆ).
different values of
18/118
Interpolating polynomials
Motivation
Interpolation
Temperature
Linear interpolation can get good results with a large number of points:
Fraction benzene
19/118
Interpolating polynomials
Motivation
Interpolation
Temperature
But increasing interpolation order can yield dramatic improvements:
Fraction benzene
20/118
Interpolating polynomials
Motivation
Nomenclature
These slides reserve the term interpolation for fitting methods where the line of fit must pass through every single point.
In contrast to regression, where the line of fit does not have to
pass through every single data point.
21/118
Interpolating polynomials
Motivation
22/118
Noise
...
Note, passing through every single point is not always appropriate
Good interpolation
Bad interpolation
Never interpolate noisy data!
Good regression
Interpolating polynomials
Motivation
Topic outline
1.
Interpolating polynomials
2.
Differentiation/integration
23/118
Interpolating polynomials
Motivation
24/118
Learning outcomes
By the end of this topic, you should be able to:
1.
Apply Lagrange, divided difference, and Newton polynomial methods to generate interpolating functions
2.
Estimate interpolation error
3.
Understand the derivation of spline interpolation
4.
Derive and apply finite difference and Newton-Cotes methods to
differentiate or integrate data
5.
Understand and apply adaptive integration methods
Interpolating polynomials
Interpolating polynomials
Interpolating polynomials
26/118
Lagrange
Lagrange polynomial
The Lagrange interpolating polynomial is the most intuitive of all the
interpolating polynomials.
Given three points
๐2 (๐ฅ) =
(๐ฅ1 , ๐1 ), (๐ฅ2 , ๐2 ), (๐ฅ3 , ๐3 ):
(๐ฅ โ ๐ฅ2 )(๐ฅ โ ๐ฅ3 )
(๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ3 )
(๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ2 )
๐1 +
๐2 +
๐
(๐ฅ1 โ ๐ฅ2 )(๐ฅ1 โ ๐ฅ3 )
(๐ฅ2 โ ๐ฅ1 )(๐ฅ2 โ ๐ฅ3 )
(๐ฅ3 โ ๐ฅ1 )(๐ฅ3 โ ๐ฅ2 ) 3
...
It looks complicated, but this equation is straightforward to derive
Interpolating polynomials
Interpolating polynomials
27/118
Lagrange
The derivation
First,
๐2 (๐ฅ) must pass through all of the ๐๐ values, so it makes sense
that they would be in the final equation:
๐2 (๐ฅ) =
๐1 +
So what can be multiplied by
when
๐ฅ = ๐ฅ1 ?
๐2 +
๐3
๐2 to make sure that the ๐2 term disappears
Interpolating polynomials
Interpolating polynomials
28/118
Lagrange
The derivation
At
๐ฅ = ๐ฅ1 , (๐ฅ โ ๐ฅ1 ) = 0, so the ๐2 and ๐3 terms disappear:
๐2 (๐ฅ) =
But what about the
๐1 +
(๐ฅ โ ๐ฅ1 )
๐1 and ๐3 terms for ๐ฅ2 ?
๐2 +
(๐ฅ โ ๐ฅ1 )
๐3
Interpolating polynomials
Interpolating polynomials
29/118
Lagrange
The derivation
Continuing:
๐2 (๐ฅ) =
(๐ฅ โ ๐ฅ2 )
๐1 +
(๐ฅ โ ๐ฅ1 )
๐2 +
(๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ2 )
๐3
Interpolating polynomials
Interpolating polynomials
30/118
Lagrange
The derivation
And the numerators are done:
๐2 (๐ฅ) =
Plug in
(๐ฅ โ ๐ฅ2 )(๐ฅ โ ๐ฅ3 )
๐1 +
(๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ3 )
๐2 +
(๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ2 )
๐ฅ = ๐ฅ1 , what should the denominator be to get ๐1 ?
๐3
Interpolating polynomials
Interpolating polynomials
Lagrange
The derivation
Divide by
(๐ฅ1 โ ๐ฅ2 )(๐ฅ1 โ ๐ฅ3 ) to make sure ๐2 (๐ฅ1 ) = ๐1 :
๐2 (๐ฅ) =
(๐ฅ โ ๐ฅ2 )(๐ฅ โ ๐ฅ3 )
(๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ3 )
(๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ2 )
๐1 +
๐2 +
๐3
(๐ฅ1 โ ๐ฅ2 )(๐ฅ1 โ ๐ฅ3 )
And follow the same approach for the other terms
...
31/118
Interpolating polynomials
Interpolating polynomials
Lagrange
The derivation
Overall result is therefore:
๐2 (๐ฅ) =
(๐ฅ โ ๐ฅ2 )(๐ฅ โ ๐ฅ3 )
(๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ3 )
(๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ2 )
๐1 +
๐2 +
๐
(๐ฅ1 โ ๐ฅ2 )(๐ฅ1 โ ๐ฅ3 )
(๐ฅ2 โ ๐ฅ1 )(๐ฅ2 โ ๐ฅ3 )
(๐ฅ3 โ ๐ฅ1 )(๐ฅ3 โ ๐ฅ2 ) 3
As illustrated before
...
32/118
Interpolating polynomials
Interpolating polynomials
Lagrange
General form
The overall equation can be concisely expressed as:
๐ฅ โ ๐ฅ๐ โ
โ
โ ๐๐
๐๐ (๐ฅ) = โ โโ
๐ฅ
โ
๐ฅ
๐
๐
๐=1 โ ๐โ ๐
โ
๐+1
33/118
Interpolating polynomials
Interpolating polynomials
34/118
Lagrange
Polynomial evaluation
Once a polynomial is constructed using a set of points, the denominator
and the
๐๐ values do not change:
๐2 (๐ฅ) = (๐ฅ โ ๐ฅ2 )(๐ฅ โ ๐ฅ3 )๐1 + (๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ3 )๐2 + (๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ2 )๐3
So it is more efficient to calculate the constant values of
for a set of points and then store them for later use.
...
But adding an extra point requires a lot of extra math
๐1 , ๐2 , ๐3 once
Interpolating polynomials
Interpolating polynomials
Lagrange
Example
Problem 1
Interpolate the following T-xy temperature data as a function
of fraction benzene using a Lagrange polynomial:
i
Temperature (ยฐC)
Fraction benzene
1
80.10
1.00
2
95.36
0.39
3
110.62
0.00
35/118
Exumple
Tefx
)
x *> ( x X 3 )
L
R ๅถ)
โ
(x *) ( x ร7) fr
–
–
* *^) <
L
-
x
-
,
โฝๅจ
*
-
f
โ
ร )
3
-
t
,
่ฟ
wn,
+
xx
0
0 )
)xc . y ( Lx
t โผ
-
"
.
3
)1
-
c
)
9 ((1 0)
t
-
JM L JC . }
)
-
132 01 399 68
^+
284 ,
:
-
.
=
13 66 x
.
^
-
.
+
44 18x + 110 62
.
.
-
c
ไธโ
qi
0
57 91 399 .68
.
+
t
)
ฯ
L
) (
0 .
0
.
39 )
391 . 96 ) ร+ 110 . 62
-
Interpolating polynomials
Interpolating polynomials
Divided difference
Divided difference polynomial
The ideal method for polynomial construction should enable
flexible calculation of different order polynomials using the
same set of points. One way to do this is with divided difference polynomials.
So, what is this divided difference?
36/118
Interpolating polynomials
Interpolating polynomials
Divided difference
Divided difference
A divided difference of order 1,
the two points
[ ๐1 , ๐2 ], is effectively the slope between
(๐ฅ1 , ๐1 ), (๐ฅ2 , ๐2 ):
[ ๐1 , ๐ 2 ] =
And a divided difference of order 2,
two slopes between three points:
[ ๐1 , ๐ 2 , ๐ 3 ] =
๐2 โ ๐ 1
๐ฅ2 โ ๐ฅ 1
[ ๐1 , ๐2 , ๐3 ], is like a โslopeโ of the
[ ๐2 , ๐ 3 ] โ [ ๐ 1 , ๐ 2 ]
=
๐ฅ3 โ ๐ฅ 1
๐3 โ ๐ 2
๐2 โ ๐1
โ
๐ฅ3 โ๐ฅ2
๐ฅ2 โ๐ฅ1
๐ฅ3 โ ๐ฅ 1
37/118
Interpolating polynomials
Interpolating polynomials
38/118
Divided difference
Divided difference polynomial
t ] ไธ
fi
,
:
a*
F,
ๆถ
tniIr
A polynomial of order
๐ can then be built using:
้ธ
!
๐๐ (๐ฅ) = [ ๐๐ ] + (๐ฅ โ ๐ฅ๐ )[ ๐๐ , ๐๐+1 ] + (๐ฅ โ ๐ฅ๐ )(๐ฅ โ ๐ฅ๐+1 )[ ๐๐ , ๐๐+1 , ๐๐+2 ] + ...
Given three points
would be:
(๐ฅ1 , ๐1 ), (๐ฅ2 , ๐2 ), (๐ฅ3 , ๐3 ), a second order polynomial
๐2 (๐ฅ) = [ ๐1 ] + (๐ฅ โ ๐ฅ1 )[ ๐1 , ๐2 ] + (๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ2 )[ ๐1 , ๐2 , ๐3 ]
Interpolating polynomials
Interpolating polynomials
Divided difference
Divided difference polynomial
๐2 (๐ฅ) = [ ๐1 ] + (๐ฅ โ ๐ฅ1 )[ ๐1 , ๐2 ] + (๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ2 )[ ๐1 , ๐2 , ๐3 ]
This may not look very convenient, but recall that:
[ ๐1 , ๐ 2 , ๐ 3 ] =
[ ๐2 , ๐ 3 ] โ [ ๐ 1 , ๐ 2 ]
๐ฅ3 โ ๐ฅ 1
So polynomial order can be increased with one new term.
39/118
Interpolating polynomials
Interpolating polynomials
Divided difference
Table of differences
The divided difference polynomial can be built up using a table
of pre-computed divided differences, with the total number of
terms selected based on the required precision.
This is a major improvement over the Lagrange polynomial,
which must be almost entirely rebuilt if it is necessary to change
the number of points used in the interpolation.
40/118
Interpolating polynomials
Interpolating polynomials
Divided difference
Shorthand notation
Since the square bracket notation
[ ๐1 , ๐2 , ๐3 , ...] can quickly grow very
long, it is common to use a shorthand notation:
[ ๐1 ] = ๐1(0) = ๐1
[ ๐1 , ๐2 ] = ๐1(1) =
๐2(0) โ ๐1(0)
๐2 โ ๐ 1
=
๐ฅ2 โ ๐ฅ 1
๐ฅ2 โ ๐ฅ 1
3
2
(1)
(1)
โ ๐ฅ2โ๐ฅ1
๐
โ
๐
๐ฅ
โ๐ฅ
2
1
1
[ ๐1 , ๐2 , ๐3 ] = ๐1(2) = 2
= 3 2
๐ฅ3 โ ๐ฅ 1
๐ฅ3 โ ๐ฅ 1
๐ โ๐
๐ โ๐
41/118
Interpolating polynomials
Interpolating polynomials
Divided difference
General form
So the overall equation can be concisely expressed as:
๐
๐
โโ(๐ฅ โ ๐ฅ๐ )โ
โ ๐1(๐)
๐๐ (๐ฅ) = โ โ
๐=0 โ ๐=1
โ
42/118
Interpolating polynomials
Interpolating polynomials
Divided difference
Verification
Problem 2
Take the definition of a divided difference polynomial:
๐๐ (๐ฅ) = [ ๐1 ] + (๐ฅ โ ๐ฅ1 )[ ๐1 , ๐2 ] + (๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ2 )[ ๐1 , ๐2 , ๐3 ] + ...
and show that for
๐ฅ = ๐ฅ2 , ๐๐ (๐ฅ2 ) = ๐2 .
43/118
Interpolating polynomials
Interpolating polynomials
Divided difference
Example
Problem 3
Interpolate the following T-xy temperature data as a function
of fraction benzene using a divided difference polynomial:
i
Temperature (ยฐC)
Fraction benzene
1
80.10
1.00
2
95.36
0.39
3
110.62
0.00
44/118
"
fi extid
Px) :
)
:
t
f,"
)
, )
t
,
8. 1
:
,
( - xp ( xx
by alaulaticndiviliddiffereue
start
f
x
ร2
-
95 36
,
:
87 (c
-
.
- 25 . 0 z
x,
0.
39
-
1
ti fi " fie
)
i
or
โ
10. 10
1
0
2
0
}
f
.
14 (
!
1
,
y95 .36 - 34 - 13
โฆ
)
011 .62
t
โก]
I
!
.3
-25
"
-
ใ
f
โฆ
.
โฆ
"
,
โผ
,
ร
3
-
ร,
t 25
39 13
-
.
:
o
=
if
we
(
-
02
,
1
4 11
,
Ist
wave
P a)
a
2
.
โผ
"
80
,
10 +
order
xL) L
-
ashimtc
,
25 c 2 )
.
udorder
Prx)
:
87 , 0 t
L) (
-
2 i )+ L
)( x- . n)
0
3
al 4 . 1)
l
Ervor
atuate :
)1 x
14 .
)
1
0 (
PRx โผโผผ
11
)
coupave
Expoud to
appractins
.
4 . llx
e ito 39 .
tLi
8ol
on
'xel
14
0
=
14 11x -44
'
.
Tese of
i 3)
.
.
.
3Y
,
6)x 4 (
10 62
.
RCC 39)
.
:
95 36
.
,
2
Interpolating polynomials
Interpolating polynomials
Newton
Newton polynomial
Recall that one of the trade-offs considered when comparing
numerical methods is robustness/generality vs. speed. A simplifying assumption can often be used to generate a simplified
and potentially better version of an existing algorithm.
One such simplification that is often made with interpolation
is to choose equidistant points, making it possible to get rid of
(๐ฅ๐+1 โ ๐ฅ๐ ) terms in the divided difference formulation and
replace them with a constant step size of โ.
all
45/118
Interpolating polynomials
Interpolating polynomials
Newton
Finite differences
Since there is no need to keep track of all the
ฮ(0) ๐1 = ๐1
๐ฅ terms, remove them:
ฮ(1) ๐1 = ฮ(0) ๐2 โ ฮ(0) ๐1 = ๐2 โ ๐1
ฮ(2) ๐1 = ฮ(1) ๐2 โ ฮ(1) ๐1 = ( ๐3 โ ๐2 ) โ ( ๐2 โ ๐1 )
= ๐ 3 โ 2 ๐2 + ๐ 1
46/118
Interpolating polynomials
Interpolating polynomials
47/118
Newton
Newton polynomial
(๐ฅ โ ๐ฅ๐ ) terms are replaced by the number of โstepsโ ๐ฅ is from
the first point ๐ฅ1 , where each step โ = ๐ฅ2 โ ๐ฅ1 :
All the
๐ =
๐ฅ โ ๐ฅ1
โ
Allowing the Newton polynomial to be defined as:
๐๐ (๐ ) = ฮ(0) ๐1 + ๐ ฮ(1) ๐1 +
๐ (๐ โ 1) (2)
๐ (๐ โ 1)(๐ โ 2) (3)
ฮ ๐1 +
ฮ ๐1 + ...
2!
3!
Which is a considerable simplification over the divided difference approach that can be used when the data is on an equally spaced grid.
Interpolating polynomials
Interpolating polynomials
Newton
The kernel of many applications
The Newton polynomial (finite difference) formulation forms
the basis of topic 4.
Start getting used to it now!
48/118
Interpolating polynomials
Interpolating polynomials
Newton
Verification
Problem 4
Substitute
๐ = (๐ฅโ๐ฅ1 )/โ into the divided difference equation:
๐2 (๐ฅ) = ๐1(0) + (๐ฅ โ ๐ฅ1 ) ๐1(1) + (๐ฅ โ ๐ฅ1 )(๐ฅ โ ๐ฅ2 ) ๐1(2)
and show that it is equivalent to the Newton polynomial when
๐ฅ2 โ ๐ฅ 1 = ๐ฅ 3 โ ๐ฅ 2 = โ .
49/118
Interpolating polynomials
Interpolating polynomials
Newton
Example
Problem 5
Using the vapour pressure example data from the beginning
of this topic, generate a 2nd order Newton polynomial from
๐ฅ1 = 94 to calculate ๐โ (96.5).
50/118
Interpolating polynomials
Interpolating polynomials
Newton
Different differences
Newton polynomials were introduced with forward differences:
ฮ ๐๐ = ๐๐+1 โ ๐๐
But backward differences are not much different:
โ ๐๐ = ๐๐ โ ๐๐โ1
๐๐ (๐ ) = โ(0) ๐๐ + ๐ โ(1) ๐๐ +
๐ =
๐ฅ โ ๐ฅ๐
โ
๐ (๐ + 1) (2)
๐ (๐ + 1)(๐ + 2) (3)
โ ๐๐ +
โ ๐๐ + ...
2!
3!
51/118
Interpolating polynomials
Interpolating polynomials
Spline interpolation
Global vs. local fit
๐ points requires a polynomial of order ๐ โ 1. However, working with high order polynomials (๐ > 20) can lead
Fitting a
to large round-off errors under some circumstances.
Instead of using a single high order polynomial to perform a
global fit, one solution is to combine many low order polynomial to perform a local fit.
52/118
Interpolating polynomials
Interpolating polynomials
Spline interpolation
Basic setup
Consider a small set of points to be fit:
(๐ฅ2 , ๐2 )
(๐ฅ1 , ๐1 )
(๐ฅ3 , ๐3 )
(๐ฅ4 , ๐4 )
53/118
Interpolating polynomials
Interpolating polynomials
Spline interpolation
1st order
Unique 1st order splines can be generated by forcing each spline to
touch neighbouring points.
(๐ฅ2 , ๐2 )
(๐ฅ1 , ๐1 )
(๐ฅ3 , ๐3 )
(๐ฅ4 , ๐4 )
54/118
Interpolating polynomials
Interpolating polynomials
Spline interpolation
1st order
๐ points can be fit by ๐ โ 1 polynomials, so:
Parameters:
2 parameters per
๐ โ 1 polynomial = 2(๐ โ 1)
Available information:
Each of the
๐ โ 1 polynomials touches 2 points
2(๐ โ 1) โ 2(๐ โ 1) = 0
(sufficient to calculate all parameters)
55/118
Interpolating polynomials
Interpolating polynomials
Spline interpolation
2nd order
However, these conditions are not sufficient for 2nd order splines.
(๐ฅ2 , ๐2 )
(๐ฅ1 , ๐1 )
(๐ฅ3 , ๐3 )
(๐ฅ4 , ๐4 )
Data points alone do not specify a unique set of polynomials.
56/118
Interpolating polynomials
Interpolating polynomials
Spline interpolation
2nd order
๐ points can be fit by ๐ โ 1 polynomials, so:
Parameters:
3 parameters per
๐ โ 1 polynomial = 3(๐ โ 1)
Available information:
Each of the
๐ โ 1 polynomials touches 2 points
3(๐ โ 1) โ 2(๐ โ 1) = ๐ โ 1
(not enough to calculate parameters)
57/118
Interpolating polynomials
Interpolating polynomials
Spline interpolation
2nd order
Intuitively, it should be clear that this is a bad fit:
(๐ฅ2 , ๐2 )
(๐ฅ3 , ๐3 )
(๐ฅ1 , ๐1 )
But how can a good fit be described mathematically?
(๐ฅ4 , ๐4 )
58/118
Interpolating polynomials
Interpolating polynomials
Spline interpolation
2nd order
In general, a โgoodโ fit is smooth
(๐ฅ2 , ๐2 )
(๐ฅ3 , ๐3 )
(๐ฅ1 , ๐1 )
And smoothness is defined by continuous derivatives
(๐ฅ4 , ๐4 )
…
59/118
Interpolating polynomials
Interpolating polynomials
Spline interpolation
2nd order
๐ points can be fit by ๐ โ 1 polynomials, so:
Parameters:
3 parameters per
๐ โ 1 polynomial = 3(๐ โ 1)
Available information:
Each of the
๐ โ 1 polynomials touches 2 points
Neighbouring polynomials have same 1st derivatives
at
๐ โ 2 points
3(๐ โ 1) โ 2(๐ โ 1) โ (๐ โ 2) = 1
(still not enough to calculate parameters)
60/118
Interpolating polynomials
Interpolating polynomials
Spline interpolation
2nd order
๐ points can be fit by ๐ โ 1 polynomials, so:
Parameters:
3 parameters per
๐ โ 1 polynomial = 3(๐ โ 1)
Available information:
Each of the
๐ โ 1 polynomials touches 2 points
Neighbouring polynomials have same 1st derivatives
at
๐ โ 2 points
Force 1st or 2nd derivative to zero at 1 point
3(๐ โ 1) โ 2(๐ โ 1) โ (๐ โ 2) โ 1 = 0
(sufficient to calculate all parameters)
61/118
Interpolating polynomials
Interpolating polynomials
Spline interpolation
Higher orders
Increasing polynomial orders require more conditions
Typically, continuity conditions are extended to all higher orders
๐ โ 2 points and have the
same 1st, 2nd, 3rd, etc., derivatives at ๐ โ 2 points
neighbouring polynomials connect at
Extra conditions are provided by setting the derivatives of edge
polynomials to zero
62/118
Interpolating polynomials
Interpolating polynomials
Spline interpolation
In practice
Cubic polynomials are usually smooth enough for most applications.
Spline definition may not be applicable for special cases like
discontinuities. It may be necessary to implement custom spline
equation with special definitions or make use of advanced packages.
63/118
Interpolating polynomials
Interpolating polynomials
Spline interpolation
Some math
Problem 6
Derive a set of equations that can be used to calculate spline
parameters for a quadratic spline across
๐ points.
64/118
Example 6
.
sit
1
1
โ
i
โ
โ
xo
six]
ien
tie
is
tfinfisfi ,
di
:
need
we
Xi+ 1
Con
–
X0
โ
e
fod
eo
‘
bilx xi” t
bi ci
,
a,
( x xu)
C,
–
–
lne
fuall
iegnae ,
li
liunty candition
on
the
left
silxi)
xis
fl aitbi xix tcicxi
a
i
,
=
โก
si (
)
fi):
ai
fi)
:
FEce
โข
xit
Ist
–
,
=fi)
tbi ( xie
–
fls ebihiecihi
derivaeve
Si ‘(xlt 1
]
=Sit
xi
)
)
xi tCi ( xier
eo
1 ( Xit )
be
–
‘
equal
to
heighbauring
playoamials
,
bf t
2ci
hie
xity
lxour -xo
)
,
t
2 cieitxun –
( bit
้ Jhi
fiur fitbihi t
=
fien fi
2
:
bo tbie
hi
โ
T
known
bitbit
,
unbow
2
=
)
ๅท
hi
f)
2
bitr
bitlt
:
hitll ไบ
โผ
1
1
1
co
ฮ
(
10
0
โฆ
โฆ โฆ
–
last
our
”
s n-
[
| ]
โฆ
โฆ
โฆ
carditichiwillsetund dorvaeve
uelaie
pilynnut
1 ( xn
)
2
: car
=
0
fuituy tbue hurs tCut
hnl ,
s bas
ๅจ
:
t
ฯ
recap
!
ifwe
Cr
have
we
.
n
–
polynennele
l
.
I
l
I
[
?
=
pones
a
[
a =
b
hoave
ai
fiso
=
,
bi
is
fom
salved
asyslem
of
lnen
,
equalin
.
ๅฃ
i ้ฌญ
ๅ
1
1
0
1
0
0
โผ
7
O
G
O
(
0
0
–
!
00
0
โฆ
O
โฆ
โฆ
โฆ
Cr
Eul ia
–
assumed
as
side
A
from biel bofer iilonmz
solue
2s
–
.
do
How
.
,
we
this
me
,
c
ฮ
0
โ
โผ
ใ
O
ฮจ
โผ
f(
n
.
6
) ?
fiud si
xi = U
we
me
8
,
whochpoly namial
V= 2
cican 6
)
a
thi luo
su – ar
6
–
)
t
–
Interpolating polynomials
Interpolating polynomials
Method comparison
Algorithm selection
Algorithm selection depends on a large number of different factors, the
following is just a suggestion:
If you are dealing with more than 20 points: splines
If your data is on an evenly spaced grid: Newton polynomial
Otherwise: divided difference polynomial
The Lagrange interpolating polynomial was introduced because it is
easy to understand.
Its only advantage over the divided difference
method is that it is easier to program.
65/118
Interpolating polynomials
Interpolating polynomials
Method comparison
Caution
All interpolating techniques define a function that passes through
every single observed data point. You should not do this if your
data is noisy!
66/118
Interpolating polynomials
Interpolating polynomials
Method comparison
Further reading
Chapter 4 in the book covers everything in these slides and
more. Sections 4.1-4.2 serve as a good introduction to interpolation, sections 4.4-4.6 cover the three major global interpolation methods, and section 4.9 describes cubic splines.
67/118
Derivatives and integrals
Interpolating polynomials
Derivatives and integrals
Background
Derivatives and integrals
Beyond the direct application of interpolating polynomials to
โread between the table rowsโ, interpolating polynomials are
also used to develop methods for techniques such as differentiation and integration.
69/118
Interpolating polynomials
Derivatives and integrals
Background
Mathematical interpretation
Derivatives and integrals are often conceptualized geometrically
a derivative is the slope of the tangent line at a given point
an integral is the area under a section of a curve
This conceptualization can be used to determine how to calculate a
derivative or integral
but it does not explain why either is useful
70/118
Interpolating polynomials
Derivatives and integrals
Background
Physical interpretation
A derivative corresponds to a rate of change, while an integral
is an accumulation (typically over space or time).
These values may be useful on their own or as a means of calculating others.
71/118
Interpolating polynomials
Derivatives and integrals
72/118
Background
Rate of change
The flow rate (
๐) out of the pipe
is directly related to the rate of
change of fluid height in the ves-
H
Q
sel (
๐ป ):
๐=๐ด
vessel
d๐ป
d๐ก
Interpolating polynomials
Derivatives and integrals
Background
Rate of change
Note that the above expression is not theoretical (we do not
necessarily know the rate of change in fluid height in advance).
However, fluid height is considerably easier to measure than
flow rate.
73/118
Interpolating polynomials
Derivatives and integrals
74/118
Background
Accumulation
Q
The overall fluid height in the ves-
๐ป ) is directly related to the
flow rate (๐) out of the pipe over
sel (
time:
H
๐ป(๐ก) = ๐ปโฃ๐ก=๐ก +
0
๐ด
1
vessel
๐ก
โซ ๐(๐ก)dt
๐ก0
Interpolating polynomials
Derivatives and integrals
Background
Accumulation
If we know the flow rate of fluid going into the pipe, we can
predict the height of the fluid in the vessel in advance.
75/118
Interpolating polynomials
Derivatives and integrals
Background
Another example
Derivatives and integrals are also used for monitoring reactions
Differentiation to calculate reaction rates from concentration data
๐=
d๐ถ
d๐ก
Integration to calculate concentration from reaction rate
๐ก
๐ถ(๐ก) = ๐ถ0 + โซ ๐dt
๐ก0
76/118
Interpolating polynomials
Derivatives and integrals
77/118
Background
Numerical vs. analytical
It is possible to solve simple derivatives and integrals analytically
typically using pattern recognition (to identify useful variable
substitutions or techniques like integration by parts)
Numerical methods are often simpler and allow the solution of
problems that cannot be solved analytically
Numerical methods can also be used to work with data (a series of
points) where the true function is unknown
Interpolating polynomials
Derivatives and integrals
Background
Simplifying equations
The general differentiation/integration technique is always the
same โ interpolate input data into a function and find the derivative/integral of the function as normal.
However, this process can be derived into a simple algebraic
equation for common problems.
78/118
Interpolating polynomials
Derivatives and integrals
79/118
Differentiating points
Differentiation
If the input data points are evenly spaced, a derivative equation can be
calculated by fitting a Newton polynomial to points around
(๐ฅ๐ , ๐๐ )
(๐ฅ๐ , ๐๐ ).
Interpolating polynomials
Derivatives and integrals
Differentiating points
1st order
Recall the 1st order Newton polynomial:
๐1 (๐ ) = ๐๐ + ๐ ฮ(1) ๐๐
๐ =
Where
๐ฅ โ ๐ฅ๐
โ
โ is the step size between ๐ฅ๐ , ๐ฅ๐+1 .
Calculating
d๐1 (๐ )
requires using the chain rule:
d๐ฅ
d๐1 (๐ ) d๐1 (๐ ) d๐
=
d๐ฅ
d๐ d๐ฅ
80/118
Interpolating polynomials
Derivatives and integrals
Differentiating points
1st order
The two terms:
d๐1 (๐ )
d
= ( ๐๐ + ๐ ฮ(1) ๐๐ )
d๐
d๐
= ฮ(1) ๐๐ = ๐๐+1 โ ๐๐
d๐
1
=
d๐ฅ โ
Together:
d๐1 (๐ )
๐๐+1 โ ๐๐
=
d๐ฅ
โ
81/118
Interpolating polynomials
Derivatives and integrals
Differentiating points
Visually
Comparing the estimate and true value:
(๐ฅ๐ , ๐๐ )
The estimate is good if the function is smooth and the step size is small.
82/118
Interpolating polynomials
Derivatives and integrals
Differentiating points
Higher orders
( ๐๐+1 โ ๐๐ )/โ is a first-order finite difference. The same process
can be used to define higher order finite difference equations.
83/118
Interpolating polynomials
Derivatives and integrals
Differentiating points
Higher orders
Problem 7
Derive a finite difference equation for the derivative at a point
๐ฅ๐ using a 2nd order Newton Polynomial equation.
84/118
Example
?
,
Phc
)
–
+
fc )
X
–
e
scfi
C
”
0
!
fc
)
Xi
H็ญ่ฃ
:
ft
as
u
d
+
<
e "
,
รท
fc
fit fi tI ( fiux
-
-
2
fie tfi )
,
)
:
cfit
dd
fo
"ft
:
:
00)
befere
2
โ
:
6
fitn
-
h
,
fi
o
fier fie - fier
)
fo ,
:
-
:
fitv afie 1 tfi
.
-
devvatve
porhe
2
3
pozut
derivatwa
โโโโ
G
;
โพ
ใ
โฆ
โผ
โผ
โฆ
โผ
โผ
!โด
โฆ
โกโผ
โผ
โฆ
โผ
ใ
ใ
ฮ
?
Sco
Three
differae
ae
sio
โ
our
- fi
fie
afiei
efo
fier
:
at
fon
equations
fit
fi
4
+
ti
sal
nfo -
etie
ae l -
whrch
is
sel
vs
the
most
perfend
accawate
( powes
!
at
clear to
S= 13
)
โฆ
gul
5
:
2
Interpolating polynomials
Derivatives and integrals
85/118
Integrating points
Integration
If the input data points are evenly spaced, an integral equation can be
calculated by fitting a Newton polynomial to points around
(๐ฅ๐ , ๐๐ )
(๐ฅ๐ , ๐๐ ).
Interpolating polynomials
Derivatives and integrals
Integrating points
1st order
Recall the same 1st order Newton polynomial:
๐1 (๐ ) = ๐๐ + ๐ ฮ(1) ๐๐
๐ =
Where
๐ฅ โ ๐ฅ๐
โ
โ is the step size between ๐ฅ๐ , ๐ฅ๐+1 .
Use the chain rule to go from
๐ฅ to ๐ :
๐ฅ๐+1
๐ผ = โซ ๐1 (๐ )d๐ฅ
๐ฅ๐
86/118
Interpolating polynomials
Derivatives and integrals
Integrating points
1st order
First, convert
๐ฅ to ๐
d๐
1
=
d๐ฅ โ
d๐ฅ = โ d๐
And do not forget about the limits of integration:
1
๐ผ = โซ ๐1 (๐ )โ d๐
0
With a 1st order estimate, use only two points
๐ฅ๐ , ๐ฅ๐+1 , or ๐ = 0, 1.
87/118
Interpolating polynomials
Derivatives and integrals
Integrating points
1st order
1
๐ผ = โซ ๐1 (๐ )โ d๐
0
1
= โ โซ ๐๐ + ๐ ฮ(1) ๐๐ d๐
0
โฃ
๐ 2 (1)
= โ ( ๐๐ ๐ + ฮ ๐๐ ) โฃโฃ
2
โฃ
1
= โ ( ๐๐ + ฮ(1) ๐๐ )
2
= โ ( ๐๐ +
=
๐๐+1 โ ๐๐
)
2
โ( ๐๐ + ๐๐+1 )
2
1
๐ =0
88/118
Interpolating polynomials
Derivatives and integrals
Integrating points
Visually
Comparing the estimate and true value:
(๐ฅ๐ , ๐๐ )
The estimate is good if the function is smooth and the step size is small.
89/118
Interpolating polynomials
Derivatives and integrals
Integrating points
Global estimate
A single integral is typically calculated over all the points:
90/118
Interpolating polynomials
Derivatives and integrals
Integrating points
Global estimate
To integrate over
๐ points, sum over all ๐ โ 1 sub-intervals:
๐โ1
๐ผ = โ ๐ผ๐
๐=1
But the formula allows some simplification:
โ( ๐๐ + ๐๐+1 )
2
๐=1
๐โ1
๐ผ=โ
๐โ1
โ
= ( ๐1 + โ 2 ๐๐ + ๐ ๐ )
2
๐=2
91/118
Interpolating polynomials
Derivatives and integrals
Integrating points
Higher orders
โ( ๐๐+1 + ๐๐ )/2 is a first-order Newton-Cotes equation (other-
wise known as the trapezoid rule). The same process can be
used to define higher order Newton-Cotes equations (such as
the Simpsonโs rule or Simpsonโs 3/8 rule).
92/118
Interpolating polynomials
Derivatives and integrals
93/118
Integrating points
Higher orders
Problem 8
Derive a Newton-Cotes equation for a global integral over
points using a 2nd order Newton Polynomial equation.
๐
Interpolating polynomials
Derivatives and integrals
Integrating points
Example
Problem 9
Given the following set of data, use a 2nd order Newton polynomial to estimate the derivative at
integral between
๐ฅ = 0, 1
๐ฅ = 0.5 and the global
x
0.00
0.25
0.50
0.75
1.00
y
1.00
1.28
1.65
2.12
2.72
Compare the estimates to the true values given that
๐ฆ = ๐๐ฅ .
94/118
Interpolating polynomials
Derivatives and integrals
95/118
Integrating functions
Integrating functions
When trying to find a derivative or integral from a set of points, the
underlying function is unknown
However, we may also be interested in finding the integral of a
known function
more common for integrals as derivatives are relatively easy to
find analytically
Recall the problem from topic 1 that could not be solved analytically:
๐
โซ ๐โ๐ฅ d๐ฅ
๐
2
Interpolating polynomials
Derivatives and integrals
Integrating functions
Integrating functions
Numerically integrating functions is similar to points.
Suggest a method to find the previously mentioned integral:
๐
โซ ๐โ๐ฅ d๐ฅ
๐
2
96/118
Interpolating polynomials
Derivatives and integrals
97/118
Integrating functions
Beyond points
If the function is known, it is easy to calculate function values at a
series of points and directly apply the Newton-Cotes equations
step size or order can be varied to estimate accuracy
However, knowing the function enables a better estimate than possible with a fixed set of points
Gauss-Legendre
quadrature
can
be
used
to
optimize
which
points to choose (outside the scope of the course)
Richardson extrapolation can be used to generate a more accurate estimate from two estimates with different step sizes
Interpolating polynomials
Derivatives and integrals
Integrating functions
Beyond points
By leveraging how truncation error works, so-called โadaptiveโ methods are able to get better estimates with less work.
98/118
Interpolating polynomials
Derivatives and integrals
Integrating functions
Taylor series and truncation
The Taylor series offers a convenient introduction to truncation error.
Recall the Taylor series formulation:
๐ (๐ฅ) = ๐ (๐ฅ0 ) + ๐ โฒ (๐ฅ0 )(๐ฅ โ ๐ฅ0 ) +
1
๐ โณ (๐ฅ0 )(๐ฅ โ ๐ฅ0 )2 + ...
2!
Where the ellipses can be represented by a remainder term:
๐ (๐ฅ) = ๐ (๐ฅ0 ) + ๐ โฒ (๐ฅ0 )(๐ฅ โ ๐ฅ0 ) +
1
๐ โณ (๐ฅ0 )(๐ฅ โ ๐ฅ0 )2 + ๐
3
2!
99/118
Interpolating polynomials
Derivatives and integrals
100/118
Integrating functions
Taylor series and truncation
It can be shown that the remainder can be defined as:
๐ (๐ฅ) = ๐ (๐ฅ0 ) + ๐ โฒ (๐ฅ0 )(๐ฅ โ ๐ฅ0 ) +
where
1
1
๐ โณ (๐ฅ0 )(๐ฅ โ ๐ฅ0 )2 +
๐ โด (๐ )(๐ฅ โ ๐ฅ0 )3
2!
3!
๐ is some value between ๐ฅ0 , ๐ฅ.
Essentially, the maximum possible error is:
๐
3 โค max (
1
๐ โด (๐ )(๐ฅ โ ๐ฅ0 )3 )
3!
๐ โ (๐ฅ0 , ๐ฅ)
Interpolating polynomials
Derivatives and integrals
101/118
Integrating functions
Big O notation
โ) from interpolation. It can be applied
here as well to rewrite the Taylor series as an estimate of ๐ (๐ฅ + โ) based
on the estimate at ๐ (๐ฅ), a step โ away:
Recall the idea of a step size (
Since
โ2
โ3
๐ (๐ฅ + โ) = ๐ (๐ฅ) + โ ๐ โฒ (๐ฅ) +
๐ โณ (๐ฅ) +
๐ โด (๐ )
2!
3!
๐ โด (๐ )/3! is a scalar term that modifies โ3 , it is simplified as:
โ2
๐ (๐ฅ + โ) = ๐ (๐ฅ) + โ ๐ โฒ (๐ฅ) +
๐ โณ (๐ฅ) + ๐(โ3 )
2!
which just means that if the step size is decreased 2-fold, for example,
the error will decrease 8-fold (or
23 ).
Interpolating polynomials
Derivatives and integrals
Integrating functions
Why do we care?
Although finding some arbitrary relation for the error may
seem pointless, understanding how error changes as a function of
โ enables more accurate estimates.
The following slides will demonstrate how to get this better
estimate using a simple Taylor series expansion.
102/118
Interpolating polynomials
Derivatives and integrals
Integrating functions
Richardson extrapolation
Take a 1st order Taylor series expansion:
โ2
โ3
๐ (๐ฅ + โ) = ๐ (๐ฅ) + โ ๐ โฒ (๐ฅ) + ๐ โณ (๐ฅ) + ๐ โด (๐ฅ) + ...
2!
3!
And use it to estimate the derivative
๐ โฒ (๐ฅ):
๐ (๐ฅ + โ) โ ๐ (๐ฅ) โ
โ2
๐ โฒ (๐ฅ) =
โ ๐ โณ (๐ฅ) โ ๐ โด (๐ฅ) โ ...
2!
3!
โ
103/118
Interpolating polynomials
Derivatives and integrals
Integrating functions
Richardson extrapolation
The focus here is on
โ so rename the ๐ โณ (๐ฅ) and ๐ โด (๐ฅ) terms:
๐ โฒ (๐ฅ) =
๐ (๐ฅ + โ) โ ๐ (๐ฅ)
โ โ๐2 โ โ2 ๐3 โ ...
โ
In fact, rename the other terms as well:
๐ โฒ (๐ฅ) = ๐ด(โ) โ โ๐2 โ โ2 ๐3 โ ...
where the sole focus is on
โ.
104/118
Interpolating polynomials
Derivatives and integrals
Integrating functions
Richardson extrapolation
Starting with:
๐ โฒ (๐ฅ) = ๐ด(โ) โ โ๐2 โ โ2 ๐3 โ ...
What happens if the step size is halved?
โ
โ
โ2
๐ โฒ (๐ฅ) = ๐ด ( ) โ ๐2 โ ๐3 โ ...
2
2
4
Remember that all the other parts of the equation stay the same.
105/118
Interpolating polynomials
Derivatives and integrals
Integrating functions
Richardson extrapolation
Compare the two estimates:
โ
โ
โ2
๐ โฒ (๐ฅ) = ๐ด ( ) โ ๐2 โ ๐3 โ ...
2
2
4
๐ โฒ (๐ฅ) = ๐ด(โ) โ โ๐2 โ โ2 ๐3 โ ...
Now multiply the first equation by 2 and subtract the second:
๐ โฒ (๐ฅ) = 2๐ด(โ/2) โ ๐ด(โ) โ (โ2 /2 + โ2 )๐3 โ ...
106/118
Interpolating polynomials
Derivatives and integrals
Integrating functions
Richardson extrapolation
So what happened? There is now another estimate for
๐ โฒ (๐ฅ):
๐ โฒ (๐ฅ) = 2๐ด(โ/2) โ ๐ด(โ) โ (โ2 /2 + โ2 )๐3 โ ...
But with reduced error:
๐ โฒ (๐ฅ) = 2๐ด(โ/2) โ ๐ด(โ) + ๐(โ2 )
The error term went from
๐(โ) to ๐(โ2 )!
107/118
Interpolating polynomials
Derivatives and integrals
Integrating functions
More generally
Knowing the error structure makes it possible to combine multiple poor estimates to increase accuracy!
108/118
Interpolating polynomials
Derivatives and integrals
109/118
Integrating functions
Romberg integration
Richardson extrapolation can be used equally well for calculating
both derivatives and integrals
The most common application is known as Romberg integration
1.
a function interval is integrated using the trapezoid rule
2.
the interval is then split in two and a new estimate is calculated
3.
the two estimates are combined to get a higher order estimate
4.
further subintervals are split until the approximation converges
Interpolating polynomials
Derivatives and integrals
110/118
Integrating functions
Romberg integration
The trapezoid rule error is
๐(โ2 ) and based on the structure of that er-
๐(โ2 ) estimates with different step sizes can be used to gen4
erate an ๐(โ ) estimate:
ror, two
๐ด4 =
4๐ด2 (โ/2) โ ๐ด2 (โ)
3
More generally, two estimates with order
an improved estimate of order
๐ + 2:
๐ can be combined to generate
2๐ ๐ด๐ (โ/2) โ ๐ด๐ (โ)
๐ด๐+2 =
2๐ โ 1
Interpolating polynomials
Derivatives and integrals
Integrating functions
Example
Problem 10
Perform 3 iterations of Romberg integration to find the inte-
๐๐ฅ from 0 to 1. Compare the estimates to the true value
1
0
of ๐ โ ๐
= 1.7182.
gral of
111/118
Interpolating polynomials
Derivatives and integrals
112/118
Method comparison
Algorithm selection
Algorithm selection depends on a large number of different factors, the
following is just a suggestion:
Derivatives from data on an even grid: finite-difference equations
Integrals from data on an even grid: Newton-Cotes equations
Calculating the integral of a known function: Romberg integration
Note that these options do not cover all possible cases.
The general
process is to fit a function to your data and take the derivative/integral
of that function.
Interpolating polynomials
Derivatives and integrals
Method comparison
Further reading
The book separates differentiation and integration into Chapters 5 and 6, whereas the slides try to highlight the overarching similarities.
Section 5.1 and 6.1 offer separate introduc-
tions for the two topics. Finite difference and Newton-Coates
equations are covered in 5.3 and 6.3. For adaptive integration,
see sections 6.4 and 6.5.
113/118
Final thoughts
Interpolating polynomials
Final thoughts
Interpolation is the core of numerical methods
Analytical methods are generally continuous whereas most
numerical methods work on discrete data. Interpolation fundamentally serves to convert discrete data into functional form.
You will come across many different numerical โrulesโ, but
most of these are derived directly from interpolation. It is necessary to develop a good understanding of interpolation to understand the principles behind other tasks like calculating derivatives and integrals.
115/118
Interpolating polynomials
Final thoughts
116/118
Interpolation algorithm selection
Algorithm selection depends on a large number of different factors, the
following is just a suggestion:
If you are dealing with more than 20 points: splines
If your data is on an evenly spaced grid: Newton polynomial
Otherwise: divided difference polynomial
The Lagrange interpolating polynomial was introduced because it is
easy to understand.
Its only advantage over the divided difference
method is that it is easier to program.
Interpolating polynomials
Final thoughts
117/118
Differentiation/integration algorithm selection
Algorithm selection depends on a large number of different factors, the
following is just a suggestion:
Derivatives from data on an even grid: finite-difference equations
Integrals from data on an even grid: Newton-Cotes equations
Calculating the integral of a known function: Romberg integration
Note that these options do not cover all possible cases.
The general
process is to fit a function to your data and take the derivative/integral
of that function.
Interpolating polynomials
Final thoughts
Caution
All interpolating techniques define a function that passes through
every single observed data point. You should not do this if your
data is noisy!
118/118