Sie sind auf Seite 1von 38

CSU Vision CSU Mission Core Values CSU IGA

Transforming lives by CSU is committed to transform the lives Productivity Compassion Competent
Educating for the BEST of people and communities through high Accessibility Accountability Self-disciplined
quality instruction and innovative Relevance Universally-adept
research, development, production and Excellence
extension.

Republic of the Philippines


Cagayan State University
COLLEGE OF ENGINEERING
Carig Sur, Tuguegarao City

DEPARTMENT OF CHEMICAL ENGINEERING

Advanced Engineering Mathematics for ChE


(ChE 57)
First Semester 2016 – 2017

Course Topic: NUMERICAL DIFFERENTIATION USING MATLAB®

Course Activity: TERM PAPER

Name of Students: REMIGIO,EDRICKSON C.


SAVELLANO, YVETTE XYRA R.
SIA, JOHN PATRIC R.
SILVA, JULIE ANNE A.
SIMANGAN, JOHN-RUSTOM M.

Program: B.S. In Chemical Engineering

Year Level: 3rd YEAR

Date Submitted:

Instructor: Engr. CAESAR P. LLAPITAN Rating: ________

Date Checked: ________


TABLE OF CONTENTS
I. Introduction .............................................................................................................. 2
II. High Accuracy Differentiation Formulas ............................................................... 2
A. Theoretical Background .................................................................................................. 3
B. Numerical Analysis Using MATLAB® ............................................................................. 8
1. Backward Finite-difference ................................................................................................................... 8
a) Algorithm .......................................................................................................................................... 8
b) Flow Chart ........................................................................................................................................ 8
c) M-file to implement Backward Finite Difference ........................................................................... 10
d) Sample Problems ............................................................................................................................ 11
2. Centered Finite Difference .................................................................................................................. 16
a) Algorithm ........................................................................................................................................ 16
b) Flow Chart ...................................................................................................................................... 16
c) M-file implement Centered Finite Difference................................................................................. 18
d) Sample Problem .............................................................................................................................. 19
3. Forward Finite-difference ................................................................................................................... 24
a) Algorithm ........................................................................................................................................ 24
b) Flow Chart ...................................................................................................................................... 24
c) M-file to implement Forward Finite Difference ............................................................................. 26
d) Sample Problems ............................................................................................................................ 27

C. Conclusion ..................................................................................................................... 29

III. Richardson Extrapolation .................................................................................... 30


A. Theoretical Background ................................................................................................ 30
B. Numerical Analysis using MATLAB® ............................................................................ 31
1. Algorithm ............................................................................................................................................ 31
2. Flow Chart........................................................................................................................................... 31
3. M-file to Implement Richardson Interpolation ................................................................................... 33
4. Sample Problems ................................................................................................................................. 33

C. Conclusion ..................................................................................................................... 36

IV. Generalization ..................................................................................................... 37


V. References ........................................................................................................... 37

1
I. Introduction

In many areas of engineering and sciences, differentiation has a wide range of


application. Beyond direct engineering and scientific applications, numerical differentiation
is also important in a variety of general mathematical contexts including other areas of
numerical methods. Most important application of numerical differentiation involves the
solution of differential equations. When the functions to be analysed are simple, it can be
evaluated analytically. However, it is often difficult or impossible when the function is
complicated. In addition, the underlying function is often unknown and defined only by
measurement at discrete points.

For these cases, for them to be differentiated, one must understand the application of
high-accuracy numerical differentiation formulas for equi-spaced data. They should know
how to evaluate derivatives for unequally spaced data; understand how Richardson
extrapolation is applied for numerical differentiation; recognize the sensitivity of numerical
differentiation to data error; and know how to evaluate derivatives in MATLAB with the
‘diff’ and ‘gradient’ functions and must know how to generate contour plots and vector fields
with MATLAB to obtain approximate values for derivatives.

II. High Accuracy Differentiation Formulas


Calculus is the mathematics of change. Because engineers and scientists must
continuously deal with systems and processes that change, calculus is an essential tool of our
profession. Standing at the heart of calculus is the mathematical concept of differentiation.
According to the dictionary definition, to differentiate means “to mark off by differences;
distinguish; . . . to perceive the difference in or between.” Mathematically, the derivative,
which serves as the fundamental vehicle for differentiation, represents the rate of change of a
dependent variable with respect to an independent variable. As depicted in Fig. 21.1, the
mathematical definition of the derivative begins with a difference approximation:
y f ( xi  x)  f ( xi )
 Eq. 1
x x

where y and f (x) are alternative representatives for the dependent variable and x is the
independent variable. If ∆x is allowed to approach zero, as occurs in moving from Fig. 21.1a
to c, the difference becomes a derivative:
dy f ( xi  x)  f ( xi )
 lim Eq. 2
dx x  0 x

2
where dy/dx [which can also be designated as y’ or f ‘(xi )] is the first derivative of y with
respect to x evaluated at xi. The second derivative represents the derivative of the first
derivative,
d2y d  dy 
   Eq. 3
dx 2
dx  dx 

Thus, the second derivative tells us how fast the slope is changing. It is commonly referred to
as the curvature, because a high value for the second derivative means high curvature.
Finally, partial derivatives are used for functions that depend on more than one variable.
Partial derivatives can be thought of as taking the derivative of the function at a point with all
but one variable held constant.

A. Theoretical Background
We will now illustrate how high-accuracy finite-difference formulas can be generated
by including additional terms from the Taylor series expansion.
The forward Taylor series expansion can be written as
f ' ' ( xi ) 2
f ( xi  1)  f ( xi )  f ' ( xi )h  h  ... Eq. 6
2!
which can be solved for
f ( xi  1)  f ( xi ) f ' ' xi 
f ' x i    h  O(h 2 ) Eq. 7
h 2!
Truncating this result by excluding the second- and higher-derivative term and were
thus left with a forward-difference formula:
f ( xi  1)  f ( xi )
f 'xi    O(h) Eq. 8
h

In contrast to this approach, we now retain the second-derivative term by substituting


the following forward-difference approximation of the second derivative:
f ( xi  2)  2 f ( xi  1)  f ( xi )
f ' ' x i    O ( h) Eq. 9
h2
into Eq. 7 to yield
f ( xi  1)  f ( xi ) f ( xi  2)  2 f ( xi  1)  f ( xi )
f ' x i    h  O( h 2 )
h 2h 2 Eq. 10
or, by collecting terms:
 f ( xi  2)  4 f ( xi  1)  3 f ( xi ) Eq. 11
f 'xi    O(h 2 )
2h

3
It can be seen that the inclusion of the second-derivative term has improved the
accuracy to O(h2). Similar improved versions can be developed for the backward and
centered formulas as well as for the approximations of higher-order derivatives.
The Taylor series expansion can be used to derive numerical estimates of higher
derivatives. To do this, we write a forward Taylor series expansion for f xi  2  in terms of

f  xi  :

f ' '  xi 
f xi 2   f xi   f ' xi h  2h2  ... Eq. 12
2!
Equation 6 can be multiplied by 2 and subtracted from Eq.12 to give
f ' '  xi 
f xi 2   f xi   f ' xi h  2h2  ... Eq. 13
2!
Which can be solved for
f ( xi  2 )  2 f ( xi 1 )  f ( xi ) Eq. 14
f " ( xi )   O(h)
h2
This relationship is called the second forward finite difference. Similar manipulations
can be employed to derive a backward version
f ( xi  2 )  2 f ( xi 1 )  f ( xi 2 )
f " ( xi )   O(h) Eq. 15
h2
A centered difference approximation for the second derivative can be derived by
adding Eqs. (6) and (4.24) and rearranging the result to give

