Numerical Examples
Numerical Examples
nb 1
Illuminating
Numerical Analysis
using Mathematica
Jennifer Voitle and Edward Lumsdaine
Abstract
This paper discusses the experiences of the authors teaching courses in Numerical Analysis to engineering
students at two universities, both pre- and post-introduction of Mathematica. Use of Mathematica in
such courses really enhances and accelerates student learning and comprehension, providing a foundation for
success in subsequent courses. With Mathematica, more realistic and complex applications can be
assigned, since Mathematica has many of the required intrinsic functions to solve real-life problems. Tips
are given to increase student acceptance of Mathematica, as some students will resist it for various reasons.
This paper also discusses the use of Mathematica as a programming language, including transition from
and communication with other languages. Much of the course can be taught using the intrinsic functions
available in Mathematica, but for certain applications extension of the built-in capabilities is required. For
example, in earthquake and vibration engineering, systems of differential equations must be solved whose
forcing functions are given as tabular data. This paper shows how to read in these data from a file and manipu-
late them so that NDSolve can work with them.
One of the most powerful ways to use Mathematica as a teaching tool is via Mathematica Note-
books. The course can be taught electronically - as a "live" textbook. Students can obtain the needed informa-
tion from these Notebooks, which include text, algorithms, animations and problem sets. The paper discusses
such Notebooks to teach Numerical Analysis, including guidelines for effective design. Where desired,
assignments can be given in the "traditional" way of writing computer programs by following algorithms, but
using Mathematica as the programming language. Other assignments can be given which involve the use
or modification of existing Mathematica functions. Freed from the tedious program writing, testing,
debugging and compiling of programs, this method encourages students to explore "what-if" questions by
altering parameters and methods.
NumericalExamples.nb 2
Summing Integers
As an example, consider the problem of summing integers from 1 to some ending value. That is, we seek
100
i
i=1
or in C:
sumint.c program to sum from 1 to n
include < stdio.h >
Mathematica code
In Mathematica, this program can be written in a single line,since there is a Sum function built in. The upper
index n is passed as a function argument. Suppose that we choose n=100:
100
i
i=1
5050
One unfortunate fact regarding other programming languages is that they only have finite precision. Mathe-
matica has infinite precision, which means that one could ask for the sum of i from one to one billion and
the correct answer would be obtained, an impossible feat in other languages. However, the way that Mathe-
matica does the sum makes the result, while correct, prohibitively slow. To speed things up, change the way
the sum is evaluated. Mathematica has a Range function, which outputs a list ranging over the limits
provided by the user. For example, Range[10] produces the following:
Range@10D
81, 2, 3, 4, 5, 6, 7, 8, 9, 10<
Mathematica has another function, Plus, which takes a set of numbers as its input and returns their sum.
NumericalExamples.nb 4
For example:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
55
To avoid having to type the arguments to Plus, combine it with Range using the command Apply as
follows:
Plus @@ Range@10D
55
Apply works by taking the output form of Range, which was a list of numbers, and replacing this by Plus.
So the output is the sum of the given set of numbers. To appreciate this, compute the sum of integers from 1 to
10000 using both Sum and Apply with Range. Wrap the function Timing around these for comparison. I
am running this on a Macintosh SE/30 running an unenhanced copy of Mathematica. Your times will vary.
$Version
10 000
TimingB iF
i=1
BASIC:
C:
double g Hdouble xL
8
return pow HHx + 1L, H1. 3LL; users function definition
<
NumericalExamples.nb 7
Clear@gD
g@x_D = Hx + 1L ;
13
tol = 0.0001;
itmax = 100;
p0 = 1.5;
i = 1;
status = "iterating"
Print@" Solution of x = ", g@xD, " by Fixed Point Iteration"D;
Print@"iteration x gHxL "D;
WhileAi itmax && status "Converged",
pnew = g@p0D; PrintAi, "\t\t\t", p0, "\t", pnewE;
If@Abs@pnew p0D tol, status = "Converged"D; p0 = pnew; i += 1E
If@status === "Converged",
Print@"Method converged in ", i = 1, " iterations to root ", pnewD,
Print@"Method did not converge in ", itmax, " iterations."DD
Clear@gD
g@x_D = Hx + 1L13 ;
itmax = 5;
p0 = 1.5;
NestList@g, p0, itmaxD
Rootfinding
Example: Heat Transfer through Concentric
Cylinders
The analysis of heat transfer through a series of concentric cylinders requires the solution of the following
equation. Determine the thickness Dr = r2 - r1 of the inner cylinder given that
r2 0.2640
ln = 2.2185 ln
r1 r ln r2
2 r
1
by all root-finding methods. Note that r2 and r1 are in meters. Take r1 = 0.1575 m. Summarize results. Did
Steffensen's method result in any improvement?
Schematic
Analysis
The given equation must be solved for r2. r1 is given as 0.1575 m. It would be reasonable to expect that r2 >
r1 but r2< 1, say. The solution will be carried out by the Bisection Method, Fixed Point iteration, Steffensen's
Method, Newton's Method and the Secant Method. The solution will be carried out to a tolerance of 0.001 and
all results compared.
NumericalExamples.nb 9
Solution
Function Definition
Define r1 and and f[r2]:
r1 = 0.1575;
r2 .264
f@r2_D = LogB F 2.2185 LogB F;
r1 r2 LogA r2
r1
E
f@r2D
r2
0.2 0.4 0.6 0.8 1
-10
-20
-30
Solution by Solve
Mathematica has the Solve function, which works really well only for polynomials and simpler non-
polynomial equations. It will not work on our particular function, which is why numerical methods are
necessary.
NumericalExamples.nb 10
N@Solve@f@r2D == 0, r2DD
0.264
[email protected] r2D 2.2185 LogB F == 0., r2F
r2 [email protected] r2D
Bisection Method
This example uses our package, bisection.m, described in the appendix. For now, just use the function
definition:
Bisection::converged :
The Bisection Method converged to the root of the function in 9 iterations.
Trial 1
Clear@gD
g@r2_D = f@r2D + r2
0.264
r2 + [email protected] r2D 2.2185 LogB F
r2 [email protected] r2D
-5
-10
-15
It is clear from the plot that this choice of g will cause the method to diverge. A few iterations will verify this:
NestList@g, .4, 5D
This is a case of monotone divergence. Different formulations of g can be attempted. In this case, conver-
gence will be attained if we solve for the innermost r2 in the nested logs. We find:
NumericalExamples.nb 13
0 . 2640
1
r2 2 . 2185
r1
r2 = r1 Exp
r2
Clear@gD
.264
r2 12.2185
K O r2
g@r2_D = r1 r1
0.114756
0.1575 r2 1.45076
Twenty five iterations of the fixed point method and a plot of the path are generated by the code below:
This method appears to be exhibiting spiral convergence. It will take a large amount of iterations to converge
since the slope of g[r2] approaches unity (the upper asymptote for convergence) near the root.
0.915321
In fact, fixed point iteration will take 208 iterations to converge to the root of 0.302134. Either NestList or
FixedPoint can be used to illustrate this:
80.302133, 0.302134<
Steffensen's Algorithm
To increase the speed of convergence of the fixed point method,
compute p1 = g[p0], p2 = g[p1]. So far, this is identical to the fixed point method. But now compute
( p1 p 0 )2
p = p0
p 2 2 p1 + p 0
where p is assumed to be a better estimate of the root than p0, p1 or p2. Convergence is attained when |p - p0|
<= tol. If not, p0 is set equal to p and the process is continued.
This algorithm can be easily programmed in Mathematica as:
NumericalExamples.nb 15
HxP2T xP1TL2
Steffensen@x_ListD := xP1T
xP3T 2 xP2T + xP1T
Start by generating the required estimates p1, p2 by NestList. These will be used by Steffensen to get the next
estimate p:
Iteration 1
x = NestList@g, .4, 2D
p = Steffensen@xD
0.317532
Iteration 2
x = NestList@g, p, 2D
p = Steffensen@xD
0.302695
Iteration 3
x = NestList@g, p, 2D
p = Steffensen@xD
0.302135
NumericalExamples.nb 16
Iteration 4
x = NestList@g, p, 2D
p = Steffensen@xD
0.302134
This is the solution. It took four iterations compared to the two hundred and eight required by fixed point,
dramatically demonstrating the acceleration of convergence.
A simple Steffensen program can be written as follows:
FindRoot[{lhs1==rhs1,lhs2==rhs2,...,lhsn==rhsn}, {var1,guess1},
NumericalExamples.nb 17
8r2 0.302134<
To see the intermediate calculations, NestList can be used. Newton's method is a special case of fixed
point iteration, with g defined as follows:
Clear@gD
f@r2D
g@r2_D = r2
f @r2D
Plot@8r2, g@r2D<, 8r2, 0.2`, 0.4`<, PlotLabel "Newton Form"D
0.264
[email protected] r2D 2.2185 LogB r2 [email protected] r2D
F
r2
1.
r2
8.40341 r2 K 0.264
0.264
O [email protected] r2D
r22 [email protected] r2D2 r22 [email protected] r2D
Newton Form
0.32
0.28
0.26
It is clear that this scheme will converge. NestList will show the calculations:
f (x + h ) f (x )
f ( x )
h
Thus, the secant scheme is
f ( x i +1 )( x i +1 x i )
x i + 2 = x i +1 , i = 0,1,2,..., done
f ( x i +1 ) f ( x i )
Note that two initial guesses x0, x1 are required. The Secant Method is carried out in Mathematica by
calling FindRoot with two initial guesses:
8r2 0.302134<
NumericalExamples.nb 19
Polynomial Interpolation
The problem may be solved by fitting an interpolating polynomial through the (angle,force) data. This approxi-
mates the function force(angle) which is then solved for the angle giving a force of 1250 pounds. Alterna-
tively, inverse interpolation may be performed. In this case, an inverse function angle(force) is generated
which may then be evaluated at 1250. Mathematica has the function InterpolatingPolynomial to
perform these tasks. The output is identical to the expanded Lagrange or Newton interpolating polynomial. A
short program, NewtonDividedDifference, is presented to show the intermediate steps of the calcula-
tions.
DataPlot = ListPlot@WeightLifterData,
PlotStyle [email protected]`D, AxesLabel 8"angle", "force"<D
force
2000
1750
1500
1250
1000
750
angle
65 70 75 80 85 90
2000
1750
1500
1250
1000
750
65 70 75 80 85 90
The above graph shows that an angle of approximately 74 degrees will result in a force of 1250
pounds on the back. We will verify this with FindRoot:
NumericalExamples.nb 21
Clear@NewtonDD, xD
NewtonDD@data_List, var_SymbolD :=
Module@8i, n, poly, xx, f<,
xx = var;
Do@xx@i 1D = dataPi, 1T;
f@i 1D = dataPi, 2T, 8i, Length@dataD<D;
f@i_, 0D := f@iD;
Hf@n_, i_D := Hf@n, i 1D f@n 1, i 1DL Hxx@nD xx@n iDL ; i > 0L;
Sum@f@i, iD Product@var xx@j 1D, 8j, i<D, 8i, 0, Length@dataD 1<D D
NewtonDD@WeightLifterData, xD
In this form, the coefficients a0, a1, a2, ... can easily be identified for checking hand calculations. Expand this
to verify that this form is identical to the Lagrange polynomial:
True
NumericalExamples.nb 22
angle@force_D = N@Expand@
InterpolatingPolynomial@Table@8force@iD, i<, 8i, 60, 90, 10<D, forceDDD
angle@1250D
75.0472
Here, we import our spline interpolation package, which we have stored in the standard package library in the
folder NumericalMath. ( Of course, Mathematica now has its own spline routines.)
Needs@"NumericalMath`SplineInterpolation`"D
NaturalCubicSpline@WeightLifterDataD
S@0,xD = 18 617. + 1198.29 x 21.324 x2 + 0.118467 x3 for 60. < x < 70.
S@1,xD = 96 562.4 3737.97 x + 49.194 x2 0.217333 x3 for 70. < x < 80.
S@2,xD = 65 332. + 2333.07 x 26.694 x2 + 0.0988667 x3 for 80. < x < 90.
2000
1750
1500
1250
1000
750
65 70 75 80 85 90
Clear@FD3Pt, BD3PtD
3 fP1T + 4 fP2T fP3T
FD3Pt@f_List, h_D :=
2h
3 Last@fD 4 fPLength@fD 1T + fPLength@fD 2T
BD3Pt@f_List, h_D :=
2h
General::spell1 :
Possible spelling error: new symbol name "BD3Pt" is similar to existing symbol "FD3Pt".
888.05, 73.35<
The procedure ClampedCubicSpline requires the given data as input and these derivatives. The usage is:
ClampedCubicSpline@88x0,y0<,8x1,y1<,...8xn,yn<,8y'0,y'n<<D
constructs and prints a clamped cubic spline interpolating
the given data points. The clamped spline boundary
conditions S'@x0D = f'@x0D,S'@xnD = f'@xnD are used.
Modify the weight lifter data to include the derivatives using Append:
Append@WeightLifterData, DerivsD
8860, 2103<, 870, 1410<, 880, 1092<, 890, 497<, 888.05, 73.35<<
NumericalExamples.nb 25
ClampedCubicSpline@%D
S@0,xD = 2294.4 + 469.35 x 10.513 x2 + 0.0652 x3 for 60. < x < 70.
S@1,xD = 87 160. 3364.41 x + 44.255 x2 0.1956 x3 for 70. < x < 80.
S@2,xD = 46 369.6 + 1642.95 x 18.337 x2 + 0.0652 x3 for 80. < x < 90.
PiecewiseCubicPlot@D;
2000
1750
1500
1250
1000
750
65 70 75 80 85 90
Show@DataPlot, %D
force
2000
1750
1500
1250
1000
750
angle
65 70 75 80 85 90
NumericalExamples.nb 26
N@P3@angleDD
Note that the cubic regression polynomial is identical to the cubic generated by InterpolatingPolynomial.
(WHY?)
What general conclusions can you draw from this?
NumericalExamples.nb 27
Numerical Solution of
Differential Equations
Application
(Dr. Naser Mostaghel's problem)
In earthquake engineering, the following system of ODE's requires solution:
dz
+ 2 z (t ) + 2 2 u (t ) = u g (t )
dt
du
= z (t )
dt
z (t 0 ) = y (t 0 ), y (t 0 ) = y 0 , t 0 t t max
where ug(t) represents the displacement of the ground with time and u(t) is the resulting displacement of the
structure under consideration. Although Mathematica's intrinsic functions DSolve and NDSolve should
be able to solve this system, the difficulty in solving this particular problem is that ug(t) is usually discrete data
points which must be read in from a data file. Currently, DSolve and NDSolve require continuous inputs,
so some manipulation of the data must be performed before these functions see it.
In the following example, we shall first assume that ug(t) is well-represented by a sine function and will apply
DSolve directly to the problem. We then read in the actual data from the file, manipulate it into a continuous
form and compare the results from DSolveing this to the preceding results.
NumericalExamples.nb 28
Parameter Definitions
Clear@u, zD
solution =
FlattenADSolveA9z @tD + 2 alpha beta xi z@tD + alpha2 omega2 u@tD == ug@tD,
u @tD == z@tD, z@t0D == yprime0, u@t0D == y0=, 8z@tD, u@tD<, tEE
General::stop :
Further output of FactorSquareFree::lrgexp will be suppressed during this calculation.
General::stop :
Further output of PolynomialGCD::lrgexp will be suppressed during this calculation.
Had Mathematica had been able to solve this, we would have plotted using the following command:
displacement u[t]
2 4 6 8 10
-1
-2
0.48843 236.435
3.20602 22.9008
5.92361 -223.666
8.6412 -144.339
10. 0.0304909
All that needs to be done is to read in the data in the proper form for Mathematica manipulation, that is, in
the form
{{pair1},{pair2}, ... ,{pairn}}. This can be accomplished with the OpenRead command (here,
it is assumed that the data are contained in the Mathematica folder. If not, the required information would
be included.)
OpenRead@"quakedata.txt"D
GroundData = ReadList@"quakedata.txt", 8Number, Number<D
[email protected], 17D
200
100
4 6 8 10
-100
-200
A curve-fit of the data (regression, interpolating polynomial, spline, etc) could be attempted. Please refer back
to the previous section if this is desired.
(For example, we could type Fit[GroundData,{Sin[N[delta] t]},t] to obtain the result
240.1597 Sin[15.7079 t]).
Here, we will construct a piecewise-continuous function using Which. First, put the data into the proper form
using the Table command to automate things. This creates a list. Then Which is mapped onto the list, which
is named ug[t].
Clear@ugD
ug@t_D := Which @@ Flatten@Append@
Table@8t GroundDataPi, 1T && t < GroundDataPi + 1, 1T, GroundDataPi, 2T<,
8i, 1, Length@GroundDataD 1<D, 8t GroundDataPLength@GroundDataD, 1T,
GroundDataPLength@GroundDataD, 2T<DD
200
100
2 4 6 8 10
-100
-200
NumericalExamples.nb 31
Show@%, inputdataplotD
200
100
2 4 6 8 10
-100
-200
Note that if this approximation is unsatisfactory, (perhaps one desires the points to be at the midpoint of the
interval rather than at the left endpoints), this is easily accomplished by modification of the code used to
construct ug[t].
Clear@u, zD
solution2 =
FlattenANDSolveA9z @tD + 2 alpha beta xi z@tD + alpha2 omega2 u@tD == ug@tD,
u @tD == z@tD, z@t0D == yprime0, u@t0D == y0=, 8z@tD, u@tD<, 8t, t0, 6<EE
displacement u@tD
1 2 3 4 5 6
-1
-2
What If
The input data for ug[t] did not give an accurate result because there were too few points to adequately
describe the forcing function. Try solving the problem again with the file quakedata2, which contains the
following points:
0.48843 236.047
0.964009 128.571
1.43959 -139.806
1.91517 -233.222
2.39074 -34.7715
2.86632 207.194
3.3419 189.866
3.81748 -65.0713
4.29306 -238.575
4.76864 -113.512
5.24421 153.605
5.71979 228.493
6.19537 17.4318
6.67095 -215.444
7.14653 -178.702
7.62211 81.6783
8.09769 239.841
8.57326 97.8538
9.04884 -166.594
9.52442 -222.557
-15
10. -1.35308 10
A regression approximation of these data in the form a Sin[d t] yields 240.00 Sin[15.7079 t]. This is a
NumericalExamples.nb 33
2 2 1
2
+ 2
+ g ( x, y ) = 0
x y k
describes two-dimensional, steady state conduction heat transfer. The solution (x,y) gives the temperature
T(x,y)- Tinf on a rectangular plate, where Tinf is the temperature of the surroundings. The physical domain is
given as 0 <= x <= PlateLength, 0 <= y <= PlateHeight.
An energy source term g(x,y) is allowed. Admissable boundary conditions are Type 1. The user specifies the
material thermal conductivity k; the boundary functions; the values of the plate dimensions PlateLength and
PlateHeight; and the desired numbers of subintervals in each direction. The program then constructs the finite
difference equation at each interior node and solves for the desired nodal temperature excesses via the Solve
function. The results are presented in the form of contour and surface plots.
The program may be modified to handle three-dimensional geometry, time-dependence and boundary condi-
tions of Types 2 and 3. Also note that Solve is used only for illustrative purposes on this small toy problem. In
other documents, we have used more sophisticated matrix methods. Also note that a variant of the heat equa-
tion is the fundamental formula underlying many financial derivatives, and in other papers these techniques are
applied to problems of financial analysis.
NumericalExamples.nb 34
Example
Determine the temperatures on a rectangular plate. The plate is internally heated by an energy source term
g(x,y) = 1000 Exp[-4 Pi^2 x y]. The plate is 1.0 m by 1.0 m in dimension with thermal conductivity k = 275
W/m-K. The boundary conditions are:
(1) T[x,y] = 100 Sin[Pi y/PlateHeight], x = 0, 0 <=y<=PlateHeight;
(2) T[x,y] = 400 Sin[Pi x/(2 PlateLength)], y = PlateHeight, 0 <=x<=PlateLength;
(3) T[x,y] = 400 y/ PlateHeight, x = PlateLength, 0 <=y<=PlateHeight;
(4) T[x,y] = 0, y = 0, 0 <=x<=PlateLength.
Solution Technique
The finite-difference methods will be used to solve for the steady temperatures on a square plate subject to
Type 1 boundary conditions T(x,0) = T(0,y) = 100; T(x,H) = T(L,y) = 0. The plate is subdivided into four
equally-spaced subdivisions in each direction. The plate thermal conductivity is uniform and there is no
internal energy generation. The governing equation is
D[T[x,y],[x,2}] + D[T[x,y],{y,2}] == 0 which is written in finite difference form as Ti,j+1 + Ti,j-1 + Ti+1,j +
Ti-1,j - 4Ti,j = 0,
i = 1, ... , 4, j = 1, ... , 4.
Solution
The finite-difference equation is defined as eq[i,j]:
NumericalExamples.nb 35
Clear@u, g, eq, TD
PlateLength = 1.0; H m L
PlateHeight = 1.0; H m L
NumberofXSubintervals = 5;
NumberofYSubintervals = 5;
DeltaX = PlateLength NumberofXSubintervals;
DeltaY = PlateHeight NumberofYSubintervals;
k = 275; H WmK L
g@i_, j_D = 1000 Exp@N@4 Pi ^ 2 i DeltaX j DeltaYDD;
equation@i_, j_D := HHu@i + 1, jD 2 u@i, jD + u@i 1, jDL DeltaX ^ 2 +
Hu@i, j + 1D 2 u@i, jD + u@i, j 1DL DeltaY ^ 2 == 0L
General::spell1 :
Possible spelling error: new symbol name "NumberofYSubintervals" is similar
to existing symbol "NumberofXSubintervals".
General::spell1 :
Possible spelling error: new symbol name "DeltaY" is similar to existing symbol "DeltaX".
Boundary Conditions
Now construct and print out the finite difference equations to be solved (one equation is written for each node
at which the temperature is unknown.:
Mathematica constructs the list of equations, one at each point at which the temperature is unknown, with the
following command:
NumericalExamples.nb 36
Equations = Flatten@
Table@
Simplify @ equation@i, jD D ,
8i, NumberofXSubintervals 1<, 8j, NumberofYSubintervals 1<D
D
8u@1, 1D, u@1, 2D, u@1, 3D, u@1, 4D, u@2, 1D, u@2, 2D, u@2, 3D, u@2, 4D,
u@3, 1D, u@3, 2D, u@3, 3D, u@3, 4D, u@4, 1D, u@4, 2D, u@4, 3D, u@4, 4D<
Plots
400
300 6
200
5
100
0 4
1
2 3
3
4 2
5
61
ListContourPlot@Temperatures,
PlotLabel "Steady Plate Temperature", ColorFunction HueD
NumericalExamples.nb 39
1
1 2 3 4 5 6
NumericalExamples.nb 40
Appendix
Code for CubicSplineInterpolation.m
BeginPackage@"NumericalMath`SplineInterpolation`"D
SplineInterpolation::"usage" =
"The package SplineInterpolation contains code for construction of
natural, clamped, periodic and B cubic splines. A plotting utility,
PiecewiseCubicPlot, and integration procedure, IntegrateSpline,
are also provided. For information on usage of these procedures,
type ?NaturalCubicSpline, ?ClampedCubicSpline, ?PeriodicSpline,
?BSplineInterpolation, ?PiecewiseCubicPlot or ?IntegrateSpline. "
NaturalCubicSpline::"usage" =
"NaturalCubicSpline@88x0,y0<,8x1,y1<,...8xn,yn<<D
constructs and prints a natural cubic spline
interpolating the given data points. The natural spline
boundary conditions S''@x0D = S''@xnD=0 are used."
ClampedCubicSpline::"usage" =
"ClampedCubicSpline@88x0,y0<,8x1,y1<,...8xn,yn<,8y'0,y'n<<D
constructs and prints a clamped cubic spline interpolating
the given data points. The clamped spline boundary
conditions S'@x0D = f'@x0D,S'@xnD = f'@xnD are used."
BSplineInterpolation::"usage" =
"BSplineInterpolation@88x0,y0<,8x1,y1<,...8xn,yn<,8y'0,y'n<<D
constructs and prints a Bspline interpolating the given
data points. Both natural and clamped spline boundary
conditions S'@x0D = f'@x0D; S'@xnD = f'@xnD; S''@x0D =
S''@xnD = 0 are used. The BSpline does not interpolate the
given data at the points 88x1,f@x1D<,8xn1,f@xn1D<<.
NOTE: n must be >2 for BSplineInterpolation."
PeriodicSpline::"usage" = "PeriodicSpline@88x0,y0<,8x1,y1<,...8xn,yn<<D
constructs and plots a periodic spline. This procedure is used for
plotting smooth, closed curves. The periodic boundary conditions
S@x0D = S@xnD, S'@x0D = S'@xnD and S''@x0D = S''@xnD are used."
IntegrateSpline::"usage" = "IntegrateSpline computes the
integral of the spline over the interval @x@0D,x@nDD.
It is not intended for use with PeriodicSpline."
CubicSpline::"smallnerr" = "Number of data points, n =`1` is insufficient
for spline interpolation: you must provide at least `2` data points."
CubicSpline::"nonfunctionerr" = "The x coordinates provided
`1` do not describe a function. The x's must be unique."
PiecewiseCubicPlot::"usage" = "PiecewiseCubicPlot@D constructs a plot
of the constructed spline. The option PlotPoints>num may be
included. Otherwise, the default value of PlotPoints is used.
NumericalExamples.nb 41
Message@CubicSpline::"smallnerr", Length@SplineDataD,
MinNumberofPointsD; Return@Hold@BSplineInterpolation@dataDDDD;
LeftDeriv = SplineDataPLength@SplineDataD, 1T;
RightDeriv = SplineDataPLength@SplineDataD, 2T;
SplineData = Drop@SplineData, 1D;
If@SplineData Sort@SplineDataD, SplineData = Sort@SplineDataDD;
xlist = Table@SplineDataPi, 1T, 8i, Length@SplineDataD<D;
If@xlist Union@xlistD,
Message@CubicSpline::"nonfunctionerr", xlistD; Return@DD;
n = Length@SplineDataD 1; Do@t@iD = SplineDataPi + 1, 1T, 8i, 0, n<D;
a@0D = SplineDataP1, 2T; a@nD = SplineDataPn + 1, 2T;
Do@a@iD = SplineDataPi + 1, 2T, 8i, 2, n 2<D; WriteEquations;
EquationList = AppendAEquationList, ExpandA8x,2< S@0, xD . x t@0DE
0E;
EquationList = AppendAEquationList, ExpandA8x,2< S@n 1, xD . x t@nDE
0E;
EquationList = Append@EquationList,
Expand@x S@0, xD . x t@0DD
LeftDerivD; EquationList =
Append@EquationList, Expand@x S@n 1, xD . x t@nDD
RightDerivD;
MakeUnknowns; Unknowns = Prepend@Unknowns, a@n 1DD;
Unknowns = Prepend@Unknowns, a@1DD; SolvetheBSplineSystemE
PeriodicSpline@slist_List, opts___RuleD :=
BlockB8i, plotpoints, X, Y<, Clear@a, b, c, dD;
plotpoints = PlotPoints . 8opts< . Options@PlotD; SplineData = slist;
MinNumberofPoints = 3; If@Length@SplineDataD < MinNumberofPoints,
Message@CubicSpline::"smallnerr", Length@SplineDataD,
MinNumberofPointsD; Return@Hold@NaturalCubicSpline@dataDDDD;
n = Length@SplineDataD; Do@t@iD = i + 1; a@iD = SplineDataPi + 1, 1T;
aa@iD = SplineDataPi + 1, 2T, 8i, 0, n 1<D; t@nD = n + 1; a@nD = a@0D;
WriteandSolvePeriodicEquations; title = "PeriodicCubicSpline";
t@i + 1D t@iD
DoBX@iD = DropBTableBN@S@i, xDD, :x, t@iD, t@i + 1D, >F, 1F,
plotpoints
8i, 0, n 2<F;
t@nD t@n 1D
X@n 1D = TableBN@S@n 1, xDD, :x, t@n 1D, t@nD, >F;
plotpoints
AbscissaPoints = Flatten@Table@X@iD, 8i, 0, n 1<D, 1D; Clear@a, b, c, dD;
Do@a@iD = aa@iD, 8i, 0, n 1<D; a@nD = a@0D; WriteandSolvePeriodicEquations;
t@i + 1D t@iD
DoBY@iD = DropBTableBN@S@i, xDD, :x, t@iD, t@i + 1D, >F, 1F,
plotpoints
8i, 0, n 2<F;
t@nD t@n 1D
Y@n 1D = TableBN@S@n 1, xDD, :x, t@n 1D, t@nD, >F;
plotpoints
OrdinatePoints = Flatten@Table@Y@iD, 8i, 0, n 1<D, 1D; SplineData = Table@
8AbscissaPointsPiT, OrdinatePointsPiT<, 8i, Length@OrdinatePointsD<D;
ListPlot@SplineData, Joined True, PlotLabel titleDF
B
NumericalExamples.nb 44