0% found this document useful (0 votes)
441 views54 pages

Spline Interpolation Techniques

The document discusses spline interpolation and its applications. It defines spline interpolation and different types of splines like linear, quadratic and cubic splines. It provides examples of natural and clamped cubic spline interpolation. The document also discusses using splines for curve fitting and representing functions with approximating polynomials.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
441 views54 pages

Spline Interpolation Techniques

The document discusses spline interpolation and its applications. It defines spline interpolation and different types of splines like linear, quadratic and cubic splines. It provides examples of natural and clamped cubic spline interpolation. The document also discusses using splines for curve fitting and representing functions with approximating polynomials.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

KOTEBE UNIVERISTY OF EDUCATION

College of Science and Mathematics Education


Department of Mathematics

SPLINE INTERPOLATION AND


ITS APPLICATIONS

An Undergraduate Project submitted to the Department of


Mathematics in Partial Fulfillment of the Requirements for
the Degree of Bachelor of Science in Mathematics

By

HAILEMARIAM WELDEGEBREAL YIMESGEN

[Link].: EDMA / 032 / 12

Advisor: Adolla Guyie

Addis Ababa, Ethiopia

May 2024
KOTEBE UNIVERISTY OF EDUCATION
College of Science and Mathematics Education
Department of Mathematics

Declaration
HAILEMARIAM WELDEGEBREAL declare that this paper is an original work and is
submitted to Department of Mathematics, College of Science and Mathematics Education,
Kotebe University of Education in partial fulfillment for the degree of Bachelor of Science in
Mathematics entitled:

“Spline interpolation and its application”

It has never been presented to any university or authority as clarified project. All the sources
used on the project have been acknowledged and given credit.

Students’ Name ID. No. Signature Date

HAILEMARIAM WELDEGEBREL ATE/9170/09 ___________ _________

Advisor’s Name Signature Date

ADOLLA GUYIE ___________ _________________

Examiner’s Name Signature Date

___________________ ___________ _________________

I|Page
Acknowledgment
Firstly, I am highly grateful to my advisor, Mr. Adolla Guyie who gave me unlimited advice,
instructions, and encouragement at every step of this project and gave me the necessary
materials, suggestions and comments. Secondly, I would like to express my thanks to all my
classmates especially Gelana Alemayehu who helped me not only in this project but throughout
my academic year in KUE. Last but certainly not least, I would like to express my sincere thanks
to all mathematics department staff and some of their students who provided valuable
information like reference books and personal notes for the project. I am indebted to all
individuals, friends and others for their support in the success of this project. I would like to say
thank you sincerely.

II | P a g e
Table of Contents
Declaration...................................................................................................................................................I
Acknowledgment.........................................................................................................................................II
List of graphs...............................................................................................................................................V
List of figures...............................................................................................................................................V
ABSTRACT...................................................................................................................................................VI
UNIT ONE.....................................................................................................................................................1
1. INTRODUCTION...................................................................................................................................1
1.1. DEFINITION..................................................................................................................................2
1.2. Statement of the problem...........................................................................................................4
1.3. Objective......................................................................................................................................4
1.3.1. General objective.................................................................................................................4
1.3.2. Specific objective.................................................................................................................4
1.4. SCOPE..........................................................................................................................................4
1.5. Delimitation of the project..........................................................................................................4
UNIT TWO....................................................................................................................................................5
2. Review Literature................................................................................................................................5
2.1. Introduction.................................................................................................................................5
2.2. Curve fitting.................................................................................................................................5
2.3. Interpolation................................................................................................................................6
2.4. Linear Interpolation.....................................................................................................................6
2.5. Quadratic Spline..........................................................................................................................6
2.6. Cubic Spline.................................................................................................................................7
2.7. Application of Cubic Spline Interpolation....................................................................................7
UNIT THREE.................................................................................................................................................8
3. ANALYSIS.............................................................................................................................................8
Issues with linear spline.........................................................................................................................11
3.2. Quadratic Spline Interpolation.......................................................................................................12
3.3. Cubic spline interpolation..............................................................................................................18
Natural cubic spline interpolation example.......................................................................................20

III | P a g e
Clamped cubic spline interpolation example.....................................................................................23
Another approach to using cubic spline....................................................................................................26
Curve Fitting with Splines......................................................................................................................37
Data interpretation................................................................................................................................39
Representing functions by approximating polynomials.........................................................................40
Unit four....................................................................................................................................................41
4. Conclusion.........................................................................................................................................41
References.................................................................................................................................................42
Appendices............................................................................................................................................43
Python codes.........................................................................................................................................43
Graph 1..............................................................................................................................................43
Graph 2..............................................................................................................................................43
graph 3...............................................................................................................................................43
Gauss elimination with partial pivoting.............................................................................................43

IV | P a g e
List of graphs
Graph 1: linear, quadratic and cubic interpolation......................................................................................3
Graph 2: linear interpolation........................................................................................................................8
Graph 3: cubic interpolation graph............................................................................................................18
Graph 4: clamped cubic spline Graph 5: comparison between
natural and clamped spline.......................................................................................................................25
Graph 6:function y =sin(x).........................................................................................................................37
Graph 7:six data points with a cubic spline function Graph 8: comparison of true value and
cubic spline................................................................................................................................................38
Graph 9: curve fitting illustrative example.................................................................................................38
Graph 10: the natural cubic interpolation of a solid line...........................................................................40

List of figures
Figure 1: a graph showing the difference between global and local interpolation......................................1
Let us see for 2 splines 3*n = 6 unknowns Figure 2: showing quadratic spline
p 1 x=a 1 x 2+ b 1 x +c 1..........................................................................................................................12
Figure 3: random data...............................................................................................................................39
Figure 4: polynomial graph 99 degree.......................................................................................................39
Figure 5: data interpretation with cubic spline..........................................................................................40

V|Page
ABSTRACT

Spline interpolation is a powerful mathematical tool with a wide range of applications in various
fields, including engineering, data analysis, and computer graphics. This project aims to provide
a comprehensive understanding of the different types of spline interpolation and their practical
applications.
The first part of the project will explore linear, quadratic, and cubic spline interpolation. Linear
spline interpolation is the simplest method, where the interpolating function is a piecewise linear
function. Quadratic spline interpolation uses a series of quadratic polynomials to connect the
given data points, providing a smoother curve than linear interpolation. The project will then
delve into the more sophisticated cubic spline interpolation, which uses third-degree polynomials
to connect the data points, resulting in a smooth, continuous, and differentiable curve.
The core of the project will focus on the cubic spline interpolation method. This approach offers
several advantages, including the ability to control the first and second derivatives of the
interpolating function at the data points, ensuring a smooth and continuous curve. The
mathematical formulation of the cubic spline interpolation will be presented, including the
derivation of the system of linear equations that must be solved to determine the coefficients of
the polynomial segments.
The project will also investigate the applications of spline interpolation in various domains. This
includes the use of spline interpolation in data fitting, function approximation, and numerical
integration. Additionally, the project will explore the implementation of spline interpolation
algorithms in software, such as MATLAB or Python, and demonstrate their performance on real-
world datasets.
Through this project, the undergraduate student will gain a deep understanding of the
fundamental concepts of spline interpolation, its mathematical properties, and its practical
applications. The focus on the cubic spline interpolation method will provide valuable insights
into the more advanced techniques used in modern numerical analysis and scientific computing.

VI | P a g e
UNIT ONE

1. INTRODUCTION
As an undergraduate mathematics student, I have come to appreciate the importance of
interpolation methods in a wide range of fields. Interpolation is a fundamental mathematical
concept that allows us to estimate the values of a function at points between known data points.
This technique is invaluable in situations where we have limited information about a function or
process, but need to make inferences or predictions about its behavior.

Broadly speaking, there are two main types of interpolation: global interpolation and local
interpolation. Global interpolation involves finding a single function that passes through all the
given data points, providing a continuous representation of the entire function. This is often
achieved using polynomial functions, such as Lagrange or spline interpolation. In contrast, local
interpolation techniques construct piecewise functions, each of which is valid only within a
specific interval of the data. Examples of local interpolation include linear and cubic spline
interpolation.

The choice of interpolation method depends on the characteristics of the data and the desired
properties of the interpolating function. Global interpolation methods are well-suited for smooth,
well-behaved functions, as they can capture the overall trend of the data. However, if the
function being interpolated has sharp changes or discontinuities, local interpolation techniques
may be more

1|Page
Figure 1: a graph showing the difference between global and local interpolation

The importance of interpolation methods cannot be overstated, as they find applications in a


wide variety of fields. In engineering, interpolation is used for the design and analysis of
structures, the modeling of physical systems, and the interpretation of experimental data. In the
sciences, interpolation is essential for the analysis of scientific measurements, the fitting of
theoretical models to empirical data, and the visualization of complex phenomena. In finance and
economics, interpolation is employed for the valuation of financial instruments, the forecasting
of market trends, and the optimization of investment strategies.

One particularly important application of interpolation is in the field of numerical analysis,


where it is used to approximate the values of functions that are difficult or impossible to compute
directly. By constructing an interpolating function, we can obtain accurate estimates of the
function's values at any point within the range of the data, allowing for more efficient and
reliable numerical computations.

As an undergraduate mathematics student, I have found the study of interpolation methods to be


both fascinating and relevant to a wide range of real-world problems. By understanding the
underlying principles and the practical considerations of these techniques, I believe I can
contribute to the advancement of knowledge and the development of innovative solutions in a
variety of fields. The insights gained from this project will undoubtedly prove invaluable as I
continue my academic and professional pursuits in the years to come.

1.1. DEFINITION
Spline interpolation is a technique used to construct a smooth, piecewise polynomial function
that passes through a set of given data points. The polynomial function is divided into multiple
segments, each of which is a low-degree polynomial (typically a cubic polynomial) that connects
two adjacent data points. These individual polynomial segments are referred to as "splines."

