Taylor Series Method
7.2 Taylor Series Method
The Taylor series method is conceptually simple and capable of high accuracy. Its basis is the truncated Taylor series for y about x:
y(x + h) ≈ y(x) + y (x)h + y ′′ (x)h 2 1 + 1 y ′′′ (x)h 3 +···+ y (m) (x)h m (7.6)
n! Because Eq. (7.6) predicts y at x + h from the information available at x, it is also a
formula for numerical integration. The last term kept in the series determines the order of integration. For the series in Eq. (7.6) the integration order is m.
The truncation error, due to the terms omitted from the series, is
E=
y (m+1) (ξ )h m+1 , x<ξ<x+h
(m+ 1)!
Using the finite difference approximation
y (m)
(m)
y (m+1)
(x + h) − y (x)
we obtain the more usable form
which could be incorporated in the algorithm to monitor the error in each integration step.
The function taylor implements the Taylor series method of integration of order four. It can handle any number of first-order differential equations y ′ i =f i (x, y 1 , y 2 ,..., y n ),
i = 1, 2, . . . , n. The user is required to supply the function deriv that returns the 4 × n array
(y ⎤ ) y ′
1 y ′ 2 ···y n ′
⎢ (y ′′ ) T ⎥ ⎢ ′′
2 ···y n ⎥
1 y 2 ···y n ⎦
(y (4) ) T
y (4) y (4) ···y (4) 1 2 n
The function returns the arrays xSol and ySol that contain the values of x and y at intervals h.
function [xSol,ySol] = taylor(deriv,x,y,xStop,h) % 4th-order Taylor series method of integration. % USAGE: [xSol,ySol] = taylor(deriv,x,y,xStop,h) % INPUT: % deriv = handle of function that returns the matrix %
d = [dy/dx dˆ2y/dxˆ2 dˆ3y/dxˆ3 dˆ4y/dxˆ4]. % x,y
= initial values; y must be a row vector. % xStop = terminal value of x %h
= increment of x used in integration (h > 0). % OUTPUT: % xSol
= x-values at which solution is computed. % ySol
= values of y corresponding to the x-values.
if size(y,1) > 1; y = y’; end % y must be row vector xSol = zeros(2,1); ySol = zeros(2,length(y)); xSol(1) = x; ySol(1,:) = y; if size(y,1) > 1; y = y’; end % y must be row vector xSol = zeros(2,1); ySol = zeros(2,length(y)); xSol(1) = x; ySol(1,:) = y;
h = min(h,xStop - x); d = feval(deriv,x,y);
% Derivatives of [y] hh = 1; for j = 1:4
% Build Taylor series hh = hh*h/j;
% hh = hˆj/j!
y = y + d(j,:)*hh; end x = x + h; k = k + 1; xSol(k) = x; ySol(k,:) = y; % Store current soln.
end
This function prints the results xSol and ySol in tabular form. The amount of data is controlled by the printout frequency freq . For example, if freq = 5 , every fifth integration step would be displayed. If freq = 0 , only the initial and final values will
be shown.
function printSol(xSol,ySol,freq) % Prints xSol and ySoln arrays in tabular format. % USAGE: printSol(xSol,ySol,freq) % freq = printout frequency (prints every freq-th %
line of xSol and ySol).
[m,n] = size(ySol); if freq == 0;freq = m; end head = ’
x’;
for i = 1:n head = strcat(head,’
y’,num2str(i)); end fprintf(head); fprintf(’\n’) for i = 1:freq:m
fprintf(’%14.4e’,xSol(i),ySol(i,:)); fprintf(’\n’) end if i ˜= m; fprintf(’%14.4e’,xSol(m),ySol(m,:)); end
EXAMPLE 7.1 Given that EXAMPLE 7.1 Given that
Solution The Taylor series up to and including the term with h 4 is
y(h) = y(0) + y ′ (0)h + y ′′ (0)h 2 2! 1 + y ′′′ (0)h 3 1 y (4) (0)h 4 3! (a) + 4! Differentiation of the differential equation yields
y ′ = −4y + x 2 y ′′
= −4y 2 ′ + 2x = 16y − 4x + 2x
y ′′′ = 16y ′ − 8x + 2 = −64y + 16x 2 − 8x + 2
= −64y 2 ′ + 32x − 8 = 256y − 64x + 32x − 8 Thus
y (4)
y ′ (0) = −4(1) = −4 y ′′ (0) = 16(1) = 16 y ′′′ (0) = −64(1) + 2 = −62
y (4) (0) = 256(1) − 8 = 248
With h = 0.2 Eq. (a) becomes
1 2 1 3 1 4 y(0.2) = 1 + (−4)(0.2) + (16)(0.2) 2! + 3! (−62)(0.2) + (248)(0.2) 4!
= 0.4539 According to Eq. (7.7) the approximate truncation error is
y (4) (0.2) = 256(0.4539) − 64(0.2) 2 + 32(0.2) − 8 = 112.04 Therefore,
The analytical solution yields
32 4 − 8 (0.2) + 32 = 0.4515 so that the actual error is 0.4515 − 0.4539 = −0.0024. EXAMPLE 7.2 Solve
y(0.2) =
e + (0.2)
y ′′ = −0.1y ′ −x y(0) = 0 y ′ (0) = 1 from x = 0 to 2 with the Taylor series method of order four using h = 0.25.
Solution With y 1 = y and y 2 =y ′ the equivalent first-order equations and initial con- ditions are
y(0) =
1 Repeated differentiation of the differential equations yields
2 −0.1y 2 −x
y ′ 2 −0.1y 2 −x
y ′′
−0.1y 2 −1
0.01y 2 + 0.1x − 1
−0.001y 2 − 0.01x + 0.1
−0.001y 2 − 0.01x + 0.1
0.0001y 2 + 0.001x − 0.01 Thus the derivative array required by taylor is
−0.001y 2 − 0.01
y ⎤ 2 −0.1y 2 −x
0.01y 2 + 0.1x − 1 ⎥ d= ⎥
−0.1y 2 −x
0.01y 2 + 0.1x − 1
−0.001y 2 − 0.01x + 0.1 ⎦ −0.001y 2 − 0.01x + 0.1 0.0001y 2 + 0.001x − 0.01
which is computed by function d = fex7 _ 2(x,y)
% Derivatives used in Example 7.2
d = zeros(4,2); d(1,1) = y(2); d(1,2) = -0.1*y(2) - x; d(2,1) = d(1,2); d = zeros(4,2); d(1,1) = y(2); d(1,2) = -0.1*y(2) - x; d(2,1) = d(1,2);
Here is the solution: >> [x,y] = taylor(@fex7 _
>> printSol(x,y,1) x
The analytical solution of the problem is
y = 100x − 5x 2 + 990(e −0.1x − 1)
from which we obtain y(2) = 0.543 45 and y ′ (2) = −1.0543, which agree with the nu- merical solution.
The main drawback of the Taylor series method is that it requires repeated differ- entiation of the dependent variables. These expressions may become very long and thus error-prone and tedious to compute. Moreover, there is the extra work of coding each of the derivatives.