f ( xi 1 )  2 f ( xi )  f ( xi 1 )
f " ( xi )  2
 O(h 2 ) Eq. 16
h

As was the case with the first-derivative approximations, the centered case is more
accurate.
Notice also that the centered version can be alternatively expressed as
f ( xi 1 )  f ( xi ) f ( xi 1 )
 Eq. 17
f " ( xi )  h h
h
Thus, just as the second derivative is a derivative of a derivative, the second finite
difference approximation is a difference of two first finite differences.
The following figures show the derived equations for higher order derivatives for
backward, centered, and forward finite differences.

4
First Derivative

f(xi )  f(xi 1 )
f ' (x)  O(h)
h
3 f(xi )  4 f(xi 1 )  f(xi  2 ) O(h2)
f ' (x) 
h

Second Derivative

f(x i )  2 f(x i 1 )  f(x i  2 ) O(h)


f''(x)  2
h
2 f(x i )  5 f(x i 1 )  4 f(x i  2 )  f(x i 3 ) O(h2)
f''(x) 
h2

Third Derivative

f(xi )  3 f(xi 1 )  3 f(xi  2 )  f(xi 3 )


f'''(x) 
h3 O(h)
5 f(xi )  18 f(xi 1 )  24 f(xi  2 )  14 f(xi 3 )  3 f(xi  4 )
f'''(x)  O(h2)
2 h3

Figure 1 Backward finite-difference formulas: two versions are presented for each
derivative. The latter version incorporates more terms of the Taylor series
expansion and is, consequently, more accurate.

5
First Derivative
f ( xi 1 )  f ( xi )
f ' ( xi )  O(h)
h
 f ( xi  2 )  4 f ( xi 1 )  f ( xi )
f ' ( xi )  O(h2)
2h

Second Derivative

f ( xi  2 )  2 f ( xi 1 )  f ( xi )
f '' ( xi )  O(h)
h2
 f ( xi 3 )  4 f ( xi  2 )  5 f ( xi 1 )  2 f ( xi ) O(h2)
f '' ( xi ) 
h2

Third Derivative

f ( xi 3 )  3 f ( xi  2 )  3 f ( xi 1 )  f ( xi )
f '' ' ( x i ) 
h3 O(h)
 3 f ( xi  4 )  14 f ( xi 3 )  24 f ( xi  2 )  18 f ( xi 1 )  5 f ( xi )
f '' ' ( x i ) 
2h 3 O(h2)

Figure 2 Forward finite-difference formulas: two versions are presented for each
derivative. The latter version incorporates more terms of the Taylor series
expansion and is, consequently, more accurate.

6
Centered finite-difference formula

First derivative

f ( xi 1 )  f ( xi 1 ) O(h2)
f ' ( xi ) 
2h
 f ( xi  2 )  8 f ( xi 1 )  8 f ( xi 1 )  f ( xi  2 ) O(h4)
f ' ( xi ) 
12h

Second derivative

f ( xi 1 )  2 f ( xi )
f ' ' ( xi )  O(h2)
h2
 f ( x i  2 )  16 f ( xi 1 )  30 f ( xi )  16 f ( xi 1 )  f ( xi  2 )
f ' ' ( xi )  O(h4)
12h 2

Third derivative

f ( xi  2 )  2 f ( xi 1 )  2 f ( xi 1 )  f ( xi  2 ) O(h2)
f ' ' ' ( xi ) 
2h 3
 f ( xi 3 )  8 f ( xi  2 )  13 f ( xi 1 )  13 f ( xi 1 )  8 f ( xi 1 )  f ( xi 3 )
f ' ' ' ( xi )  O(h4)
8h 3

Figure 3 Centered finite-difference formulas: two versions are presented for each
derivative. The latter

7
B. Numerical Analysis Using MATLAB®

1. Backward Finite-difference

a) Algorithm
Step 1. Start

Step 2. Differentiate the given function

Step 3. Solve the values of f(xi), f(xi-1), f(xi-2), f(xi-3), and f(xi-4)

Step 4. Substitute the values to the equations of f’(x), f’’(x), f’’’(x) and
compute the estimate value

Step 5. Compute for the relative error of the estimates.

Step 6. Print f’(x), f’’(x), f’’’(x), and relative error

Step 7. End

b) Flow Chart
Starting the program by setting the initial input variables namely the original
function –f(x), to be evaluated –x, and the step size –h. After that, differentiate the
given function and Solve its value at x. Then solve for the values of f(xi), f(xi-1),
f(xi-2), f(xi-3), and f(xi-4).Then substitute the computed values to the equations of
f’(x), f’’(x), f’’’(x) to compute for the estimate value. To compute for the relative
error of the estimates, the difference between the true value and the estimated value is
divided by the estimated value and multiplied by 100. Finally, display the
approximate values and the relative error of each approximate

8
Start

f(x)=0
x=0
h=0

Read f(x), x and h

Differentiate f(x)

Compute for the values of f(xi), f(xi-1), f(xi-2), f(xi-3), and f(xi-4)

First Derivative
f'(x)= (f(xi)-f(xi-1))/h
f'(x)= (3f(xi)-4f(xi-1)+f(xi-2))/2h
Second Derivative
f''(x)= (f(xi)-2f(xi-1)+f(xi-2))/h2
f''(x)= (2f(xi)-5f(xi-1)+4f(xi-2)-f(xi-3))/h2
Third Derivative
f'''(x)= (f(xi)-3f(xi-1)+3f(xi-2)-f(xi-3))/h3
f'''(x)= (5f(xi)-18f(xi-1)+24f(xi-2)-14f(xi-3)+3f(xi-4))/2h3

Percent error=[(true value - approximate value)/true


value ]100%

Print f’(x), f’’(x), f’’’(x) and Percent error

END

Figure 4 Flow Chart for Backward Finite Difference

9
c) M-file to implement Backward Finite Difference

% original function to be evaluated at xi with a step size of h