The advantage of using spline interpolation over other interpolation methods, such as linear or
polynomial interpolation, is its ability to provide a smooth and continuous function that closely
approximates the underlying data. Linear interpolation, for example, can result in a piecewise
linear function that may not capture the nuances of the data, while higher-order polynomial
interpolation can lead to unwanted oscillations, particularly when dealing with large datasets.

2|Page
Spline interpolation overcomes these limitations by using a series of lower-degree polynomials,
which results in a more flexible and accurate representation of the data. The continuity of the
first and second derivatives across the spline segments ensures that the resulting function is
smooth and has a continuous curvature, making it particularly useful in applications where the
smoothness of the interpolated function is crucial, such as in computer graphics, engineering, and
scientific modeling.

To illustrate the concept of spline interpolation, consider the following example:

Suppose you have a set of data points representing the temperature measurements at various
locations along a coastline. You want to use interpolation to estimate the temperature at
intermediate points where measurements were not taken.

Graph 1: linear, quadratic and cubic


interpolation

The blue dots represent the given data points, while the lines represent the interpolated functions.
As you can see, the linear interpolation (red line) provides a piecewise linear approximation,
which may not capture the subtle changes in temperature along the coastline. The polynomial
interpolation (green line) exhibits unwanted oscillations, which can be problematic when dealing
with noisy or irregular data.

In contrast, the spline interpolation (blue line) provides a smooth, continuous function that
closely follows the underlying data. The spline curve is composed of multiple cubic polynomial
segments that seamlessly connect the data points, ensuring a smooth transition between them.
The advantage of spline interpolation is that it can accurately represent the underlying function
3|Page
while maintaining a high degree of smoothness, making it a valuable tool in various scientific
and engineering applications where the continuity and curvature of the interpolated function are
crucial.

1.2. Statement of the problem


In this study, liner and cubic spline are approached in order to overcome the weakness of
polynomials which are having strongly oscillating properties and consist of a high number of
arithmetical operations involved in the calculations of the polynomials. In the mathematical field
more of problems are solve analytical but some problems are not solved analytically. They are
solved numerically Meanwhile; the accuracy of liner and cubic spline interpolation are
determined to compare with each other.

1.3. Objective
1.3.1. General objective

 To solve problems by using spline method

1.3.2. Specific objective

 By using liner interpolation solve problems solve problems in hand and in python

 By using quadratic interpolation solve problems in hand and in python

 By using cubic interpolation solve problems in hand and in python

1.4. SCOPE
The scope of this project starts from defining and looking at the theories of spline interpolation and
working on some problems to illustrate the use of spline interpolation in addition it will look at some of
the applications of spline interpolation in particular cubic spline interpolation in the application area of
curve fitting and data interpretation.

1.5. Delimitation of the project


Under this project in particular the application part it focuses only on cubic spline and its application.
Due to the time limitation the project will be doing fewer illustrating problems than usual.

4|Page
UNIT TWO

2. Review Literature
2.1. Introduction
Spline interpolation is a form of interpolation where the interpolate is a special type of piecewise
polynomial called spline. Spline interpolation is often preferred over polynomial interpolation because
the interpolation error can be made small even when using low degree polynomials for the spline
(Hazewinkel, Michiel, 2001)

2.2. Curve fitting


Curve fitting is the process of finding a curve that could best fit a given set of data. There are
two approaches of fitting a curve from a set of data points. The first approach called collocation
is the case where the curve is made to pass through all data points. This approach is used either
when the data is known to be accurate or the data are generated from the evaluation of some
complicated function at discrete set of points. Such function could be polynomial, trigonometric
or exponential functions. The second approach is when a given curve is made to represent the
general trend of the data. This approach is useful when there are more data points than the
number of unknown coefficient or when the data appear to have a significant error or noise
[Singiresu, 2002].
According to Singiresu (2002), the errors of a single polynomial tend to increase drastically as
its order 𝑛 becomes large. The higher order polynomial often introduces unnecessary oscillation
and wiggles and due to this, polynomial interpolation will not be always accurate. To avoid this,
information from more data points will be used and at the same time keeping the function true to
the data behavior is the objective of spline interpolation. The most common spline and piecewise
interpolation used are linear, quadratic and cubic respectively. To obtain a smoother curve, cubic
splines are frequently recommended, because they provide the simplest representation that
exhibits the desired appearance of smoothness. They are generally well behaved and continuous
up to the second order derivative at the data points. Even though cubic splines are less prone to
oscillation or overshooting due to instability inherent in higher order polynomial than global
polynomial equations, they do not prevent it. Thus, to avoid these oscillations, it is common to
divide the interval into sub-interval and approximate the function using low degree polynomial
on each sub-interval [Kruger, n.d].
5|Page
2.3. Interpolation
Interpolation is an estimation of a value within two known values. It is an estimation of an
unknown quantity between two known quantities or drawing conclusions about missing
information from the available one. Interpolation is useful where the data surrounding missing
data is available and where longer-term cycles are known. Interpolation is a method of building a
function that crosses through a discrete set of known data points.

2.4. Linear Interpolation


Linear interpolation has been used for filling the gaps in tables, often with astronomical data. It is
believed that it was used by Babylonian astronomers, by mathematicians in Seleucid
Mesopotamia, and by the Greek astronomer and mathematician, Hipparchus. A description of
linear interpolation can also be found in the Almagest by Ptolemy. The linear splines are much
simpler than cubic splines. The result of linear interpolation is a set of straight lines connecting
the data points. For two spatial dimensions, the extension of linear interpolation is called
bilinear interpolation. In three dimensions it is called tri-linear interpolation. (Carpenter K. H.
1999)
This method is often used in chemical engineering since the resulting spline never overshoots.
Even so, when a smooth the fundamental operation of linear interpolation between two values is
widely used in computer graphics, it is sometime called as a lerp. Lerp operations are constructed
on the hardware interpolate is desired, a higher order spline is needed. of all modern computer
graphics processors. They are often used as constructing blocks for more complex operations, for
example, a bilinear interpolation can be accomplished in three lerp. Because this operation is not
expensive, it’s also a good way to implement accurate lookup tables with quick look up for
smooth functions without having too many table entries. Linear spline models are criticized
because of the abrupt change in trend going from one segment to the next, which does not
represent what would naturally occur. The true change in trend should be smooth. In
mathematical terms this means that the fitting function should have a continuous derivative at
each value of the independent variable. This is not possible for linear splines, but it is possible
for quadratic splines that are joined segments of parabolas. [Z. Huang and J. Stone, 1999]

2.5. Quadratic Spline


A quadratic spline interpolation method produces a better ft to a continuous function than a
linear spline does. A quadratic spline is a continuously differentiable piecewise quadratic
6|Page
function, where quadratic includes all linear combinations of the basic monomials. Quadratic
splines are not used in applications as much as natural cubic splines. However, the derivations of
interpolating quadratic and cubic splines are similar enough, so that an understanding of the
simpler 2nd-degree spline theory allows us to grasp easily the more complicated third-degree
spline theory. (Cheney W. and Kincaid D.)

2.6. Cubic Spline


Cubic splines play an important role in fields where smooth interpolation is essential, in
modeling, for example, animation and image scaling. In computer graphics interpolating cubic
splines are often used to define smooth motion of objects and cameras passing through user-
specified positions in a key frame animation system. In image processing splines prove useful in
implementing high-quality image magnification.

2.7. Application of Cubic Spline Interpolation


The main application of cubic spline interpolation techniques is, of course, curve fitting. To this
end, the consistency and efficiency of the spline as a data correlation tool will be demonstrated.

Orthodontic teeth movement is influenced by the characteristics of the applied force, including
its magnitude and direction and the physiological condition of the periodontal tissue of
individual patients. However, researches carry out on teeth treatments in clinics indicated no sign
of application of this mechanical theory. What really happens is that dentists compare the actual
teeth conformation and the perfect one to confer the braces. In this study, analysis of dental
arches conformation could be done through cubic spline method. (Rokiah Rozita Ahmad,Najah
Ghazali, 2012)

This paper aims to explore a new technique for structural damage identification using cubic
spline interpolation. The method is based on the interpolation of modal rotations measured with
shearography, making use of the analytical derivative of the spline to compute the modal
curvature, which is known to be very sensitive to damage. As a means of reducing noise and
measurement uncertainty propagation to a minimum, an expression for an optimal spatial
sampling is derived. Furthermore, a baseline-free damage factor, allied with an optimal
sampling, is also introduced. The proposed identification method is validated using experimental
data of a beam. Using a damage localization quality index, a comparison between the present

7|Page
method and one using finite deference is carried out, showing that the differentiation of spline
interpolation leads to better damage identifications. (L. Pedroso1 | A. Arco, 2021)

UNIT THREE

3. ANALYSIS
3.1. Linear Spline Interpolation
This often leads to “knots”/ sharp changes in our function which is unideal as we want smooth
and continuous functions.
How do go about finding p1(x) and the point along it?
We assume the function is linear then each segment is simply a straight-line equation.
Recall: the general equation of a straight line is y=mx+b

Then p1 ( x ) = y 1 +
( y 2− y 1
x 2−x 1 )( x−x 1 ) ; x 1 ≤ x ≤ x 2------------------ Equation 1

p2 ( x ) = y 2 +
( y 3− y 2
x 3−x 2 )( x−x 2 ) ; x2 ≤ x ≤ x3------------------Equation 2

Note: at x=x 2 our p1 ( x )= p2 ( x )


Illustrating Example
Using linear spline interpolation function find:
A. The necessary interpolating functions
B. The output at x=2 , 5∧10 P5(x)

X 1 6 7 9 12 20
Y 2 8 6 10 14 41 P2(x)
P1(x)
P4(x)
Using equation 1 P3(x)

Graph 2: linear interpolation

p1 ( x ) = y 0 +
( y 2− y 1
x 2−x 1 )
( x−x 1 ) ;( x ¿¿ 1=1)≤ x ≤ (x¿ ¿2=6)¿¿

