1
8.15 Use Lagrange polynomials to develop a difference formula for the second derivative of a function
that is specified by a discrete set of data points with unequal spacing. The formula determines the second
derivative at point using points , , and .
Solution
Using Eq. (6.45):
xiyi
,()
xi1yi1
,()
xiyi
,()
xi1+ yi1+
,()
1
8.16 Using Lagrange polynomials, develop a difference formula for the third derivative of a function that
is specified by a discrete set of data points. The formula determines the third derivative at point
using points , , , and . The points are spaced such that
and .
Solution
xiyi
,()
xi1yi1
,()
xiyi
,()
xi1+ yi1+
,()
xi2+ yi2+
,()
ixi1
xi2+ xi1+
==
xi1+ xi
–2h=
2
1
8.17 Write a MATLAB user-defined function that determines the first derivative of a function that is
given by a set of discrete points with equal spacing. For the function name use yd = First-
Deriv(x,y). The input arguments x and y are vectors with the coordinates of the points, and the output
argument yd is a vector with the values of the derivative at each point. At the first and last points, the func-
tion should calculate the derivative with the three-point forward and backward difference formulas, respec-
tively. At all the other points FirstDeriv should use the two-point central difference formula. Use
FirstDeriv to calculate the derivative of the function that is given in Problem 8.1.
Solution
The following user-defined MATLAB function solves this problem:
function yd = FirstDeriv(x,y)
n=length(x); h=x(2)-x(1);
for i=2:n-1
yd(i)=(y(i+1)-y(i-1))/2/h;
1
8.18 Write a MATLAB user-defined function that calculates the second derivative of a function that is
given by a set of discrete data points with equal spacing. For the function name and arguments use
ydd=SecDeriv(x,y), where the input arguments x and y are vectors with the coordinates of the
points, and ydd is a vector with the values of the second derivative at each point. For calculating the sec-
ond derivative, the function SecDeriv should use the finite difference formulas that have a truncation
error of . Use SecDeriv for calculating the second derivative of the function that is given by the
following set of points:
Solution
From Table 8-1, the second derivative at the interior points can be calculated using the three-point central
difference formula:
–1 –0.5 00.5 11.5 22.5 33.5 44.5
–3.632 –0.3935 10.6487 1.282 –4.518 –8.611 –12.82 –15.91 –15.88 –9.402 9.017
Oh
2
()
x
fx()
f′′ xi
() fx
i1
()2fx
i
()fx
i1+
()+
————————————————————
=
2
The user-defined function SecDeriv is next used in the Command Window for calculating the derivative
of the function that is given.
>> format long
>> x=-1:0.5:4.5;
>> y=[-3.632 -0.3935 1 0.6487 -1.282 -4.518 -8.611 -12.82 -15.91 -15.88
-9.402 9.017];
1
8.19 Write a MATLAB user-defined function that determines the first and second derivatives of a func-
tion that is given by a set of discrete points with equal spacing. For the function name use [yd,ydd] =
FrstScndDeriv(x,y). The input arguments x and y are vectors with the coordinates of the points,
and the output arguments yd and ydd are vectors with the values of the first and second derivatives,
respectively, at each point. For calculating both derivatives, the function should use the finite difference
formulas that have a truncation error of .
(a) Use the function FrstScndDeriv to calculate the derivatives of the function that is given by the
data in Problem 8.18.
(b) Modify the function (rename it FrstScndDerivPt) such that it also creates three plots (one page in
a column). The top plot should be of the function, the second plot of the first derivative, and the third
of the second derivative. Apply the function FrstScndDerivPt to the data in Problem 8.18.
Solution
(a) The user-defined function FrstScndDeriv is listed below.
Oh
2
()
2
(b) The function from part (a) is modified such that it also creates three plots (on the same page in a col-
umn). The top plot should be of the function, the second plot of the first derivative, and the third of the sec-
ond derivative. The listing of the function is:
function [yd,ydd] = FrstScndDerivPt(x,y)
n=length(x); h=x(2)-x(1);
for i=2:n-1
yd(i)=(y(i+1)-y(i-1))/2/h;
ydd(i)=(y(i-1)-2*y(i)+y(i+1))/h/h;
3
When the function is used for calculating the derivative of the func-
tion given in Problem 8.18, the display is the same as in Part (a). In
addition, the figure on the right is displayed in the Figure Window:
−2 0 2 4 6
−20
−10
0
10
x
f(x)
1
8.20 Write a MATLAB user-defined function that determines the first derivative of a function that is
given in an analytical form. For the function name and arguments use dfx = DiffAnaly(Fun,xi).
Fun is a name for the function that is being differentiated. It is a dummy name for the function that is
imported into DiffAnaly. The actual function that is differentiated should be written as an anonymous
function, or as a user-defined function, that calculates the values of for given values of x. It is entered
as a function handle when DiffAnaly is used. xi is the value of x where the derivative is calculated. The
user-defined function should calculate the derivative by using the two-point central difference formula. In
the formula, the values of and should be taken to be 5% higher and 5% lower than the value
of , respectively.
(a)Use DiffAnaly to calculate the first derivative of at .
(b)Use DiffAnaly to calculate the first derivative of the function from Problem 8.12 at .
Solution
The listing of the user-defined function DiffAnaly is:
function dfx = DiffAnaly(Fun,xi)
h=0.05*xi;
dfx=(Fun(xi+h)-Fun(xi-h))/(2*h);
fx()
xi1+
()
xi1
()
xi
()
fx() exxln=
x2=
x2=
1
8.21 Modify the MATLAB user-defined function in Problem 8.20 to include Richardson’s extrapolation.
The function should calculate a first estimate for the derivative as described in Problem 8.20, and a second
estimate by taking the values of and to be 2.5% higher and 2.5% lower than the value of ,
respectively. The two estimates should then be used with Richardson’s extrapolation for calculating the
derivative. For the function name and arguments use dfx=DiffRichardson(Fun,xi).
(a) Use the function to calculate the derivative of at .
(b) Use the function to calculate the first derivative of the function that is given in Problem 8.12 at .
Solution
Richardson’s extrapolation for numerical differentiation is given by Eq.(8.45):
Parts a and b are solved in the following script file:
clear, clc
% Part a
Fa=@(x) exp(x)*log(x);
When the script is executed the following results are displayed in the Command Window.
dFa =
xi1+
()
xi1
()
xi
()
fx() exxln=
x2=
x2=
D1
3
4Dh
2

 Dh()