f=@(x) {original function};
dy=@(x){ first derivative }; % actual first derivative
d2y=@(x) {second derivative};
d3y=@(x){ third derivative};
h=0; % step size
xi=0; %value at which the differential is to be evaluated
%Backward finite-difference approximation of O(h)
true_dy= dy(xi); %true value of first derivative
true_d2y= d2y(xi);%true value of the second derivative
true_d3y= d3y(xi);%true value of the third derivative
first_BackwardOh= ( f(xi)-f(xi -h))./h;
second_BackwardOh=(f(xi -2*h)-2*f(xi -h) +f(xi))./(h^2);
third_BackwardOh=(f(xi)-f(xi -3*h)+ 3*f(xi -2*h)-3*f(xi -h))./(h^3);
%relative per cent error
ERRdy=100*(true_dy-first_BackwardOh)./true_dy;
ERRd2y=100*(true_d2y-second_BackwardOh)./true_d2y;
ERRd3y=100*(true_d3y-third_BackwardOh)./true_d3y;
% Construct a 1 x 3 matrix of zeros
z=zeros(1,3); y=zeros(1,3); E=zeros(1,3);
z(:,1)=first_BackwardOh; y(:,1)=true_d2y; E(:,1)=ERRdy;
z(:,2)=second_BackwardOh; y(:,2)=true_d2y; E(:,2)=ERRd2y;
z(:,3)=third_BackwardOh; y(:,3)=true_d3y; E(:,3)=ERRd3y;
disp('Backward finite-difference approximation of first degree (O(h))')
fprintf(' \n');
fprintf('\t\t\t\t First Derivative\tSecond Derivative\t Third Derivadive
\t\n')
fprintf(' \n');
fprintf('Approximation\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',z')
fprintf(' \n');
fprintf('True Value\t \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',y')
fprintf(' \n');
fprintf('Percent Error \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',E')
fprintf(' \n');
%Backward finite-difference approximation of O(h^2)
first_BackwardOh2=(3*f(xi)-4*f(xi -h)+f(xi-2*h))/(2*h);
second_BackwardOh2=(-f(xi-3*h)+4*f(xi -2*h)-5*f(xi -h) +2*f(xi))/(h^2);
third_BackwardOh2=(5*f(xi)+3*f(xi-4*h)-14*f(xi -3*h)+ 24*f(xi -2*h)-18*f(xi -
h))/(2*h^3);
ERR2dy=100*(true_dy-first_BackwardOh2)/true_dy;
ERR2d2y=100*(true_d2y-second_BackwardOh2)/true_d2y;
ERR2d3y=100*(true_d3y-third_BackwardOh2)/true_d3y;
%relative per cent error
z2=zeros(1,3); y2=zeros(1,3); E2=zeros(1,3);
z2(:,1)=first_BackwardOh2; y2(:,1)=true_dy; E2(:,1)=ERR2dy;
z2(:,2)=second_BackwardOh2; y2(:,2)=true_d2y; E2(:,2)=ERR2d2y;
z2(:,3)=third_BackwardOh2; y2(:,3)=true_d3y; E2(:,3)=ERR2d3y;
disp('Backward finite-difference approximation of second degree (O(h)^2)')
fprintf(' \n');
fprintf('\t\t\t\tFirst Derivative\t Second Derivative\t Third Derivative\t\n')
fprintf(' \n');
fprintf('Approximation\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',z2')
fprintf(' \n');
fprintf('True Value \t\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',y2')
fprintf(' \n');
fprintf('Percent Error \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',E2')

10
d) Sample Problems

(1) Compute backward difference approximations of O(h) and O(h2) for the first
derivative of y = cos x at x = π/4 using a value of h = π/12. Estimate the true percent
relative error εt for each approximation.

Solution: The required output is the backward difference approximations of O(h)


and O(h2) for the first derivative of y = cos x at x = π/4 using a value of h = π/12.

We make use of the given M-file to solve for the problem by setting the
variables as f=@(x) cos(x);, dy=@(x) -sin(x)};, d2y=@(x) -cos(x)};, dy=@(x)
sin(x)};, h=pi/12;, and xi=pi/4; and run the M-file to get the results.

11
M-file of the Problem
% original function to be evaluated at xi with a step size of h
f=@(x) cos(x);
dy=@(x) -sin(x); % actual first derivative
d2y=@(x) -cos(x);
d3y=@(x) sin(x);
h=pi/12; % step size
xi=pi/4; %value at which the differential is to be evaluated
%Backward finite-difference approximation of O(h)
true_dy= dy(xi); %true value of first derivative
true_d2y= d2y(xi);%true value of the second derivative
true_d3y= d3y(xi);%true value of the third derivative
first_BackwardOh= ( f(xi)-f(xi -h))./h;
second_BackwardOh=(f(xi -2*h)-2*f(xi -h) +f(xi))./(h^2);
third_BackwardOh=(f(xi)-f(xi -3*h)+ 3*f(xi -2*h)-3*f(xi -h))./(h^3);
%relative per cent error
ERRdy=100*(true_dy-first_BackwardOh)./true_dy;
ERRd2y=100*(true_d2y-second_BackwardOh)./true_d2y;
ERRd3y=100*(true_d3y-third_BackwardOh)./true_d3y;
% Construct a 1 x 3 matrix of zeros
z=zeros(1,3); y=zeros(1,3); E=zeros(1,3);
z(:,1)=first_BackwardOh; y(:,1)=true_dy; E(:,1)=ERRdy;
z(:,2)=second_BackwardOh; y(:,2)=true_d2y; E(:,2)=ERRd2y;
z(:,3)=third_BackwardOh; y(:,3)=true_d3y; E(:,3)=ERRd3y;
disp('Backward finite-difference approximation of first degree (O(h))')
fprintf(' \n');
fprintf('\t\t\t\t First Derivative\tSecond Derivative\t Third Derivadive
\t\n')
fprintf(' \n');
fprintf('Approximation\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',z')
fprintf(' \n');
fprintf('True Value\t \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',y')
fprintf(' \n');
fprintf('Percent Error \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',E')
fprintf(' \n');
%Backward finite-difference approximation of O(h^2)
first_BackwardOh2=(3*f(xi)-4*f(xi -h)+f(xi-2*h))/(2*h);
second_BackwardOh2=(-f(xi-3*h)+4*f(xi -2*h)-5*f(xi -h) +2*f(xi))/(h^2);
third_BackwardOh2=(5*f(xi)+3*f(xi-4*h)-14*f(xi -3*h)+ 24*f(xi -2*h)-18*f(xi
-h))/(2*h^3);
ERR2dy=100*(true_dy-first_BackwardOh2)/true_dy;
ERR2d2y=100*(true_d2y-second_BackwardOh2)/true_d2y;
ERR2d3y=100*(true_d3y-third_BackwardOh2)/true_d3y;
%relative per cent error
z2=zeros(1,3); y2=zeros(1,3); E2=zeros(1,3);
z2(:,1)=first_BackwardOh2; y2(:,1)=true_dy; E2(:,1)=ERR2dy;
z2(:,2)=second_BackwardOh2; y2(:,2)=true_d2y; E2(:,2)=ERR2d2y;
z2(:,3)=third_BackwardOh2; y2(:,3)=true_d3y; E2(:,3)=ERR2d3y;
disp('Backward finite-difference approximation of second degree (O(h)^2)')
fprintf(' \n');
fprintf('\t\t\t\tFirst Derivative\t Second Derivative\t Third
Derivative\t\n')
fprintf(' \n');
fprintf('Approximation\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',z2')
fprintf(' \n');
fprintf('True Value \t\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',y2')
fprintf(' \n');
fprintf('Percent Error \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',E2')

12
Output
I
t

c
a
n

b
e

Figure 5 Output after running the M-file for Backward finite Difference

Discussion of Result

It can be noticed that the percent error of the approximations in O(h) is 14.15378%
lower compared to the true value while the approximation of O(h2) yields only a percent
error of -1.78673% which means that the approximation is 1.78673% higher compared to
the true value. This is due to the additional term of the Taylor series retained in solving for
the formula for the approximation of the derivative.

13
(2) Use Backward Finite Difference to estimate the first derivative of y = cos (x) at x =
π/4 using step sizes of h1=π/3. Employ differences of O(h2) and compute for the percent
relative error.

Solution: The problem requires us to solve for the estimate of the fist derivative O(h2) of
The given code for solving the backward finite difference can be modified to give the
needed output. To solve for the problem by setting the needed input variables as follows,
f=@(x) cos(x), xi=pi/4, and h=pi/3.

Modified M-file to give the desired result of the problem

% original function to be evaluated at xiwith a step size of h


f=@(x) cos(x);
dy=@(x)-sin(x); % actual first derivative
d2y=@(x) -cos(x);
d3y=@(x)sin(x);
h=pi/3; % step size
xi=pi/4; %value at which the differential is to be evaluated
%Backward finite-difference approximation of O(h^2)
first_BackwardOh2=(3*f(xi)-4*f(xi -h)+f(xi-2*h))/(2*h);
second_BackwardOh2=(-f(xi-3*h)+4*f(xi -2*h)-5*f(xi -h) +2*f(xi))/(h^2);
third_BackwardOh2=(5*f(xi)+3*f(xi-4*h)-14*f(xi -3*h)+ 24*f(xi -2*h)-
18*f(xi -h))/(2*h^3);
ERR2dy=100*(true_dy-first_BackwardOh2)/true_dy;
ERR2d2y=100*(true_d2y-second_BackwardOh2)/true_d2y;
ERR2d3y=100*(true_d3y-third_BackwardOh2)/true_d3y;
%relative per cent error
z2=zeros(1,3); y2=zeros(1,3); E2=zeros(1,3);
z2(:,1)=first_BackwardOh2; y2(:,1)=true_dy; E2(:,1)=ERR2dy;
z2(:,2)=second_BackwardOh2; y2(:,2)=true_d2y; E2(:,2)=ERR2d2y;
z2(:,3)=third_BackwardOh2; y2(:,3)=true_d3y; E2(:,3)=ERR2d3y;
disp('Backward finite-difference approximation of second degree
(O(h)^2)')
fprintf(' \n');
fprintf('\t\t\t\tFirstDerivative\tSecond Derivative\t Third Derivative
\t \n')
fprintf(' \n');
fprintf('Approximation\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',z2')
fprintf(' \n');
fprintf('True Value \t\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',y2')
fprintf(' \n');
fprintf('Percent Error \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',E2')

14
Output

Figure 6 Output Of The Modified Program Displaying The Result For The O(h2)
Estimate

Discussion of result

After running the program, it was found that the value of the estimate of y = cos (x) at x =
π/4 using step sizes of h1=π/3 was -0.70835 and it was -0.17576% higher than the true value .

15
2. Centered Finite Difference

a) Algorithm
Step 1. Start

Step 2. Differentiate the given function

Step 3. Solve the values of f(xi), f(xi+1), f(xi+2), f(xi+3), f(xi-1), f(xi-2),

f(xi-3),and f(xi-4)

Step 4. Substitute the values to the equations of f’(x), f’’(x), f’’’(x) and
compute the estimate value

Step 5. Compute for the relative error of the estimates.

Step 6. Print f’(x), f’’(x), f’’’(x), and relative error

Step 7. End

b) Flow Chart
Start by setting the input variables namely the original function – f(x), to be
evaluated – x, and the step size – h. After that, differentiate the given function and
solve its value at x. Then solve for the values of f(xi), f(xi+1), f(xi+2), f(xi+3), and
f(xi+4).Then substitute the computed values to the equations of f’(x), f’’(x), f’’’(x)
to compute for the estimate value. To compute for the relative error of the estimates,
the difference between the true value and the estimated value is divided by the
estimated value and multiplied by 100. Finally, print or display the approximated
values of f’(x), f’’(x), f’’’(x) and percent error of each.

16
Start

f(x)=0
x=0
h=0

Read f(x), x and h

Differentiate f(x)

Compute for the values of f(xi), f(xi+1), f(xi+2),


f(xi+3), and f(xi+4)

First Derivative
f'(x)= (f(xi+1)-f(xi))/h
f'(x)= (-f(xi+2)+4f(xi+1)-3f(xi))/2h
Second Derivative
f''(x)= (f(xi+2)-2f(xi+1)+f(xi))/h2
f''(x)= (-f(xi+3)+4f(xi+2)-5f(xi+1)+2f(xi))/h2
Third Derivative
f'''(x)= (f(xi+3)-3f(xi+2)+3f(xi+1)-f(xi))/h3
f'''(x)= (-3f(xi+4)+14f(xi+3)-24f(xi+2)+18f(xi+1)-5f(xi))/2h3

Percent error=[(true value - approximate


value)/true value ]100%

Print f’(x), f’’(x), f’’’(x) and Percent error

END

Figure 7 Flow Chart for Centered finite-difference Approximation

17
c) M-file implement Centered Finite Difference
% original function to be evaluated at xi with a step size of h
f=@(x) {original function};
dy=@(x){ first derivative };% actual first derivative
d2y=@(x){ second derivative };% actual second derivative
d3y=@(x){ third derivative };% actual third derivative
h=0; % step size
xi=0; %value at which the differential is tobe evaluated
%Centered finite-difference approximation of O(h^2)
true_dy= dy(xi); %true value of first derivative
true_d2y= d2y(xi);%true value of the second derivative
true_d3y= d3y(xi);%true value of the third derivative
first_CenteredOh2= (-f(xi-h)+f(xi+h))/(2*h);
second_CenteredOh2=(f(xi+h)-2*f(xi)+f(xi-h))/(h^2);
third_CenteredOh2=(f(xi+2*h)-2*f(xi+h)+2*f(xi-h)-f(xi-2*h))/(2*h^3);
ERRdy=100*(true_dy-first_CenteredOh2)./true_dy;
ERRd2y=100*(true_d2y-second_CenteredOh2)./true_d2y;
ERRd3y=100*(true_d3y-third_CenteredOh2)./true_d3y;
%relative per cent error
% Construct a 1 x 3 matrix of zeros
z=zeros(1,3); y=zeros(1,3); E=zeros(1,3);
z(:,1)=first_CenteredOh2; y(:,1)=true_d2y; E(:,1)=ERRdy;
z(:,2)=second_CenteredOh2; y(:,2)=true_d2y; E(:,2)=ERRd2y;
z(:,3)=third_CenteredOh2; y(:,3)=true_d3y; E(:,3)=ERRd3y;
disp('Centered finite-difference approximation of first degree (O(h)^2)')
fprintf(' \n');
fprintf('\t\t\t\tFirst Derivative\tSecond Derivative \t Third Derivadive
\t\n')
fprintf(' \n');
fprintf('Approximation\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',z')
fprintf(' \n');
fprintf('True Value\t \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',y')
fprintf(' \n');
fprintf('Percent Error \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',E')
fprintf(' \n');
%Centered finite-difference approximation of O(h^4)
first_CenteredOh4=(f(xi-(2*h))-8*f(xi-h)+8*f(xi +h)-f(xi+(2*h)))/(12*h);
second_CenteredOh4=(-f(xi+2*h)+16*f(xi+h)-30*f(xi)+16*f(xi-h)-f(xi-
2*h))./(12*h^2);
third_CenteredOh4=(-f(xi+3*h)+8*f(xi+2*h)-13*f(xi+h)+13*f(xi-h)-8*f(xi-
2*h)+f(xi-3*h))/(8*h^3);
ERR2dy=100*(true_dy-first_CenteredOh4)/true_dy;
ERR2d2y=100*(true_d2y-second_CenteredOh4)/true_d2y;
ERR2d3y=100*(true_d3y-third_CenteredOh4)/true_d3y;
%relative per cent error
% Construct a 1 x 3 matrix of zeros
z2=zeros(1,3); y2=zeros(1,3); E2=zeros(1,3);
z2(:,1)=first_CenteredOh4; y2(:,1)=true_dy; E2(:,1)=ERR2dy;
z2(:,2)=second_CenteredOh4; y2(:,2)=true_d2y; E2(:,2)=ERR2d2y;
z2(:,3)=third_CenteredOh4; y2(:,3)=true_d3y; E2(:,3)=ERR2d3y;
disp('Centered finite-difference approximation of second degree (O(h)^4)')
fprintf(' \n');
fprintf('\t\t\t\t First Derivative\tSecond Derivative\t Third Derivative \t
\n')
fprintf(' \n');
fprintf('Approximation\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',z2')
fprintf(' \n');
fprintf('True Value \t\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',y2')
fprintf(' \n');
fprintf('Percent Error \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',E2')
fprintf(' \n');
18
d) Sample Problem

(1) Compute centered difference approximations of O(h2) and O(h4) for the first
derivative of y = cos x at x = π/4 using a value of h = π/12. Estimate the true percent
relative error εt for each approximation.

Solution: The required output is the backward difference approximations of O(h)


and O(h2) for the first derivative of y = cos x at x = π/4 using a value of h = π/12.

We make use of the given M-file to solve for the problem by setting the variables as
f=@(x) cos(x);, dy=@(x) -sin(x)};, d2y=@(x) -cos(x)};, dy=@(x) sin(x)};,
h=pi/12;, and xi=pi/4; and run the M-file to get the results.

19
M-file for the Problem
% original function to be evaluated at xi with a step size of h
f=@(x) cos(x);
dy=@(x) -sin(x);% actual first derivative
d2y=@(x) -cos(x);% actual second derivative
d3y=@(x) sin(x);% actual third derivative
h=pi/12; % step size
xi=pi/4; %value at which the differential is tobe evaluated
%Centered finite-difference approximation of O(h^2)
true_dy= dy(xi); %true value of first derivative
true_d2y= d2y(xi);%true value of the second derivative
true_d3y= d3y(xi);%true value of the third derivative
first_CenteredOh2= (-f(xi-h)+f(xi+h))/(2*h);
second_CenteredOh2=(f(xi+h)-2*f(xi)+f(xi-h))/(h^2);
third_CenteredOh2=(f(xi+2*h)-2*f(xi+h)+2*f(xi-h)-f(xi-2*h))/(2*h^3);
ERRdy=100*(true_dy-first_CenteredOh2)./true_dy;
ERRd2y=100*(true_d2y-second_CenteredOh2)./true_d2y;
ERRd3y=100*(true_d3y-third_CenteredOh2)./true_d3y;
%relative per cent error
% Construct a 1 x 3 matrix of zeros
z=zeros(1,3); y=zeros(1,3); E=zeros(1,3);
z(:,1)=first_CenteredOh2; y(:,1)=true_d2y; E(:,1)=ERRdy;
z(:,2)=second_CenteredOh2; y(:,2)=true_d2y; E(:,2)=ERRd2y;
z(:,3)=third_CenteredOh2; y(:,3)=true_d3y; E(:,3)=ERRd3y;
disp('Centered finite-difference approximation of first degree (O(h)^2)')
fprintf(' \n');
fprintf('\t\t\t\tFirst Derivative\tSecond Derivative \t Third Derivadive
\t\n')
fprintf(' \n');
fprintf('Approximation\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',z')
fprintf(' \n');
fprintf('True Value\t \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',y')
fprintf(' \n');
fprintf('Percent Error \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',E')
fprintf(' \n');
%Centered finite-difference approximation of O(h^4)
first_CenteredOh4=(f(xi-(2*h))-8*f(xi-h)+8*f(xi +h)-f(xi+(2*h)))/(12*h);
second_CenteredOh4=(-f(xi+2*h)+16*f(xi+h)-30*f(xi)+16*f(xi-h)-f(xi-
2*h))./(12*h^2);
third_CenteredOh4=(-f(xi+3*h)+8*f(xi+2*h)-13*f(xi+h)+13*f(xi-h)-8*f(xi-
2*h)+f(xi-3*h))/(8*h^3);
ERR2dy=100*(true_dy-first_CenteredOh4)/true_dy;
ERR2d2y=100*(true_d2y-second_CenteredOh4)/true_d2y;
ERR2d3y=100*(true_d3y-third_CenteredOh4)/true_d3y;
%relative per cent error
% Construct a 1 x 3 matrix of zeros
z2=zeros(1,3); y2=zeros(1,3); E2=zeros(1,3);
z2(:,1)=first_CenteredOh4; y2(:,1)=true_dy; E2(:,1)=ERR2dy;
z2(:,2)=second_CenteredOh4; y2(:,2)=true_d2y; E2(:,2)=ERR2d2y;
z2(:,3)=third_CenteredOh4; y2(:,3)=true_d3y; E2(:,3)=ERR2d3y;
disp('Centered finite-difference approximation of second degree (O(h)^4)')
fprintf(' \n');
fprintf('\t\t\t\t First Derivative\tSecond Derivative\t Third Derivative \t
\n')
fprintf(' \n');
fprintf('Approximation\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',z2')
fprintf(' \n');
fprintf('True Value \t\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',y2')
fprintf(' \n');
fprintf('Percent Error \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',E2')

20
Results:

Figure 8 Resulting Output of the Pogram for the centered finite difference

Discussion of Result

It can be observed that the centered finite difference yields a lower percent
error compared to the Backward and Forward Finite Differences. This is because the
centered finite difference is the average of Backward and Forward Finite
Differences.

(2) Use centered difference approximations to estimate the first and second derivatives of
y = ex at x = 2 for h = 0.1. Employ both O(h2) and O(h4) formulas for your estimates.

Solution: the required output is the central difference approximations of O(h2) and O(h4) for
the first derivative of y = ex at x = 2 for h = 0.1. Using the aid of Matlab, the group created a
program which could solve the problem. The codes are as follows;

21
M-file of the Problem

% original function to be evaluated at xi with a step size of h


f=@(x) exp(x);
dy=@(x) exp(x);% actual first derivative
d2y=@(x) exp(x);% actual second derivative
d3y=@(x) exp(x);% actual third derivative
h=.1; % step size
xi=2; %value at which the differential is tobe evaluated
%Centered finite-difference approximation of O(h^2)
true_dy= dy(xi); %true value of first derivative
true_d2y= d2y(xi);%true value of the second derivative
true_d3y= d3y(xi);%true value of the third derivative
first_CenteredOh2= (-f(xi-h)+f(xi+h))/(2*h);
second_CenteredOh2=(f(xi+h)-2*f(xi)+f(xi-h))/(h^2);
third_CenteredOh2=(f(xi+2*h)-2*f(xi+h)+2*f(xi-h)-f(xi-2*h))/(2*h^3);
ERRdy=100*(true_dy-first_CenteredOh2)./true_dy;
ERRd2y=100*(true_d2y-second_CenteredOh2)./true_d2y;
ERRd3y=100*(true_d3y-third_CenteredOh2)./true_d3y;
%relative per cent error
% Construct a 1 x 3 matrix of zeros
z=zeros(1,3); y=zeros(1,3); E=zeros(1,3);
z(:,1)=first_CenteredOh2; y(:,1)=true_d2y; E(:,1)=ERRdy;
z(:,2)=second_CenteredOh2; y(:,2)=true_d2y; E(:,2)=ERRd2y;
z(:,3)=third_CenteredOh2; y(:,3)=true_d3y; E(:,3)=ERRd3y;
disp('Centered finite-difference approximation of first degree (O(h)^2)')
fprintf(' \n');
fprintf('\t\t\t\t First Derivative \t Second Derivative \t Third
Derivadive \t\n')
fprintf('Approximation\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',z')
fprintf('True Value\t \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',y')
fprintf('Percent Error \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',E')
%Centered finite-difference approximation of O(h^4)
first_CenteredOh4=(f(xi-(2*h))-8*f(xi-h)+8*f(xi +h)-f(xi+(2*h)))/(12*h);
second_CenteredOh4=(-f(xi+2*h)+16*f(xi+h)-30*f(xi)+16*f(xi-h)-f(xi-
2*h))./(12*h^2);
third_CenteredOh4=(-f(xi+3*h)+8*f(xi+2*h)-13*f(xi+h)+13*f(xi-h)-8*f(xi-
2*h)+f(xi-3*h))/(8*h^3);
ERR2dy=100*(true_dy-first_CenteredOh4)/true_dy;
ERR2d2y=100*(true_d2y-second_CenteredOh4)/true_d2y;
ERR2d3y=100*(true_d3y-third_CenteredOh4)/true_d3y;
%relative per cent error
% Construct a 1 x 3 matrix of zeros
z2=zeros(1,3); y2=zeros(1,3); E2=zeros(1,3);
z2(:,1)=first_CenteredOh4; y2(:,1)=true_dy; E2(:,1)=ERR2dy;
z2(:,2)=second_CenteredOh4; y2(:,2)=true_d2y; E2(:,2)=ERR2d2y;
z2(:,3)=third_CenteredOh4; y2(:,3)=true_d3y; E2(:,3)=ERR2d3y;
disp('Centered finite-difference approximation of second degree
(O(h)^4)')
fprintf(' \n');
fprintf('\t\t\t\t First Derivative \t Second Derivative \t Third
Derivative \t \n')
fprintf('Approximation\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',z2')
fprintf('True Value \t\t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',y2')
fprintf('Percent Error \t\t%8.5f\t\t\t%8.5f\t\t\t %8.5f\n',E2')

22
Output

Figure 9 Output After Running The M-File


Discussion of the Result

Comparing the value of the approximation to the real value, it was found out that the
error is too small. This due to the high-order polinomial used which gain high accuracy
because of the terms used.

23
3. Forward Finite-difference

a) Algorithm
Step 1. Start

Step 2. Differentiate the given function

Step 3. Solve the values of f(xi), f(xi+1), f(xi+2), f(xi+3), and f(xi+4)

Step 4. Substitute the values to the equations of f’(x), f’’(x), f’’’(x) and
compute the estimate value

Step 5. Compute for the relative error of the estimates.

Step 6. Print f’(x), f’’(x), f’’’(x), and relative error

Step 7. End

b) Flow Chart
Start by setting the initial input variables namely the original function –f(x),
to be evaluated –x, and the step size –h. Then, differentiate the given function and
solve its value at x. After that, solve for the values of f(xi), f(xi+1), f(xi+2), f(xi+3),
f(xi-1), f(xi-2), f(xi-3), and f(xi-4 ).Then, substitute the computed values to the
equations of f’(x), f’’(x), f’’’(x) to compute for the estimate value. In computing the
relative error of the estimates, get the difference between the true value and the
estimated value and divide the value by the estimated value and multiplied by 100.
Finally, print the approximate values and the relative error of each approximates.

24
Start

f(x)=0
x=0
h=0

Read f(x), x and h

Differentiate f(x)

Compute for the values of f(xi), f(xi+1), f(xi+2), f(xi+3), f(xi-1),


f(xi-2), f(xi-3), and f(xi-4)

First Derivative
f'(x)= (-f(xi-1)+f(xi+1))/2h
f'(x)= (f(xi-2)-8f(xi-1)+8f(xi +1)-f(xi+2))/12h
Second Derivative
f''(x)= (f(xi-1)-2f(xi)+f(xi+1))/h2
f''(x)= (-f(xi+2) +16f(xi+1)-30f(xi)+16f(xi-1)-f(xi-2))/12h2
Third Derivative
f'''(x)= (f(xi+2)-2f(xi+1)+2f(xi-1)-f(xi-2))/2h3
f'''(x)= (-f(xi+3)+8f(xi+2)-13f(xi+1)+13f(xi-1)-8f(xi-2)+f(xi-3))/8h3

Percent error=[(true value-approximate


value)/true value ]100%

Print f’(x), f’’(x), f’’’(x), and Percent error

END

Figure 10 Flow Chart for Forward finite Difference Approximation

25
c) M-file to implement Forward Finite Difference

% original function to be evaluated at x with a step size of h


f=@(x) original function;
dy=@(x) first derivative; % actual first derivative
d2y=@(x) second derivative; % actual second derivative
d3y=@(x) third derivative; % actual third derivative
h=0; % step size
xi= 0; %value at which the differential is to be evaluated
%Forward finite-difference approximation of O(h)
%true value of first, second, and third derivative
true_dy= dy(xi);
true_d2y= d2y(xi);
true_d3y=d3y(xi);
first_forwardOh= (f(xi+1*h)-f(xi))/h;
second_forwardOh=(f(xi+2*h)-2*f(xi+1*h)+f(xi))/(h^2);
third_forwardOh=(f(xi+3*h)-3*f(xi+2*h)+3*f(xi+1*h)-f(xi))/(h^3);
%relative per cent error of first, second, and third derivative
ERRdy=100*(true_dy-first_forwardOh)./true_dy;
ERRd2y=100*(true_d2y-second_forwardOh)./true_d2y;
ERRd3y=100*(true_d3y-third_forwardOh)./true_d3y;
% Construct a 1 x 3 matrix of zeros
z=zeros(1,3); y=zeros(1,3); e=zeros(1,3);
z(:,1)= first_forwardOh; y(:,1)= true_dy; e(:,1)= ERRdy;
z(:,2)= second_forwardOh; y(:,2)= true_d2y; e(:,2)= ERRd2y;
z(:,3)= third_forwardOh; y(:,3)= true_d3y; e(:,3)= ERRd3y;
disp('Forward finite-difference approximation of degree (O(h))')
fprintf(' \n');
fprintf('\t\t\t First Derivative\t Second Derivative\t Third Derivative\n')
fprintf('Estimate\t\t\t%8.5f\t\t%8.5f\t\t\t %8.5f\n',z')
fprintf('True Value \t\t\t%8.5f\t\t%8.5f\t\t\t %8.5f\n',y')
fprintf('Percent Error \t\t%8.5f\t\t%8.5f\t\t\t %8.5f\n',e')
fprintf(' \n');
%Forward finite-difference approximation of O(h^2)
%relative per cent error of first, second, and third derivative
first_forwardOh2=(-f(xi+2*h)+4*f(xi+1*h)-3*f(xi))/(2*h) ;
second_forwardOh2=(-f(xi+3*h)+4*f(xi+2*h)-5*f(xi+1*h)+2*f(xi))/(h^2) ;
third_forwardOh2=(-3*f(xi+(4*h))+14*f(xi+(3*h))-
24*f(xi+(2*h))+18*f(xi+(1*h))-5*f(xi))/(2*(h^3));
%relative per cent error of first, second, and third derivative
ERR2dy=100*(true_dy-first_forwardOh2)./true_dy;
ERR2d2y=100*(true_d2y-second_forwardOh2)./true_d2y;
ERR2d3y=100*(true_d3y-third_forwardOh2)./true_d3y;
z2=zeros(1,3); y2=zeros(1,3); e2=zeros(1,3);
z2(:,1)= first_forwardOh2; y2(:,1)= true_dy; e2(:,1)= ERR2dy;
z2(:,2)= second_forwardOh2; y2(:,2)= true_d2y; e2(:,2)= ERR2d2y;
z2(:,3)= third_forwardOh2; y2(:,3)= true_d3y; e2(:,3)= ERR2d3y;
disp('Forward finite-difference approximation of degree (O(h^2))')
fprintf(' \n');
fprintf('\t\t\tFirst Derivative \t Second Derivative\t Third Derivative\n')
fprintf('Estimate\t\t\t%8.5f\t\t%8.5f\t\t\t %8.5f\n',z2')
fprintf('True Value \t\t\t%8.5f\t\t%8.5f\t\t\t %8.5f\n',y2')
fprintf('Percent Error \t\t%8.5f\t\t%8.5f\t\t\t %8.5f\n',e2')

26
d) Sample Problems

(1) Compute backward difference approximations of O(h) and O(h2) for the first
derivative of y = cos x at x = π/4 using a value of h = π/12. Estimate the true percent
relative error εt for each approximation.

Solution: The required output is the backward difference approximations of O(h)


and O(h2) for the first derivative of y = cos x at x = π/4 using a value of h = π/12.

The group make use of the given M-file to solve for the problem by setting the
variables as f=@(x) cos(x);, dy=@(x) -sin(x)};, d2y=@(x) -cos(x)};, dy=@(x)
sin(x)};, h=pi/12;, and xi=pi/4; and run the M-file to get the results.

27
M-file of the Problem

% original function to be evaluated at x with a step size of h


f=@(x) cos(x);
dy=@(x) -sin(x); % actual first derivative
d2y=@(x) -cos(x); % actual second derivative
d3y=@(x) sin(x); % actual third derivative
h=pi/12; % step size
xi= pi/4; %value at which the differential is to be evaluated
%Forward finite-difference approximation of O(h)
%true value of first, second, and third derivative
true_dy= dy(xi);
true_d2y= d2y(xi);
true_d3y=d3y(xi);
first_forwardOh= (f(xi+1*h)-f(xi))/h;
second_forwardOh=(f(xi+2*h)-2*f(xi+1*h)+f(xi))/(h^2);
third_forwardOh=(f(xi+3*h)-3*f(xi+2*h)+3*f(xi+1*h)-f(xi))/(h^3);
%relative per cent error of first, second, and third derivative
ERRdy=100*(true_dy-first_forwardOh)./true_dy;
ERRd2y=100*(true_d2y-second_forwardOh)./true_d2y;
ERRd3y=100*(true_d3y-third_forwardOh)./true_d3y;
% Construct a 1 x 3 matrix of zeros
z=zeros(1,3); y=zeros(1,3); e=zeros(1,3);
z(:,1)= first_forwardOh; y(:,1)= true_dy; e(:,1)= ERRdy;
z(:,2)= second_forwardOh; y(:,2)= true_d2y; e(:,2)= ERRd2y;
z(:,3)= third_forwardOh; y(:,3)= true_d3y; e(:,3)= ERRd3y;
disp('Forward finite-difference approximation of degree (O(h))')
fprintf(' \n');
fprintf('\t\t\t First Derivative\t Second Derivative\t Third Derivative\n')
fprintf('Estimate\t\t\t%8.5f\t\t%8.5f\t\t\t %8.5f\n',z')
fprintf('True Value \t\t\t%8.5f\t\t%8.5f\t\t\t %8.5f\n',y')
fprintf('Percent Error \t\t%8.5f\t\t%8.5f\t\t\t %8.5f\n',e')
fprintf(' \n');
%Forward finite-difference approximation of O(h^2)
%relative per cent error of first, second, and third derivative
first_forwardOh2=(-f(xi+2*h)+4*f(xi+1*h)-3*f(xi))/(2*h) ;
second_forwardOh2=(-f(xi+3*h)+4*f(xi+2*h)-5*f(xi+1*h)+2*f(xi))/(h^2) ;
third_forwardOh2=(-3*f(xi+(4*h))+14*f(xi+(3*h))-24*f(xi+(2*h))+18*f(xi+(1*h))-
5*f(xi))/(2*(h^3));
%relative per cent error of first, second, and third derivative
ERR2dy=100*(true_dy-first_forwardOh2)./true_dy;
ERR2d2y=100*(true_d2y-second_forwardOh2)./true_d2y;
ERR2d3y=100*(true_d3y-third_forwardOh2)./true_d3y;
z2=zeros(1,3); y2=zeros(1,3); e2=zeros(1,3);
z2(:,1)= first_forwardOh2; y2(:,1)= true_dy; e2(:,1)= ERR2dy;
z2(:,2)= second_forwardOh2; y2(:,2)= true_d2y; e2(:,2)= ERR2d2y;
z2(:,3)= third_forwardOh2; y2(:,3)= true_d3y; e2(:,3)= ERR2d3y;
disp('Forward finite-difference approximation of degree (O(h^2))')
fprintf(' \n');
fprintf('\t\t\tFirst Derivative \t Second Derivative\t Third Derivative\n')
fprintf('Estimate\t\t\t%8.5f\t\t%8.5f\t\t\t %8.5f\n',z2')
fprintf('True Value \t\t\t%8.5f\t\t%8.5f\t\t\t %8.5f\n',y2')
fprintf('Percent Error \t\t%8.5f\t\t%8.5f\t\t\t %8.5f\n',e2')

28
Output

Figure 11 Output of the M-file for solving Forward Finite Difference

Discussion of Result

It can be noticed that the percent error of the approximations in O(h) is -


11.87697% higher compared to the true value and the approximation of O(h2) yields
only a percent error of -2.67371% which also means that the approximation is -
2.67371% higher compared to the true value. Just like in the backward finite
difference, this is due to the additional term of the Taylor series retained in solving
for the formula for the approximation of the derivative.

C. Conclusion
The group found out, that to solve differential equations numerically, the replacing of
the derivatives in the equation with finite difference approximations on a discretized domain
is possible. This results in a number of algebraic equations that can be solved one at a time
(explicit methods) or simultaneously (implicit methods) to obtain values of the dependent
function f(xi) corresponding to values of the independent function xi in the discretized
domain. Comparing the exactness of the three finite-difference approximation using percent
error, the group noticed a great lead of exactness in the value computed using the equation of
Centered finite-difference than other two approximation (Backward and Forward), and its
applicability does not change even in higher derivatives.

29
III. Richardson Extrapolation

A. Theoretical Background
Richardson extrapolation provided a means to obtain an improved integral
1
estimate by the formula I  I (h2 )  [ I (h2 )  I (h1 )] where I (h1 ) and I (h2 )
(h1 / h2 )  1
are integral estimates using two step sizes: h1 and h2 . Because of its convenience
when expressed as a computer algorithm, this formula is usually written for the case
where h2  h1 / 2 as in.

4 1
I I (h2)  I (h3) Eq. 18
3 3

4 1
In a similar fashion, I  I (h 2)  I (h3) can be written for derivatives as
3 3

4 1
D D(h2 )  D(h1 ) . Eq. 19
3 3

For centered difference approximations with O(h2 ) , the application of this


formula will yield a new derivative estimate of O(h4 ) .

Richardson extrapolation is actually equivalent to fitting a higher-order


polynomial through the data and then evaluating the derivatives by centered divided
differences. For most other functions,our derivative estimate would be improved but
not exact. Consequently, as was the case for the application of Richardson
extrapolation, the approach can be applied iteratively using a Romberg algorithm until
the result falls below an acceptable error criterion.

30
B. Numerical Analysis using MATLAB®

1. Algorithm
Step 1. Start

Step 2. Differentiate the given function

Step 3. Solve the values of D (h1) and D (h2), the derivative estimates using
two step sizes h1 and h2 respectively.
4 1
Step 4. Substitute the values to the D  D(h2 )  D(h1 )
3 3

Step 5. Compute for the relative error of the approximations.

Step 6. Print D and relative error

Step 7. End

2. Flow Chart
Start program by Setting the initial input variables namely the original
function –f(x), to be evaluated –x, and the step size –h. Then, differentiate the given
function and solve its value at x. After that, solve for the values of D(h1) and D(h2)
using Central Finite Difference. Then, substitute the computed values to the equation
D = (4/3)*D(h2) - (1/3)*D(h1) to compute for the estimate value. To compute for
the relative error of the estimates, the difference between the true value and the
estimated value is divided by the estimated value and multiplied by 100. Finally,
display the approximate values and the relative error of each.

31
Start

f(x)=0
x=0
h=0

Read f(x), x and h

Differentiate f(x)

Compute for the values of D(h1) and D(h2)


using Central Finite Difference

Calculate the estimate


D = (4/3)*D(h2) - (1/3)*D(h1)

Percent error=[(true value - D)/true value ]100%

Print D and Percent error

END

Figure 12 Flow Chart for Richardson Extrapolation

32
3. M-file to Implement Richardson Interpolation

% original function to be evaluated at xi with a step size of h and h2


f=@(x) {original function};
dy=@(x){ first derivative }; % actual first derivative
h=0; % first step size
h2=0; % second step size
xi= 0; % value at which the differential is to be evaluated
%Central finite-difference approximation of O(h^2)
true_dy= dy(xi); %true value of first derivative
% using pi/3 as a step size
first_Centralh= (-f(xi-h)+f(xi+h))/(2*h);
% using pi/6 as a step size
first_Centralh2= (-f(xi-h2)+f(xi+h2))/(2*h2);
D=(4/3)*first_Centralh2 - (1/3)*first_Centralh;
ERRdy=100*(true_dy-D)/D;
z=zeros(1,3); % Construct a 1 x 3 matrix of zeros
z(:,1)=true_dy; % 1st column of matrix
z(:,2)=D; % 2nd column of matrix
z(:,3)=ERRdy; % 3rd column of matrix
disp('Richardson Extrapolation')
fprintf(' \n');
fprintf('True Value \t Estimated Value \t Percent Relative Error\n')
fprintf(' \n');
fprintf('%8.5f\t\t%8.5f\t\t\t %8.5f\n',z')
fprintf(' \n');

4. Sample Problems

a) Use Richardson extrapolation to estimate the first derivative of y = cos x at x


= π/4 using step sizes of h1=π/3 and h2 = π/6. Employ centered differences of O(h2)
for the initial estimates.

Solution: The required ouput in this problem is the estimated value of the first
derivative of y = cos x at x = π/4 using step sizes of h1=π/3 and h2 = π/6 using
Richardson extrapolation. Using the aid of Matlab, the group modified the created
M-files which was used in solving the problem using Richardson extrapolation.

33
M-file to Implement Richardson extrapolation

f=@(x) cos(x); % original function to be evaluated at x= pi/4 with a step


size of h=pi/12
dy=@(x) -sin(x); % actual first derivative
h=pi/3; % first step size
h2=pi/6; % second step size
xi= pi/4; % value at which the differential is tobe evaluated
%
%Central finite-difference approximation of O(h^2)
true_dy= dy(xi);
%true value of first derivative
first_Centralh= (-f(xi-h)+f(xi+h))/(2*h); % using pi/3 as a step size
first_Centralh2= (-f(xi-h2)+f(xi+h2))/(2*h2); % using pi/6 as a step size
D=(4/3)*first_Centralh2 - (1/3)*first_Centralh;
ERRdy=100*(true_dy-D)/D;
z=zeros(1,3); % Construct a 1 x 3 matrix of zeros
z(:,1)=true_dy; % 1st column of matrix
z(:,2)=D; % 2nd column of matrix
z(:,3)=ERRdy; % 3rd column of matrix
disp('Richardson Extrapolation')
fprintf(' \n');
fprintf('True Value \t Estimated Value \t Percent Relative Error\n')
fprintf(' \n');
fprintf('%8.5f\t\t%8.5f\t\t\t %8.5f\n',z')
fprintf(' \n');

Output

Figure 13 The Result after running the M-file


Discussion of Results

The extrapolation yielded an exact result because the function being analyzed
was a second-order polynomial. This is due to the additional term of the Taylor
series retained in solving for the formula for the approximation of the derivative in
Centered Finite-difference approximation

34
(1) Repeat Prob. 1, but for the first derivative of ln x at x = 5 using h1 = 2 and h2 =

Solution: The required output in this problem is the estimated value of the first derivative of
y = ln x at x = 5 using step sizes of h1=2 and h2 = 1 using Richardson extrapolation. Utilizing
the previous m-file which was used in solving the problem using Richardson extrapolation.

M-file to solve the Problem

f=@(x) log(x); % original function to be evaluated at x=5 with a step size of


h1=2,h2=1
dy=@(x) 1/(x); % actual first derivative
h1=2; % first step size
h2=1; % second step size
xi=5; % value at which the differential is to be evaluated

%Central finite-difference approximation of O(h^2)


%true value of first derivative
true_dy= dy(xi);
% using 2 as a step size
first_Centralh= (-f(xi-h1)+f(xi+h1))/(2*h1);
% using 1 as a step size
first_Centralh2= (-f(xi-h2)+f(xi+h2))/(2*h2);
D=(4/3)*first_Centralh2 - (1/3)*first_Centralh;
ERRdy=100*(true_dy-D)/true_dy;
z=zeros(1,3); % Construct a 1 x 3 matrix of zeros
z(:,1)=true_dy; % 1st column of matrix
z(:,2)=D; % 2nd column of matrix
z(:,3)=ERRdy; % 3rd column of matrix
disp('Richardson Extrapolation')
fprintf(' \n');
fprintf('True Value \t Estimated Value \t Percent Relative Error\n')
fprintf(' \n');
fprintf('%8.5f\t\t%8.5f\t\t\t %8.5f\n',z')
fprintf(' \n');

Output

Figure 14 The Result After Running The M-File

35
Discussion of Result

Same as the first problem, the extrapolation yielded an exact result because the
function being analyzed was a second-order polynomial. This is due to the additional
term of the Taylor series retained in solving for the formula for the approximation of
the derivative in Centered Finite-difference approximation

C. Conclusion
The group found out that the previous examples yielded an exact result because the
function being analyzed was a second-order polynomial. The exact outcome was due to the
fact that Richardson extrapolation is actually equivalent to fitting a higher-order polynomial
through the data and then evaluating the derivatives by Centered divided differences. Thus,
the present case matched the derivative of the second-order polynomial precisely. For most
other functions this would not occur and the derivative estimate would be improved but not
exact.

36
IV. Generalization
In the whole discussion of this paper we have observed that there are two ways
to improve the accuracy of the approximation of the derivatives, one is by retaining
more terms of the Taylor series when deriving the different finite difference formulas
and the other is by using the Richardson Extrapolation. Furthermore, it can be
comprehended from the previous problems that the Centered finite difference is the
most accurate among the three High accuracy differential formulas and the accuracy
can be further be enhanced by using the Richardson extrapolation.

V. References
About Matlab Tutorial and Manual. (n.d.). Retrieved from Tutorials point:
https://www.tutorialspoint.com/matlab/

Chapra, S. C. (2012). Applied Numerical Methods with MATLAB® for Engineers and Scientists Third
Edition. New York, NY: McGraw-Hill Companies, Inc.

Karris, S. T. (2007). Numerical Analysis Using MATLAB® and Excel® Third Edition. United States of
America: Orchard Publications. .

Numerical Methods. (n.d.). Retrieved from Math's Tools:


www.mathstools.com/section/main/Numerical_Methods

Programming and Data Types. (n.d.). Retrieved from Mathworks:


http://www.mathworks.com/help/matlab/examples.html#programming-and-data-types

Urroz, G. E. (2004). Numerical Solution to Ordinary Differential Equations.

37

Das könnte Ihnen auch gefallen