8|Page
¿ 1.2 x+0.8−−−−−−−−−−−[1]

p4 ( x ) = y 4 +
( y 5− y 4
x 5−x 4 )( x−x 3 ) ; (x ¿¿ 4=9)≤ x ≤(x ¿¿ 5=12)¿ ¿

4x
¿ −2−−−−−−−−−−−−−−¿[2]
3
For B substituting 2 and 5 in [1] because the domain D :1≤ x ≤ 6
 The value of y at x=2 can be given y=1.2 ( 2 ) +0.8=3.2 and
 The value of y at x=5 can be given y=1.2 ( 5 ) +0.8=6.8

For x= 10 we use [2] because the domain D :9 ≤ x ≤12

4 ( 5) 14
 The value of y at x=10 can be given y= −2=
3 3

Piecewise liner interpolation


:- is spline function must satisfy φ ( x ) of a digree n must satisfy the following 2 conditions
1. φ ( x ) is in sub interval [ xi , xi +1 ]
2. φ ( x )and its derivative are continous in range [ x o , x n ]
x−x i x−x i−1
p1 ( x ) = f ( x i−1 ) + f ( x i)
x i−1−x i xi −xi−1

Problem 1: - state whether the following piecewise polynomials are splines or not

{
X +1−1 ≤ x ≤ 0
f ( x )=¿ 2 x +1 0 ≤ x ≤1
4 – x1≤ x≤2
Solution: - x o=−1 , x 1=0, x 2=1, x 3=2then n=4
f 1 ( x) = x +1
f 2 (x) = 2 x+1
f 3 ( x) = 4−x
Then f 1 (x1 ) = 0+1 ¿1
f 2 ( x1 ) = 2(0)+1 ¿1
f 2 (x 2) = 2(1)+1 ¿3
f 3 ( x 2) = 4−1=¿ 3

9|Page
Therefore f 1 ( x 1 ) = f 2 (x1 ) and f 2 (x 2) = f 3 (x 2)
Its liner spline function

10 | P a g e
Problem 2
Obtain the piecewise liner interpolating polynomial for the function f (x)defined by the data

x 1 2 4 8

y 3 7 21 73

Hence estimate the value of f (3) and f (7)


Solution
x−x 1 x−x 0
p1 ( x ) = f ( xo)+ f (x 1 )
x 0−x 1 x 1−x o
x−2 x−1
p1 ( x ) = (3)+ (7)
1−2 2−1
p1 ( x ) =−3 x+6 +7 x−7
p1 ( x ) =4 x−1
In the interval [ 2 , 4 ] we have
x−x 2 x−x 1
p2 ( x ) = f ( x1 )+ f (x 2 )
x 1−x 2 x 2−x 1
x−4 x−1
p2 ( x ) = ( 7) + (21)
2−4 4−2
p2 ( x ) =7 x−7
In the interval [ 4 ,8 ] we have
x−x 3 x−x 2
p3 ( x ) = f ( x 2) + f (x 3)
x 2−x 3 x 3−x 2
x −8 x−4
p3 ( x ) = ( 21 )+ (73)
4−8 8−4
p3 ( x )=13 x−31
Hence the piecewise interpolation polynomials are

{
4 x −11 ≤ x ≤ 2
p ( x )= 7 x−7 2≤ x ≤ 4
13 x−31 4 ≤ x ≤ 8
3 ∈[ 2 , 4 ]

11 | P a g e
f ( 3 )=7 ( 3 )−7
f ( 3 ) = 21
7 ∈ [ 4 , 8] ,
f ( 7 )=13 ( 7 ) −31 f ( 7 )=60

Issues with linear spline


1. Continuity: Linear splines only provide continuous functions, but not continuous
derivatives. This can be a problem if you need smooth, differentiable functions.

2. Inflection points: With linear splines, any change in slope occurs at the knot points. This
means there will be sharp corners or inflection points at the knot locations, which may
not be desirable in some applications.

3. Approximation accuracy: Linear splines are generally not as accurate as higher-order


spline functions (e.g. cubic splines) for approximating smooth, curved functions. The
piecewise linear segments can only provide a rough approximation, especially between
knot points.

4. Sensitivity to knot placement: The quality of the linear spline approximation is highly
dependent on where the knot points are placed. Choosing an appropriate knot spacing is
important to balance accuracy and computational cost.

5. Limited flexibility: Compared to higher-order splines, linear splines have less flexibility
in shape and cannot capture more complex functional forms as effectively.

6. Potential oscillations: In some cases, linear splines can exhibit unwanted oscillatory
behavior, particularly near regions with abrupt changes in the underlying function.

12 | P a g e
3.2. Quadratic Spline Interpolation
quadratic spline interpolation method is a local interpolation method which uses the quadratic
polynomial to approximate the required data from the sub sets of the total data set. With global
interpolation method such as newton’s divided difference interpolation data points supplied to us
which can create very high order polynomials whereas spline interpolation can use only a sub set
of the total data.

Solving For a Quadratic Interpolation

General 2nd order polynomial

2
pi ( x ) =ai x + bi x +c i

We have 3 unknowns for every single spline


generates. Let n be the number of spline then the
number of unknowns is 3n.

How do we find 3n equations?

Let us see for 2 splines 3*n = 6 unknowns Figure 2: showing quadratic spline
2
p1 ( x ) =a1 x +b 1 x +c 1

. p2 ( x ) =a2 x 2+b 2 x +c 2

1) Evaluate at known points


a) We know at p1 ( x1 ) = y 1 and p1 ( x2 ) = y 2 gives 2 equations
b) Likewise, p2 ( x 2) = y 2 and p2 ( x3 ) = y 3 gives 2 more equations

This gives 2 equations per spline therefore 2n equations.

2) Smoothness at interior points

For the system to be smooth the interior data points must be equal

dp1 ( x 2) dp 2 ( x 2 )
=
dx dx

This must be true for every interior data point which will give us n−1 equations

13 | P a g e
2 n+n−1=3 n−1

We are missing only 1 equation

3) Assumption

Assumptions is necessary to achieve 0 degree of freedom.

Commonly we assume f
''
( p1 ( x ) ) =0and this will be our final missing equation.

Illustrating example

a. find all relevant interpolating functions between the following data points using quadratic
spline interpolation.
b. Find outputs at x= 2,4 and 7.

X 1 3 5 8
Y 2 3 9 10 p3 ( x )
n-spline

we have 3 splines therefore we will have


3*3 = 9 unknowns meaning we need 9
equations. p1 ( x)
p2 ( x )
Steps in solving quadratic spline

1) Write the general polynomials


2) Identify unknowns
3) Solve unknowns
4) Plug in x-input

Polynomial to find

. p1 ( x ) =a1 x2 +b 1 x +c 1

. p2 ( x ) =a2 x 2+b 2 x +c 2

. p3 ( x )=a3 x 2+ b3 x + c3

14 | P a g e
9 unknowns: a 1 , b1 , c 1 , a 2 , b2 , c 2 , a 3 , b3 , c 3

p1 ( x1 ) = y 1=2=a1 (1 )2 +b 1 ( 1 ) +c 1

2=a1 +b1 +c 1−−−−−−−−[1]

2
p1 ( x2 ) = y 2=3=a1 ( 3 ) +b1 ( 3 ) +c 1

3=9 a1 +3 b1 +c 1−−−−−−−−[2]

p2 ( x 2) = y 2=3=a2 ( 3 )2 +b2 ( 3 ) +c 2

3=9 a2 +3 b2 +c 2−−−−−−−−[3]

2
p2 ( x3 ) = y 3=9=a 2 ( 5 ) +b 2 ( 5 ) + c2

9=25 a2 +5 b2 +c 2−−−−−−−−[4]

p3 ( x 3 )= y 3=9=a 3 ( 5 )2 +b 3 ( 5 )+ c3

9=25 a3 +5 b3 + c3 −−−−−−−−[5 ]

2
p3 ( x 4 )= y 4=10=a3 ( 8 ) +b 3 ( 8 )+ c 3

10=64 a3 +8 b 3 +c 3−−−−−−−−[6]

dp1 ( x 2) dp 2 ( x 2 )
Smoothness =
dx dx

d 2 d 2
(a1 x +b 1 x +c 1)= (a ¿ ¿2 x +b2 x+ c 2)¿
dx dx

2 a1 x2 +b 1=2 a2 x2 +b 2 x 2=3

6 a 1+ b1−6 a 2−b2 =0−−−−−−−−[7 ]

dp2 ( x 3 ) dp3 ( x3 )
=
dx dx

15 | P a g e
d 2 d 2
(a ¿ ¿ 2 x +b 2 x +c 2)= (a3 x +b3 x +c 3)¿
dx dx

2 a2 x3 + b2=2 a3 x 3+ b3 x3 =5

10 a2 +b 2−10 a3−b3=0−−−−−−−−[8]

2
d
Assume: f
''
( p1 ( x ) ) =0 2
¿
dx

d d
(
dx dx
( ) d
a1 x 2+ b1 x +c 1 ) = ( 2 a 1 x +b1 ) =2 a1=0
dx

2 a1=0−−−−−−−−−−−−−−[9]

Now we have 9 equations and 9 unknows so we can solve this with gauss elimination solver with
partial pivoting on python. 2=a1 +b1 +c 1

3=9 a1 +3 b1 +c 1

3=9 a2 +3 b2 +c 2

9=25 a2 +5 b2 +c 2

9=25 a3 +5 b3 + c3

10=64 a3 +8 b 3 +c 3

0=6 a1 +b 1−6 a2 −b2

0=10 a2 +b2 −10 a3−b 3

0=2 a1

We get: a1 = 0 b1 = 0.5 c1 = 1.5

a2 = 1.25 b2 = -7 c2 = 12.75

a3 = -1.72 b3 = 22.72 c3 = -61.56

2
p1 ( x ) =0 x +0.5 x +1.5 D :1≤ x ≤ 3