Oh
4
()+=
1
8.22 Write a MATLAB user-defined function that calculates the second derivative of a function that is
given in an analytical form. For the function name and arguments use ddfx = DDiffAnaly(Fun,xi).
Fun is a name for the function that is being differentiated. It is a dummy name for the function that is
imported into DiffAnaly. The actual function that is differentiated should be written as an anonymous
function, or as a user-defined function, that calculates the values of for given values of x. It is entered
as a function handle when DiffAnaly is used. xi is the value of x where the second derivative is calcu-
lated. The function should calculate the second derivative with the three-point central difference formula.
In the formula, the values of and should be taken to be 5% higher and 5% lower than the
value of , respectively.
(a) Use the function to calculate the second derivative of at .
(b) Use the function to calculate the second derivative of the function that is given in Problem 8.12 at
.
Solution
The listing of the user-defined function DDiffAnaly is:
function ddfx = DDiffAnaly(Fun,xi)
h=0.05*xi;
ddfx=(Fun(xi-h)-2*Fun(xi)+Fun(xi+h))/(h^2);
fx()
xi1+
()
xi1
()
xi
()
fx() 2xx=
x2=
x2=
1
8.23 Write a MATLAB user-defined function that evaluates the first derivative of a function that is given
by a set of discrete points with unequal spacing. For the function name use yd = FirstDeriv-
Uneq(x,y). The input arguments x and y are vectors with the coordinates of the points, and the output
argument yd is a vector with the values of the first derivative at each point. The differentiation is done by
using second-order Lagrange polynomials (Section 8.5). At the first and last points, the function should
calculate the derivative with Eqs. (8.35) and (8.37), respectively. At all the other points the function should
use Eq. (8.36). Use FirstDerivUneq to calculate the derivative of the function that is given by the fol-
lowing set of points:
Solution
The listing of the user-defined function FirstDerivUneq is:
function yd = FirstDerivUneq(x,y)
n=length(x);
yd(1)=(2*x(1)-x(2)-x(3))*y(1)/(x(1)-x(2))/(x(1)-x(3))
+(x(1)-x(3))*y(2)/(x(2)-x(1))/(x(2)-x(3))
–1 –0.6 –0.3 00.5 0.8 1.6 2.5 2.8 3.2 3.5 4
–3.632 –0.8912 0.3808 1.0 0.6487 –0.3345 –5.287 –12.82 –14.92 –16.43 –15.88 –9.402
x
fx()
2
-9.402];
dFx=FirstDerivUneq(x,Fx)
When the script is executed the following results are sisplayed in the Command Window.
1
8.24 Write a MATLAB user-defined function that evaluates the second derivative of a function that is
given by a set of discrete data points with unequal spacing. For the function name use yd = SndDeriv-
Uneq(x,y). The input arguments x and y are vectors with the coordinates of the points, and the output
argument yd is a vector with the values of the second derivative at each point. Use the following scheme
for the differentiation. Write a third-order Lagrange polynomial for four points , , ,
and , and derive formulas for the second derivative of the polynomial at each of the four points.
SndDerivUneq uses the formula for for calculating the second derivative at the first data
point, and the formula for and for calculating the second derivative at the last data
point and one point before the last, respectively. The formula for is used in all the points in
between. Use the function to calculate the second derivative of the function that is given by the following
set of points:
Solution
A third-order Lagrange polynomial for four points , , , and is:
–1 –0.6 –0.3 00.5 0.8 1.6 2.5 2.8 3.2 3.5 4
–3.632 –0.8912 0.3808 1.0 0.6487 –0.3345 –5.287 –12.82 –14.92 –16.43 –15.88 –9.402
fx()
xi1
()
xi
()
xi1+
()
xi2+
()
fxi1
()
fxi2+
()
fxi1+
()
fxi
()
x
fx()
fx()
xi1
()
xi
()
xi1+
()
xi2+
()
fx() xx
i
()xx
i1+
()xx
i2+
()
xi1xi
()xi1xi1+
()xi1xi2+
()
—————–—————-————————————————-——yi1
xx
i1
()xx
i1+
()xx
i2+
()
xixi1
()xixi1+
()xixi2+
()
—————–———————————————————–yi
++=
2
Second differentiation of the equation above gives:
The second derivative at the four points , , , and is:
The listing of the user-defined function SndDerivUneq is:
xi1
()
xi
()
xi1+
()
xi2+
()
fxi1
() 6xi12xi1+ xixi2+
++()
xi1xi
()xi1xi1+
()xi1xi2+
()
—————–———————————————-————————-yi1
4xi12xi1+ xi2+
+()
xixi1
()xixi1+
()xixi2+
()
—————–———————————————————–yi
++=
4xi12xixi2+
+()
xi1+ xi1
()xi1+ xi
()xi1+ xi2+
()
—————————————————————-————————-yi1+
4xi12xixi1+
+()
xi2+ xi1
()xi2+ xi
()xi2+ xi1+
()
——————-—————————————————————-——yi2+
+
3
function yd = SndDerivUneq(x,y)
n=length(x);
for j=2:n-2
yd(j)=(4*x(j)-2*(x(j+1)+x(j+2)))*y(j-1)/((x(j-1)-x(j))*(x(j-1)-
x(j+1))*(x(j-1)-x(j+2)))
+(6*x(j)-2*(x(j+1)+x(j-1)+x(j+2)))*y(j)/((x(j)-x(j-1))*(x(j)-
x(j+1))*(x(j)-x(j+2)))
+(4*x(j)-2*(x(j-1)+x(j+2)))*y(j+1)/((x(j+1)-x(j-1))*(x(j+1)-x(j))*(x(j+1)-
x(j+2)))
+(4*x(j)-2*(x(j-1)+x(j+1)))*y(j+2)/((x(j+2)-x(j-1))*(x(j+2)-x(j))*(x(j+2)-
x(j+1)));
end
yd(n-1)=(4*x(n-1)-2*(x(n-2)+x(n)))*y(n-3)/((x(n-3)-x(n-2))*(x(n-3)-x(n-
1))*(x(n-3)-x(n)))
+(4*x(n-1)-2*(x(n-3)+x(n)))*y(n-2)/((x(n-2)-x(n-3))*(x(n-2)-x(n-1))*(x(n-
4
The user-defined function SndDerivUneq is next used in the script file for calculating the derivative of
function given in the problem statement:
clear, clc
When the script is executed the following results are sisplayed in the Command Window.
ddFx =
-7.6933 -7.4419 -7.2533 -7.0037 -6.2943 -5.9801 -2.8063
4.8824 8.5333 17.0057 25.8429 40.5714
1
8.25 Write a MATLAB user-defined function that evaluates the partial first derivatives and of a
function that is specified by discrete tabulated points with equal spacing. Use two-point central dif-
ference formulas at the interior points and one-sided three-point forward and backward difference formulas
at the endpoints. For the function name use [dfdx,dfdy]= ParDer(x,y,f). The input arguments x
and y are vectors with the values of the independent variables. f is a vector with the value of f at each
point. The output arguments dfdx and dfdy are vectors with the values of the partial derivatives at each
point. Use ParDer to calculate the partial derivatives with respect to x and y of the function given in Prob-
lem 8.14.
Solution
Since the points are equally spaced, we can use the formulae from Table 8-1 in each coordinate
direction. For the interior points, the three-point central difference formulae are:
f
x
—-
f
y
—-
fxy,()
2
The following user-defined function calculates the partial derivatives according to the above
formulae:
end
end
% use one-sided four-point forward and bacward differences at the end
% points:
for j=1:m
dfdx2(1,j)=(2*f(1,j)-5*f(2,j)+4*f(3,j)-f(4,j))/(deltax^2);
dfdx2(n,j)=(2*f(n,j)-5*f(n-1,j)+4*f(n-2,j)-f(n-3,j))/(deltax^2);
When executed for the function specified in Problem (8.14), the following output is produced:
2
ni1ni2ni3ni
)y,x(
2
2
)y(
)y,x(f2)y,x(f5)y,x(f4)y,x(f
y
f
ni Δ
++
=