2
p2 ( x ) =1.25 x −7 x +12.75 D :3 ≤ x ≤5
16 | P a g e
2
p3 ( x )=−1.72 x +22.72 x−61.56 D :5 ≤ x ≤8

p1 ( 2 )=0 x + 0.5(2)+ 1.5 = y 2=2.5


2

p2 ( 4 )=1.25(4) −7 (4)+12.75 = y 4 =4.75


2

p3 ( x )=−1.72(7) +22.72(7)−61.56 = y 7=13.15


2

Problem 2: - state whether the following piecewise polynomials is 2nd degree spline function or
not

{
2
x +1 0≤ x ≤ 1
f ( x )=¿ 2
2 x 1 ≤ x ≤2
5 x – 2 2 ≤ x ≤3

Solution:- x o=0 , x 1=1 , x 2=2 , x 3=3 then n=4

'
f 1 (x) = x 2+ 1 f 1 ( x )=2 x

'
f 2( x ) = 2 x2 f 2 ( x )=4 x

'
f 3 (x) = 5 x – 2 f 2 ( x )=5

Then f 1 ( x 1 )=1+1=2
'
f 1 ( x 1 )=2

f 2 ( x 1 ) = 2(1)2=2 '
f 2 ( x 1 )=4

f 2 ( x 2 ) = 2(2)2 =8 '
f 2 ( x 2 )=4 ( 2 ) =8

f 3 ( x 2 ) = 5 ( 2 ) – 2=8 '
f 2 ( x 2 )=5

The polynomial is continuous but their derivative is not continuous than it is not 2nd degree spline
function

Problem 3: - state whether the following piecewise polynomials is 2nd degree spline function or

{
x 0≤ x≤1
not f ( x )=¿ 2
x −x+1 1 ≤ x ≤2
3 x – 3 2≤ x ≤ 3

Solution: - x o=0, x 1=1, x 2=2, x 3=3 then n=4

17 | P a g e
'
f 1 ( x) = x f 1 ( x )=1

'
f 2 ( x ) = x 2−x +1 f 2 ( x )=2 x−1

'
f 3 ( x) = 3 x – 3 f 2 ( x )=3

'
f 1 ( x1 ) = 1 f 1 ( x 1 )=1

f 2 ( x1 ) = 1 '
f 2 ( x 1 )=1

'
f 3 ( x 2) = 3 f 2 ( x 2 )=3

The first derivative is continuous and it is 2nd degree spline

But

} left (x right ) =¿
f1

} left (x right ) =¿
f2

} left (x right ) =¿
f3

The second derivation is not continuous and it not 3rd degree spline function

Then the formula of liner spline method is

x−x i x−x i−1


p1 ( x ) = f ( x i−1 ) + f ( x i)
x i−1−x i xi −xi−1

Problem 4 :- Approximation a function f ( x )=4 x on [-1,1] by liner spline method and find the
value of f ( 0.125 )

p4

p3

p2

18 | P a g e
p1

-1 -0.5 0 0.5 1

p1 ∈ [−1 ,−0.5] ; p2 ∈[−0.5 , 0] ; p3 ∈[0 , 0.5] ; p4 ∈[0.5 , 1]

x o=−1 x 1=−0.5 , x 2=0 x 3=0.5 x 4 =1

x -1 -0.5 0 0.5 1

y 0.25 0.5 1 2 4

{
x−(−1)
y 1= [ 0.5−0.25 ] +0.25=0.5 x+0.75 if x ∈ [ −1 ,−0.5 ]
−0.5−(−1)
x +0.5
y 2= [ 1−0.5 ] +0.5=x+ 1if x ∈ [ −0.5 , 0 ]
f ( x )= 0+0.5
x−0
y 3= [ 2−1 ] +1=2 x +1 if x ∈ [ 0 ,0.5 ]
0.5−0
x−0.5
y 4= [ 4−2 ] + 2=4 x if x ∈ [ 0.5 , 1 ]
1−0.5

if x=0.125 then x ∈ [ 0 , 0.5 ] Which means? 0.125 ∈ [ 0 , 0.5 ]

y ( 0.125 )=2× 0.125+1=1.25 Therefore y ( 0.125 )=1.25 , but 4 0.125 ≈1.192071

3.3. Cubic spline interpolation


A powerful technique used to approximate a smooth curve that passes through a set of data
points.

Similar to linear and quadratic spline interpolation it breaks down the data range in to smaller
segments. However, we will now be going to fit a cubic polynomial to each section and this
ensures the continuity and smoothness at
the connection points which something we
were lucking in both linear and quadratic
splines.

19 | P a g e
p2 ( x )

p1 (x)

xa xb xc
Graph 3: cubic interpolation graph

Splines create a piecewise function

{
f ( x )= p1 ( x ) x a ≤ x ≤ x b
p2 ( x ) x b ≤ x ≤ x c

In cubic spline interpolation p1 ( x ) ∧p 2 (x) are found through general 3rd order polynomial

Cubic/3rd order polynomial: a x 3 +b x 2+ cx +d

Benefits of cubic spline interpolation

 Typically, good function fit.


 Smoothness and continuity.
 Reduce oscillation in certain condition.

Solving for cubic interpolation

Each pn ( x ) will be a 3rd order polynomial


3 2
p1 ( x ) =a1 x +b 1 x +c 1 x +d 1
3 2
p2 ( x ) =a2 x + b2 x +c 2 x +d 2
n = number of interpolating spline (always 1 less than the number of data points being
used)

20 | P a g e
steps in solving cubic spline interpolation

solve using a linear


find our number of find 4n worth of
system of equation
unknown cofficent equations
solver!

1) Evaluate at known points

For the above example in graph 3 we have p1 ( x1 ) = y 1 p1 ( x2 ) = y 2


Get 2n
equations
p2 ( x 2 ) = y 2 , p2 ( x3 ) = y 3

2) Smoothness at interior points


Get n-1
' '
p1 ( x2 ) = p2 (x 2) equations

3) 2nd derivatives are also equal at interior points

'' '' Get n-1


p1 ( x 2 )= p2 (x 2)
equations

4) Make some assumption about our end points.


 Natural cubic spline: assume the second derivatives at Get 2
'' ''
equations
end points are zero Assume p1 ( x 1 )=0∧ p2 ( x 3 )=0
 Clamped cubic spline: requires some extra data where we specify slopes at
the end points.

Natural cubic spline interpolation example


I) Find all relevant interpolating
functions between the following data
II) By using the interpolating cubic
function find the output at
x=1.5 , x=4∧x=7

21 | P a g e
X 1 3 5 8
Y 2 3 9 10
3 2
p1 ( x ) =a1 x +b 1 x +c 1 x +d 1
3 2
p2 ( x ) =a2 x + b2 x +c 2 x +d 2
3 2
p3 ( x )=a3 x + b3 x +c 3 x+ d 3

We have 4 data pints thus we get 3 splines which implies 4*3 = 12 unknowns so we need 12
equations for the degree of freedom to be zero
p1 (x)
1) Evaluate at known points p2 (x )

3 2
(x 1 , y 1 ) 3 (x 2 ,2 y 2 )
p1 ( x1 ) = y 1=a 1 x + b1 x +c 1 x+ d 1 p1 ( x2 ) = y 2=a 1 x +b1 x + c1 x+ d 1

3 2 3 2
p1 (1 )=2=a1 ( 1 ) +b 1 ( 1 ) +c 1 ( 1 ) + d 1 ; p1 (3 )=3=a1 ( 3 ) + b1 ( 3 ) + c 1 ( 3 ) + d1

1=a1 +b1 +c 1 +d 1−−−−−−( 1 ) ; 3=27 a1 +9 b 1+ 3 c1 +d 1−−−−−−( 2 )

3 2 3 2
p2 ( x 2) = y 2=a 2 x +b2 x + c2 x+ d 2 ; p2 ( x 3 )= y 3=a2 x +b 2 x +c 2 x +d 2

3 2 3 2
p2 ( 3 )=3=a2 (3) +b 2(3) +c 2 ( 3 ) +d 2 ; p2 ( 5 )=9=a2 (5) + b2 (5) + c 2(5)+d 2

3=27 a2 +9 b 2+3 c 2 +d 2−−−−( 3 ) ; 9=125 a 2+ 25 b2 +5 c 2+ d 2−−−−(4)

3 2 3 2
p3 ( x 3 )= y 3=a3 x +b 3 x +c 3 x +d 3 ; p 3 ( x 4 )= y 4=a 3 x +b3 x + c 3 x +d 3

3 2 3 2
p3 ( 5 )=9=a3 (5) +b 3 (5) + c3 ( 5 ) +d 3 ; p 3 ( 8 )=10=a3 (8) +b 3 (8) + c3 (8)+ d 3

9=125 a3 +25 b3 +5 c 3 +d 3−−−−( 5 ) ; 10=512 a3 +64 b3 +8 c 3 +d 3−−−−(6)

2) Smoothness at interior points

' '
p1 ( x2 ) = p2 (x 2) '
P1 ( x ) =3 a1 x +2 b1 x+ c1
2

' 2
P2 ( x ) =3 a2 x +2 b2 x+ c 2

' ' ' ' 2 2


p1 ( x2 ) = p2 ( x 2 ) =p 1 ( 3 )=p 2 ( 3 )=3 a1 ( 3 ) +2 b1 (3)+ c 1=3 a2 ( 3 ) +2 b2 (3)+ c 2

2 7 a1 +6 b 1+ c1 −27 a2−6 b2−c 2=0−−−−−−−(7)

22 | P a g e
' '
p2 ( x3 ) = p3 (x 3)

' 2
P2 ( x ) =3 a2 x +2 b2 x+ c 2

' 2
P3 ( x )=3 a3 x +2 b3 x +c 3

p'2 ( x3 ) = p'3 ( x3 ) = p'2 (5 )= p'3 ( 5 )=3 a 2 ( 5 )2 +2 b2 (5)+c 2=3 a 3 ( 5 )2 +2 b3 (5)+c 3

75 a2 +10 b 2+ c2 −75 a1−10 b1 +c 1=0−−−−−−−(8)

'' ''
p1 ( x 2 )= p2 (x 2)

''
P1 ( x )=6 a1 x +2 b1

''
P2 ( x )=6 a2 x +2 b2

p'1' ( x 2 )= p'2' ( x 2 ) =p ''1 ( 3 )= p'2' ( 3 )=6 a1 ( 3 )1 +2 b1=6 a2 ( 3 )1+2 b 2

18 a1 +2 b1−18 a2−2 b2=0−−−−−−−(9)

'' '
p2 ( x 3 )= p ' 3 (x3 ) ''
P2 ( x )=6 a2 x +2 b2

''
P3 ( x )=6 a3 x +2 b3

p'2' ( x 3 )= p'3' ( x3 ) = p'2' ( 5 )= p'3' ( 5 )=6 a 2 ( 5 )1 +2 b2 c 2=6 a3 ( 5 )❑+2 b3

30 a2 +10 b 2−30 a1−10 b 1=0−−−−−−−(10)

3) Assumption for natural cubic spline

'' ''
assume the second derivatives at end points are zero Assume p1 ( x 1 )=0∧ p3 ( x 4 ) =0

''
P1 ( 1 )=6 a1 (1 )+2 b 1=6 a1 + s b1 =0−−−−−−−−−−(11)

''
P3 ( 8 )=6 a3 ( 8 ) +2 b3 =48 a 3+ 2b 3=0−−−−−−−−−(12)

Know we find all 12 of the equations we can find our 12 unknowns by using the system of
equation solver we use in for quadratic spline illustrative example*gauss elimination method
with partial pivoting* on python.
23 | P a g e
We find the values to be as follows

a 1=0.1995 b1=0.5986 c 1=0.3004 d 1=2.0986

a 2=−0.3728 b2 =4.5526 c 2=−15.153 d 2=17.5526

a 3=0.11549b 3=−2.7719 c 3=21.4692 d 3=−43.4853

The spline functions will become as follows

3 2
p1 ( x ) =0.1995 x + 0.5986 x +0.3004 x +2.0986 D :1≤ x ≤ 3

3 2
p2 ( x ) =−0.3728 x + 4.5526 x +−15.153 x +17.5526 D :3 ≤ x ≤5

3 2
p3 ( x )=0.11549 x +−2.7719 x +21.4692 x +−43.4853 D :5 ≤ x ≤8

By using the interpolating cubic function find the output at x=1.5 , x=4∧x=¿ 7

3 2
x=1. 5 ; p 1 ( 1.5 ) =0.1995 ( 1.5 ) +0.5986 ( 1.5 ) +0.300(1.5)+2.0986

=1.875663

p(x)
12
10
8
6
4
2
0
1 2.7 4.4 6.17.80000000000001

3 2
x=4 ; p2 ( 4 )=−0.3728 ( 4 ) +4.5526 ( 4 ) +−15.153(4)+17.5526

=5.923

x=7 ;

3 2
p3 ( 7 )=0.11549 ( 7 ) −2.7719 ( 7 ) +21.4692 ( 7 )−43.485 3

24 | P a g e
=10.58767

Clamped cubic spline interpolation example


I) Find all relevant interpolating functions between the following data points using
dx
cubic splines. (clamped condition of =4 at x 1∧x 4 )
dy
II) By using the interpolating cubic function find the output at x=1.5 , x=4∧x=7

X 1 3 5 8
Y 2 3 9 10

3 2
p1 ( x ) =a1 x +b 1 x +c 1 x +d 1
3 2
p2 ( x ) =a2 x + b2 x +c 2 x +d 2
3 2
p3 ( x )=a3 x + b3 x +c 3 x+ d 3

We have 4 data pints thus we get 3 splines which implies 4*3 = 12 unknowns so we need 12
equations for the degree of freedom to be zero

4) Evaluate at known points

3 2 3 2
p1 ( x1 ) = y 1=a 1 x + b1 x +c 1 x+ d 1 ; p1 ( x2 ) = y 2=a 1 x +b1 x + c1 x+ d 1

3 2 3 2
p1 (1 )=2=a1 ( 1 ) +b 1 ( 1 ) +c 1 ( 1 ) + d 1 ; p1 (3 )=3=a1 ( 3 ) + b1 ( 3 ) + c 1 ( 3 ) + d1

1=a1 +b1 +c 1 +d 1−−−−−−( 1 ) ; 3=27 a1 +9 b 1+ 3 c1 +d 1−−−−−−( 2 )

3 2 3 2
p2 ( x 2) = y 2=a 2 x +b2 x + c2 x+ d 2 ; p2 ( x 3 )= y 3=a2 x +b 2 x +c 2 x +d 2

3 2 3 2
p2 ( 3 )=3=a2 (3) +b 2(3) +c 2 ( 3 ) +d 2 ; p2 ( 5 )=9=a2 (5) + b2 (5) + c 2(5)+d 2

3=27 a2 +9 b 2+3 c 2 +d 2−−−−( 3 ) ; 9=125 a 2+ 25 b2 +5 c 2+ d 2−−−−(4)

3 2 3 2
p3 ( x 3 )= y 3=a3 x +b 3 x +c 3 x +d 3 ; p 3 ( x 4 )= y 4=a 3 x +b3 x + c 3 x +d 3

3 2 3 2
p3 ( 5 )=9=a3 (5) +b 3 (5) + c3 ( 5 ) +d 3 ; p 3 ( 8 )=10=a3 (8) +b 3 (8) + c3 (8)+ d 3

25 | P a g e
9=125 a3 +25 b3 +5 c 3 +d 3−−−−( 5 ) ; 10=512 a3 +64 b3 +8 c 3 +d 3−−−−(6)

5) Smoothness at interior points

' '
p1 ( x2 ) = p2 (x 2) ' 2
P1 ( x ) =3 a1 x +2 b1 x+ c1

' 2
P2 ( x ) =3 a2 x +2 b2 x+ c 2

p'1 ( x2 ) = p'2 ( x 2 ) =p '1 ( 3 )=p '2 ( 3 )=3 a1 ( 3 )2 +2 b1 (3)+ c 1=3 a2 ( 3 )2 +2 b2 (3)+ c 2

2 7 a1 +6 b 1+ c1 −27 a2−6 b2−c 2=0−−−−−−−(7)

' '
p2 ( x3 ) = p3 (x 3) '
P2 ( x ) =3 a2 x +2 b2 x+ c 2
2

' 2
P3 ( x )=3 a3 x +2 b3 x +c 3

p'2 ( x3 ) = p'3 ( x3 ) = p'2 (5 )= p'3 ( 5 )=3 a 2 ( 5 )2 +2 b2 (5)+c 2=3 a 3 ( 5 )2 +2 b3 (5)+c 3

75 a2 +10 b 2+ c2 −75 a1−10 b1 +c 1=0−−−−−−−(8)

'' ''
p1 ( x 2 )= p2 (x 2) ''
P1 ( x )=6 a1 x +2 b1

''
P2 ( x )=6 a2 x +2 b2

p'1' ( x 2 )= p'2' ( x 2 ) =p ''1 ( 3 )= p'2' ( 3 )=6 a1 ( 3 )1 +2 b1=6 a2 ( 3 )1+2 b 2

18 a1 +2 b1−18 a2−2 b2=0−−−−−−−(9)

'' '
p2 ( x 3 )= p ' 3 (x3 ) ''
P2 ( x )=6 a2 x +2 b2

''
P3 ( x )=6 a3 x +2 b3

p'2' ( x 3 )= p'3' ( x3 ) = p'2' ( 5 )= p'3' ( 5 )=6 a 2 ( 5 )1 +2 b2=6 a3 (5 )❑+ 2b 3

30 a2 +2 b2 −30 a1−2 b1=0−−−−−−−(10)

6) Assumption for clamped cubic spline

' '
assume the second derivatives at end points are zero Assume p1 ( x1 ) =4∧ p3 ( x 4 ) =4

' 2
P1 (1 )=3 a1 ( 1 ) + 2b 1 ( 1 ) +c 2=3 a 1+ 2b 1+ c2 =4−−−−−−−−−−(11)
26 | P a g e
' 2
P3 ( 8 )=3 a 3 ( 8 ) + 2b 3 ( 8 )+ c 3=192 a3 +16 b3 + c3=0−−−−−−−−−(12)

Know we find all 12 of the equations we can find our 12 unknowns by using the system of
equation solver we use in for quadratic spline illustrative example*gauss elimination method
with partial pivoting* on python.

We find the values to be as follows

a 1=1.0473 b1=−6.9864 c 1=14.831 d 1=−6.982

a 2=−0.7669 b2=9.3412 c2 =−34.152 d 2=42.0912

a 3=0.564 b 3=−10.623 c 3=65.6697 d3 =124.278

The spline functions will become as follows comparison

3 2
p1 ( x ) =1.0473 x −6.9864 x +14.831 x−6.982 D :1≤ x ≤ 3

3 2
p2 ( x ) =−0.7669 x + 9.3412 x −34.152 x+ 42.0912 D :3 ≤ x ≤5

3 2
p3 ( x )=0.564 x −10.623 x + 65.6697 x+124.278 D :5 ≤ x ≤8

clamped cubic spline comparison chart


12
12
10 10
8 8
6
6
4
4
2
2 0
1 2.5 4 5.5 7.00000000000001
0
1 2.4 3.8 5.2 6.6 8
natural cubic spline clamped cubic spline
Graph 4: clamped cubic spline Graph 5: comparison between natural and clamped spline

By using the interpolating cubic function find the output at x=1.5 , x=4∧x=¿ 7

3 2
x=1. 5 ; p 1 ( 1.5 ) =1.0473(1. 5) ±6.9864 ( 1. 5 ) + 14.831(1.5)±6.982

= 3.07973
27 | P a g e
3 2
x=4 ; p2 ( 4 )=−0.7669 ( 4 ) +9.3412 ( 4 ) −34.152( 4)+ 42.0912

=5.8608

3 2
x=7 ; p3 ( 7 )=0.564(7) −10.623 (7 ) + 65.6697(7)+124.278

= 8.3349

Another approach to using cubic spline


For a piecewise function to be a cubic spline it must satisfy the following conditions

1. f ( x ) must interpolate f at all points x o , x 1 , x 2 , … .. x n

f ( x i ) =f i

2. The function values must be equal to all the interior knots

f i (x i)=f i+1 (x i )

3. The first derivative at interior knots must be equals

' '
f i ( x i ) =f i +1 (xi )

4. The second derivative at interior knots must be equals

'' ''
f i ( x i )=f i+1 (x i)

5. The second derivative at the end point are zero

'' ''
f i ( x o )=f i ( x n )

Since f 1 ( x ) is cubic spline function in second derivative f ''1 (x 1)is a straight line. the street line
represented by first order Lagrange interpolating polynomial. since the line pass through the
point.

To construct cubic spline, we process as the following

Since f ( x ) is cubic polynomial and also we know Lagrange interpolating polynomial

28 | P a g e
( x−x 0 ) ( x−x 1 ) ( x−x 2 ) … … … … … ( x−x i−1) … … ( x −xn )
l i= ……………………..( 1 )
( x i−x 0 ) ( xi −x1 ) ( x i−x 2 ) … … … … … ( x i−x i−1 ) … … ( x i−x n )

Suppose f } left (x right ¿ is linear polynomial interpolation in the interval [ x i−1 , x i ]

} left (x right ) = {left ({x-x} rsub {i} right )} over {left ({{x} rsub {i-1} -x} rsub {i} right )} {f} ^ { ( x−x i−1 ) } left ({x} rsub {i} right ¿
f ( x i−1 )+ f
( x i−x i−1 )
………………………..( 2 )

Where hi =( x i−x i−1 ) and

Let f } left ({x} rsub {i-1} right ) = {m} rsub {i-1¿

} left ({x} rsub {i} right ) = {m} rsub {i ¿


f

'' x i−x x−x i−1


⟹ f i ( x )= mi−1+ mi
hi hi

1
''
⟹ f i ( x )= [ ( x −x ) mi−1 + ( x−x i−1 ) mi ] …………………………………….( 3 )
hi i

Integrate twice w.r.t x

[ ]
2 2
' 1 ( x i−x ) ( x −xi−1 )
f i ( x )= mi−1+ mi + ci
hi 2 2

[ ]
3 3
' 1 ( x i−x ) ( x −xi−1 )
f i ( x )= mi−1 + mi + ci x +d i
hi 6 6

1
f i ( x )=
6 hi
[ ( x i−x ) mi−1 + ( x−x i−1 ) mi ] + c i x +d i ……………………………….( 4 )
3 3

Consider

c i x + di =ai ( x i−x ) +bi ( x−x i−1 )

and substitute in equation 4

1
f i ( x )= [ ( x −x )3 mi−1 +( x−x i−1 )3 mi ] + ai ( xi −x )+ bi ( x−x i−1) …………( 5 )
6 hi i

29 | P a g e
Now replacing x by x i−1

1
f i−1= [ ( x −x )3 m +( x i−1−x i−1 )3 mi ] + ai ( x i−x i−1 ) +b i ( x i−1−x i−1 )
6 hi i i−1 i−1

1
f i−1=
6 hi
[ ( h i ) mi−1 ] +a i hi
3

2
hi
f i−1= mi−1 + ai h i
6

2
hi
a i hi =f i−1− m
6 i−1

[ ]
2
1 hi
a i= f i−1− mi−1 ………………………………..……..( 6 )
hi 6

[ ]
2
1 hi
Similarly b i= f i− mi ………………………………………..( 7 )
hi 6

Subsite 6 and 7 in equation 5

[ ] [ ]
2 2
1 1 hi 1 hi
f i ( x )=
6 hi
[ ( x i−x ) mi−1 + ( x−x i−1 ) mi ] + f i −1− mi−1 + f i− mi …………( 8 )
3 3
hi 6 hi 6

Differentiate 8 w r t x

[ ] [ ]
2 2
1 1 hi 1 hi
'
f ( x )=
6 hi
[ −3 ( xi −x ) mi+1 +3 ( x−x i−1 ) mi ] − f i−1− mi−1 + f i− mi
2 3
hi 6 hi 6

At x=x i in the above equation

[ ] [ ]
2 2
1 1 hi 1 hi
'
f ( x )=
6 hi
[ −3 ( xi −xi ) mi+1 +3 ( x i−x i−1 ) mi ] − f i−1 − mi−1 + f i− mi
2 3
hi 6 hi 6

[ ] [ ]
2 2
1 1 hi 1 hi
'
f ( x )=
6 hi
[ 3 h i mi ]− f i−1− mi−1 + f i− mi
2
hi 6 hi 6

' hi mi f i−1 hi f i hi
f ( x )= − + mi−1+ − m1
2 hi 6 hi 6

30 | P a g e
hi hi 1
mi + mi−1+ [ f i−f i−1 ] ……………………………( 9 )
'
f i ( x 1 )=
3 6 hi

In equation 8 replace i with i+ 1

[ ] [ ]
2 2
1 1 hi +1 1 h i+1
f i+1 ( x ) =
6 hi+1
[ ( x i +1−x ) mi +(x−x i) mi +1 ] +
3 3
hi+1
f i−
6
m i ( x i +1−x ) +
hi+1
f i+ 1−
6
mi+1 ( x−x i )

…………………………………………………( 10 )

Differentiate 10 w r t x

[ ] [ ]
2 2
1 1 hi+1 1 hi+1
'
f i+1 ( x ) =
6 hi+1
[−3 ( x i +1−x ) m i +3 ( x−x i ) mi +1 ]−
2 2
hi+ 1
f i−
6
mi +
hi+1
f i +1− m
6 i +1

At x=x iin the above equation

[ ] [ ]
2 2
1 1 hi +1 1 h i+1
'
f i+1 ( x ) =
6 hi+1
[−3 ( x i +1−x i) mi +3 ( x i−x i ) mi+1 ]−
2 2
hi+1
f i−
6
mi +
hi+1
f i+ 1−
6 i+1
m

[ ] [ ]
2 2
1 1 hi+1 1 hi +1
f
'
i+1 (x )=
6 hi+1
[−3 ( x i +1−x i) mi ]−
2
hi +1
f i−
6
mi +
hi +1
f i+1−
6 i+1
m

' −hi+1 f i hi+1 f i+1 hi +1


f i+1 ( x ) = mi− + mi + − m
2 hi +1 6 hi+ 1 6 i+1

−hi+1 mi hi +1 1
'
f i+1 ( x ) =
3
− m + [ f −f ] ……………………………………….( 11 )
6 i+1 hi+1 i+1 i

Now equating 9 and 11

' '
i.e. f i ( x i ) =f i +1 ( x i )

hi hi 1 −hi+ 1 mi hi+1 1
mi + mi−1+ [ f i−f i−1 ]= − m + [ f −f ]………………………….( 12 )
3 6 hi 3 6 i+1 hi+ 1 i +1 i

Equation 12 gives the system of (n−1) liner equation in (n+1) unknowns

m0 , m1 , m2 , m3 ,… … … ,mn

31 | P a g e
∴ Two additional conditions may be taken in one of the following forms

I. m0=mn =0 natural spline

II. m0=mn , m1=mn+1

Suppose that points are equ-spaced

hi =hi+1 =h

Equation 8 becomes

[ ] [ ]
2 2
1 1 h 1 h
f i ( x )=
6h
[ ( x i−x ) mi−1 + ( x−x i−1 ) mi ]+ f i−1− mi−1 ( x i−x ) + f i − mi ( x− xi−1 )
3 3

h 6 h 6
…………………………………………………………….( 13 )

Together with 12

hi hi 1 −hi+ 1 mi hi+1 1
mi + mi−1+ [ f i−f i−1 ]= − mi+1 + [ f −f ]
3 6 hi 3 6 hi+ 1 i +1 i

h h mi h h mi +1 1
mi + + mi−1 + = [ f i +1−f i −f i + f i−1 ]
3 3 6 6 h

2h h h 1
⟹ m + m + m = [ f −f −f +f ]
3 i 6 i−1 6 i +1 h i+1 i i i−1

6
⟹ 4 h mi +h mi−1 +h mi+1= [ f −2 f i + f i−1 ]
h i+ 1

6
2 [ i+1
⟹ 4 mi +mi−1 +mi+1= f −2 f i +f i −1 ] ……………………………………( 14 )
h

13 and 14 are required cubic spline interpolation

For equidistance knots it is possible to use system of matrix such that

32 | P a g e
( )( ) ( )
4 1 0 … 0 0 m1 d1
1 4 1 … 0 0 m2 d2
0 1 4 1 … 0 m3 d3
=
0 0 1 4 1 … . .
… … 0 1 4 1 . .
0 0 0 … 0 0 mi−1 di −1

Problem 6: - the following values of x and y are given

x: 1 2 3 4

y: 1 2 5 11

Find the cubic spline and y ( 1.5 ) and y ' (3 )

Solution h=1, n=3

mi−1+ 4 mi +mi+1=6 ( y i−1−2 y i+ y i+1 )

∴ mo+ 4 m1 +m2=6 ( y o−2 y 1+ y 2) =12

m1 +4 m2 +m3=6 ( y 1−2 y 2+ y 3 ) =18

mo=0 & m3=0 , i=1 , 2

4 m1+ m2=12

m1 +4 m2=18

Therefore m1=2 , m2=4

f ( x )=¿

11
y ( 1.5 )=f ( 1.5 )=
8

33 | P a g e
' 14
y (3 )= from both the spline interval [2,3] and [3,4] as they should be
3

Problem 7: - find the approximation function of y=e x in [-1,1]

x -1 -0.5 0 0.5 1

y 0.37 0.6 1 1.65 2.72

Solution

x 0=−1 ; x 1=−0.5 ; x 2=0 ; x 3=0.5 ; x 4 =1

y o =0.37 ; y 1=0 .6 ; y 2=1 ; y 3=1.65 ; y 4 =2.72

{
φ1 ( x ) if x ∈[−1 ,−0.5 ]
f ( x ) φ 2 ( x ) if x ∈[−0.5 , 0]
φ 3 ( x ) if x ∈[0 , 0.5]
φ 4 ( x ) if x ∈[0.5 , 1]

f (x)=¿ ¿ ¿

if k=1

(−0.5−x )3
( )( )( )
3
(x+ 1) 0.25 −0.5− x 0.25 x +1
f 1 ( x) = mo + m1 + 0.37−mo . + 0.6−m1 .
3 3 6 0.5 6 0.5

= m1
(x +1)
3
+ 0.37(−0.5−x
0.5 )(
+ 0.6−
0.25
6
m1
x+ 1
0.5 )
0.5 2 0.5 1
ii, m + (−0.5 ) m1 + m= ¿
6 o 3 6 2 0.5

= 0.08m2=−1.88−0.3 m1 m2=−23.5−3.75 m1

(−0.5−0 ) ( 0+1 )
iii, m2=φ ”(0) = mo +m1 =m2=2m1
0.5 0.5

m2=2 m1=−23.5−3.75 m1=2m1 +3.75 m1=−23.5

i.e. m1=≈−4=5.75 m1 = -23.5 = m1=−0.4086

34 | P a g e
3
−4 (x+ 1) (−0.5−x) x+1
f i (x) = +0.37 +¿)
3 0.5 0.5

K=2

( )
(0−x ) m (x +0.5) + 0.6−m 0.25 ( 0−x ) (1−m 0.25 ) x +0.5
3
f 2 ( x ) = m1 + 2 1 2
3 3 6 0.5 6 0.5

0.5 2 0.5 1
ii, m 1 + ( 0.5 ) m 2+ m 3= ( 1.65−2 ( 1 )+ 0.6 )
6 5 6 0.5

=0.083m1 +0.3m1 + 0.08m3 =0.5 =0.083(-4) +0.3m2 +0.08m3 =0.5

0.08m3 =0.5 +0.332 +0.3 m2 = m3 =6.25+4.15+3.75m2 =m3=10.4+3+5m2

(0.05) (0.5+0.5)
iii, m3 = f (0.5) =m1 +m2
0.5 0.5

m 3 = 4+2m2 = m3 =m3=4+2m2 = 10.4 +3.75m2

2m2 +3.75 m2 10.4 – 4 = 5.75m2 = 6.4 m2 =1.1

If k=3

f 3 (x)= {m} rsub {2} {{(0.5-x)} ^ {3}} over {3} + {m} rsub {3} {{(x-0)} ^ {3}} over {3} + left (1- {m} rsub

0.5 2 0.5 1
ii, m 2 + 0.05m2+ m4 + ¿)
6 5 6 0.5

=0.09 + 0.3 m3 + 0.083m4 = 0.84

= 0.083m4 = 0.84-0.09-0.3m3

m4 = 10.12 – 1.08 - 3.6 m3 = m4 = 9.04 - 3.6 m 3

( 0.5−1 ) ( 1+0.5 )
iii, m4 = f (1) = m2 + m3
0.5 0.5

= -1.1 + 3m3 = m4 = -1.1 + 3 m3

m4 = m4 = 9.04 – 3.6 m 3 = -1.1 + 3m 3

= 9.04 + 1.1 = 3 m3 + 3.6 m3 = 10.14 = 6.6 m3

35 | P a g e
m 3 = 1.54

If k= 4

( ) ( )
3 3
(1−x) (x−0.5) 0.25 1−x 0.25 x−0.5
f 4 ( x )=¿ m3 + m4 + 1.65−m 3 2.72−m4
3 3 6 0.5 6 0.5

= 1.54
( x−x )3
3 (
1.65−1.54
( 0.25 )
6 ) 1−x
0.5
( 2.72 )
( x−0.5 )
0.5

Problem 8 :- following values of x and y are given

x 0 1 2 3

f (x) 1 2 33 244

Find the cubic spline and find f (2.5)

m0=0 m3=0

Solution

Since the points are equi space h =1

We have

6
2 [ i ∓1
mi−1+ 4 mi +mi+1= f −2 f i+ f i−1 ] for i=1 , 2
h

m0 +4 m1+ m2=6 [ f 2−2 f 1+ f 0 ]

m1 +4 m2 +m3=6 [ f 3−2 f 2+ f 1 ]

4 m1+ m2=6 [ 33−4 +1 ] =180

m 1 +4 m2=6 [ 244−66 +2 ] =1080

m1=−24 , m2=276

[ ] [ ]
2 2
1 1 h 1 h
f 0 ( x )=
6h
[ ( x i−x ) mi−1 + ( x−x i−1 ) mi ] + f i−1 mi−1 ( x 1−x )+ f i− mi ( x−x i−1)
3 3

h 6 h 6

36 | P a g e
For [ 0 , 1 ] and i=1

f 1 ( x )=
1
6
[ ( [
x 1−x ) m0 + ( x −x0 ) m1 ] + ( x 1−x ) f 0−
3 3 m0
6 ]
+ ( x−x 0 ) f 1− [
m1
6 ]
¿
1
6
[ 0
[ ] 1
( 1−x )3 (0)+ ( x −0 )3 (−24) ]+ (1−x ) 1− + ( x−0 ) 2− (−24 )
6 6 [ ]
1 3
¿
6
[ x (−24 )]+1−x+ x( 2+ 4)
3
¿−4 x +1−x +6 x
3
¿−4 x +5 x+ 1

at [ 1 ,2 ] and i=2

f 2 ( x )=
1
6
[ ( [
x 2−x ) m1 + ( x−x 1 ) m2 ] + ( x 2−x ) f 1 −
3 3 m1
6 ]
+ ( x−x 1 ) f 2−[m2
6 ]
f 2 ( x )=
1
6
[ [ 1
6 ] 1
( 2−x )3 (−24 ) + ( x−1 )3 ( 276 ) ] + ( 2−x ) 2− (−24 ) + ( x−1 ) 33− 276
6 [ ]
1
¿
6
[ ( 8−12 x+ 6 x2 −x3 ) (−24 ) + x 3−3 x 2+3 x−1+(276)] + ( 2−x ) ( 2+4 ) +(x −1) [ 33−46 ]

1
¿ [−192+288 x−144 x 2 +24 x 3 +276 x 3−828 x 2 +12−6 x−13 x +13 ]
6

3 2
f 2 ( x )=50 x −166 x +167 x−53

at [ 2 ,3 ] for i=3

f 3 ( x )=
1
6
[ ( [
x 3−x ) m2+ ( x −x2 ) m3 ] + ( x 3−x ) f 2−
3 3 m2
6 ]
+ ( x−x 2 ) f 2−
6[
m3
]
¿
1
6 [
[ ( 3−x )3 276+ ( x−2 )3 (0)] +( 3−x ) 33− 276
6 ]
+ ( x−2 ) 244−
0
6 [ ]
1
¿
6
[ ( 27−27 x +9 x 2−x 3 ) 276 ]−39+13 x +244 x −488
3 2
f 3 ( x )=−46 x + 414 x −985 x +715
37 | P a g e
An estimate 2.5 is

f ( 2.5 )=f 3 (2.5)

3 2
f 3 ( 2.5 )=−46 (2.5) +414 (2.5) −985 ( 2.5 ) +715

=121.25

f ( 2.5 )=121.25

Problem 9 the following values of x and y are given

x 0 1 2

f (x) 1 2 3

With mo=0 , m2=0 here find f (1.5)

Solution

We have

6
2 [ i ∓1
mi−1+ 4 mi +mi+1= f −2 f i+ f i−1 ] for i=1
h

m0 +4 m1+ m2=6 [ f 2−2 f 1+ f 0 ]

4 m1+ 0+0=6 [ 33−4 +1 ]

4m1=6 (30)

m1=45

We have

[ ] [ ]
2 2
1 1 h 1 h
f 0 ( x )=
6h
[ ( x i−x ) mi−1 + ( x−x i−1 ) mi ] + f i−1− mi−1 ( x 1−x ) + f i − mi ( x −xi−1 )
3 3

h 6 h 6

38 | P a g e
f 1 ( x )=
1
6
[ ( [
x 1−x ) m0 + ( x −x0 ) m1 ] + ( x 1−x ) f 0−
3 3 m0
6 ] [
+ ( x−x 0 ) f 1−
m1
6 ]
f 1 ( x )=
1
6
[ [ ]0
( 1−x )3 (0)+ ( x−0 )3 45 ]+ ( 1−x ) 1− + ( x−0 ) 2−
6
45
6 [ ]
f 1 ( x )=
1 3
6
[ x (45) ] + ( 1−x ) + x
6( )
−33

45 3 33
f 1 ( x )= x +1−x− x
6 6

1
¿ [ 45 x 3 −39 x +6 ]
6

For [ 1 ,2 ] i=2

f 2 ( x )=
1
6
[ ( [
x 2−x ) m1 + ( x−x 1 ) m2 ] + ( x 2−x ) f 1 −
3 3 m1
6 ] [
+ ( x−x 1 ) f 2−
m2
6 ]
f 2 ( x )=
1
6
[ [
( 2−x )3 45+ ( x−1 )3 (0) ] + ( 2−x ) 2−
45
6 ]
+ ( x−1 ) 33−
[
0
6 ]
¿
1
6
[ ( 2−x )3 45 ] +( )
−33
6
( 2−x )+33 ( x−1 )

15 66 33
¿ ( 2−x )3− + x +33 x−33
2 6 6

15 11
f 2 ( x )= ( 2−x )3−11+ x +33 x−33
2 2

15 77
f 2 ( x )= ( 2−x )3+ x−44
2 2

At x=1.5

15 77
f 2 ( 1.5 ) = ( 2−1.5 )3 + (1.5)−44
2 2

15 77
f 2 ( 1.5 ) = ( 0.5 )3 + (1.5)−44
2 2

39 | P a g e
f 2 ( 1.5 ) =0.9375+57.75−44

f 2 ( 1.5 ) =14.6875

Curve Fitting with Splines


The main application of cubic spline interpolation techniques is, of course, curve fitting. To this
end, the consistency and efficiency of the spline as a data correlation tool will be demonstrated.
Function Imitation Cubic splines would not be necessary were it simple to determine a well-
behaved function to fit any data set. This is, however, usually not the case. Thus, the cubic spline
technique is used to generate a function to fit the data. Moreover, it can be shown that data
generated by a particular function is interpolated by a spline which behaves more or less like the
original function. This is testimony to the consistency of splines. For example, the following
figure was generated using the function y=sin ( x )

Graph 6:function y =sin(x)

This next figure was generated by connecting six data points along the above line with a cubic
spline function.

Graph 7:six data points with a cubic spline function Graph 8: comparison of true value and cubic spline

By superimposing graph 6 on graph 7, we can see the degree to which the cubic curve imitates
the original function y = sin(x). Clearly, the cubic curve closely imitates the sine curve. It has no

40 | P a g e
extreme behavior between data points, and it effectively correlates the points. This characteristic
also works with more erratic functions. Take for instance the function below.

Graph 9: curve fitting illustrative example

While the fit is not perfect, it does closely approximate the function without a great degree of
divergence.

Data interpretation
The splines other strength lies in its ability to correlate data which doesn’t follow any specific
pattern without a single polynomial’s extreme behavior. Take, for instance, the 100 random data
points below

Figure 3: random data

Clearly there is no relation between these data points. A spline, however, can interpolate all 100
points without the drastic behavior that the necessary 99th degree polynomial would exhibit.

41 | P a g e
Figure 4: polynomial graph 99 degree

Note that, while the spline is not exactly pleasing to the eye, its range stays from 0 to 1.2; a much
more reasonable range than an approximating polynomial. Also, it is not much of a task to
generate an approximating spline which generally correlates the data while being much more
well behaved.

Figure 5: data interpretation with cubic spline

The above figure demonstrates the general trends in the data (of which there are none) without
necessarily connecting all data points. Its curvature is much less severe than that of fig 4.

Representing functions by approximating polynomials


First, let’s review the application of a cubic spline to approximate polynomials, or to evaluate a
cubic spline at certain point within the given interval [a , b]. As an example, consider the
1
polynomial function f ( x )= 2
sin ( x ) , on the interval [ π , 3 π ]. We can take few coupled data
x 4 2
points:

Graph 10: the natural cubic interpolation of a solid line

π π 3π 5π 3π
( , 1.1463),( , 0.4053),( , 0.1274),(π , 0) ,( ,−0.0459) ,( ,−0.0450). With natural
4 2 4 4 2
cubic spline, the interpolating function is shown as a solid line in Fig. 6 below. The exact values
42 | P a g e
of the polynomial function f (x) (red markers) are also shown in Fig. Comparing the results, we
can see that the cubic-spline interpolating function approximates the exact function with a good
accuracy.

43 | P a g e
Unit four

4. Conclusion
From spline interpolation methods three interpolation method are compared namely, linear spline,
quadratic and cubic spline I have presented an overview of the methods of interpolation and especially
the cubic spline interpolation, which is widely used in numerous real-world applications. The study
presents the definition and related theory of linear, quadratic and cubic spline interpolation and
properties of different types of cubic splines, including a natural spline, a clamped spline. I’ve also
analyzed some general applications of cubic spline interpolation such as curve fitting, data interpretation
and Representing functions by approximating polynomials. In conclusion of this study, I can say that
cubic spline is by far the best interpolating method especially in application of curve fitting.

44 | P a g e
References
Hazewinkel, Michiel. (2001). Spline interpolatio. Springer,ISBN 978-1-55608-010-4:
Encyclopedia of Mathematics.

L. Pedroso1 | A. Arco. (2021, Aug 18). Application of cubic spline interpolation with optimal
spatial sampling for damage identification.

Rokiah Rozita Ahmad,Najah Ghazali. (2012). Application of Cubic Spline in the. Journal of
Mathematics and Statistics 8 (1): 144-149, 2012.

Kruger, C. J. C. (n.d): Constrained cubic spline interpolation for chemical engineering application

Crochiere, R.E. and Rabiner, L.R. (1983): [Link]

Hazewinkel, Michiel, ed. (2001): "Spline interpolation", Encyclopedia of Mathematics, Springer,ISBN 978-
1-55608-010-4

Henrici, P. (1982): Essential of numerical analysis, New York. John Wiley & Sons.

McKinley, S., Levine, M., Cubic Spline Interpolation, Math 45: Linear Algebra,
[Link]

Akima, H., 1969, A Method of Smooth Curve Fitting, ESSA Tech. Rep. ERL 101-ITS 73 (Washington,D.C.:
U.S. Government Printing Office)

Jianhua Z. Huang and Charles J. Stone, Extended Linear Modeling with Splines, 1999, 1-20 Ward Cheney,
David Kincaid Numerical Mathematics and Computing, seventh edition, 250-260

Kenneth H. Carpenter, An introduction to interpolation and splines, EECE KSU, November 22, 1999
revised November 20, 2001, April 24, 2002, April 14, 2004, 1-8

45 | P a g e
7. y_linear = interp1d(x, y)

Appendices 8. [Link] (x,y,"o", label= "data points")

Python codes 9. [Link](x_interp,y_linear(x_interp), "red",


label= "linear spline")
Graph 1
10. [Link]()
1. import numpy as np
11. [Link]()
2. import [Link] as plt
3. from [Link] import interp1d graph 3
4. (3) = [Link] ([1, 6, 7, 9, 12, 20]) 1. import numpy as np
5. y = [Link]([2, 8, 6, 10, 14, 41]) 2. import [Link] as plt
6. x_interp = np. linspace([Link](x), [Link](x), 3. from [Link] import interp1d
70) 4. # 1d array of data points
7. y_linear = interp1d(x, y) 5. x = [Link] ([1, 3, 5, 8])
8. y_quadratic = interp1d (x, y, kind="quadratic") 6. y = [Link]([2, 3, 9, 10])
9. y_cubic = interp1d (x, y, kind="cubic") 7. x_interp = np. linspace([Link](x), [Link](x), 70)
10. [Link] (x,y,"o", label= "data points") 8. # 2D quadratic interpolation
11. [Link](x_interp,y_linear(x_interp), "red", 9. y_quadratic = interp1d (x, y, kind="quadratic")
label= "linear spline") 10. [Link] (x,y,"o", label= "data points")
12. [Link](x_interp,y_quadratic(x_interp), "green", 11. [Link](x_interp,y_quadratic(x_interp), "red",
label= "quadratic spline") label= "quadratic spline")
13. [Link](x_interp,y_cubic(x_interp), "blue", 12. [Link]()
label= "cubic") 13. [Link]()
14. [Link]()
Gauss elimination with partial pivoting
15. [Link]()
1. import numpy as np
Graph 2 2. def guassElimination(a_matrix,b_matrix):
1. import numpy as np 3. if a_marix.shape [0]!= a_matrix.shape[1]:
2. import [Link] as plt 4. print("error: square matricx not given")
3. from [Link] import interp1d 5. return

4. x = [Link] ([1, 6, 7, 9, 12, 20]) 6. if b_matrix.shape[1]>1 or b_matrix.shape[0] !=

5. y = [Link]([2, 8, 6, 10, 14, 41]) a_matrix.shape[0]:


7. print("error constant vector is incorectly sized")
6. x_interp = np. linspace([Link](x), [Link](x),
8. return
70)
9. n=len(b_matrix)

46 | P a g e
10. m = n - 1
11. i = 0
12. x = [Link](n)
13. new_line = "\n" 37. variable_matrix = [Link]([[1,1,1,0,0,0,0,0,0],
14. augmented_matrix = np. concatenate((a_matrix, [0,0,0,9,0,0,0,0,0],
b_matrix), axis=1,dtype=float) [9,3,1,0,0,0,0,0,0],
15. print(f"the initial augmented matrix is: [0,0,0,25,5,1,0,0,0],
{new_line}{augmented_matrix}")
[0,0,0,0,0,0,25,5,1],
16. print("solving for the upper-triangular matrix:")
[0,0,0,0,0,0,64,8,1],
17. while i < n :
[6,1,0,-6,-1,0,0,0,0],
18. for p in range(i+1, n):
[0,0,0,10,1,0,-10,-1,0],
19. if abs(augmented_matrix[i,i]) <
[2,0,0,0,0,0,0,0,0]])
abs(augmented_matrix[p,i]):
20. augmented_matrix[[p,i]]=augmented_matrix[[i,p]] 38. constant_matrix = [Link]([[2],

21. if augmented_matrix[i,i]==0.0: [3],


22. print("divide by zero error!") [3],
23. return [9],
24. for j in range(i+1,n): [9],
25. scaling_factor =
[10],
augmented_matrix[j][i]/augmented_matrix[i][i]
[0],
26. augmented_matrix[j]=augmented_matrix[j] -
[0],
(scaling_factor*augmented_matrix[i])
[0]])
27. print(augmented_matrix)
28. i = i + 1
39. guassElimination(variable_matrix,
29. x[m]= augmented_matrix [m] [n] /
constant_matrix)
augmented_matrix [m] [m]
30. for k in range (n - 2, -1, -1):
31. x[k]= augmented_matrix[k][n]
32. for j in range (k+1,n):
33. x[k]=x[k]/augmented_matrix[k][k]
34. print (f"the folloeing x-vector matrix solve the
above augmented matrix:")
35. for answer in range (n):
36. print(f"x{answer} is{x[answer]}")

47 | P a g e

You might also like