9. Project: Numerical Differentiation#

9.1. Introduction#

In this project, you will write MATLAB codes to practice numerical differentiation formulas. We will use this formulas to approximate \(f'(x_0)\), applied to \(f(x)=e^x\) at \(x=0\). Note the true value of \(f'(x_0)\) is \(1\) and we will use this to calculate the absolute errors to investigate the error behavior of these formulas. To get started with this project, you will need to download the starter code and unzip its contents to the directory where you wish to complete the project.

Files included in this project

  • project9.m - MATLAB script that steps you through the project.

  • checkMyAnswer.m - MATLAB script that checks your code.

  • checkMyAnswer.mat - Data set.

  • data.mat - Data set.

  • f.m - The function M-file.

  • * two_point_backward.m - Two-point backward formula.

  • * two_point_forward.m - Two-point forward formula.

  • * three_point_backward.m - Three-point backward formula.

  • * three_point_forward.m - Three-point forward formula.

  • * three_point_mid.m - Three-point midpoint formula.

  • * five_point_backward.m - Five-point backward formula.

  • * five_point_forward.m - Five-point forward formula.

  • * five_point_mid.m - Five-point midpoint formula.

You need to complete the files with *.

Throughout the project, you will be using the script project9.m. This script set up all dataset for the problem and make calls to functions that you will write. Do not modify it. You are only required to modify functions in * files, following the instructions in this project.

9.2. Assignments#

Part 1 - Two-Point Formulas#

The first part of project9.m gives you practice with two-point formulas of numerical differentiation in MATLAB. Recall the forward and backward difference formulas are:

  • Two-Point Backward Difference

\[ f'(x_0)=\frac{f(x_0)-f(x_0-h)}{h}+O(h) \]
  • Two-Point Forward Difference

\[ f'(x_0)=\frac{f(x_0+h)-f(x_0)}{h}+O(h) \]

The term \(O(h)\) means the truncation error is on the first order of \(h\).

The function \(f(x)=e^x\) is given in f.m. You do not need to modify it.

In the file two_point_backward.m and two_point_forward.m, you will find the outlines of MATLAB functions. Modify them to return of derivative of \(f(x)\).

Now you can run project9.m, and you should see the following output:

The approximation by two-point backward formula is   0.9516.
The absolute error is -4.84e-02.

and

The approximation by two-point forward formula is   1.0517.
The absolute error is 5.17e-02.

Part 2 - Three-Point Formulas#

The second part of project9.m gives you practice with three-point formulas of numerical differentiation in MATLAB. Recall the forward, backward and midpoint difference formulas are:

  • Three-Point Backward Difference

\[ f'(x_0)=\frac{f(x_0-2h)-4f(x_0-h)+3f(x_0)}{2h}+O(h^2) \]
  • Three-Point Forward Difference

\[ f'(x_0)=\frac{-3f(x_0)+4f(x_0+h)-f(x_0+2h)}{2h}+O(h^2) \]
  • Three-Point Midpoint Differece

\[ f'(x_0)=\frac{f(x_0+h)-f(x_0-h)}{2h}+O(h^2) \]

In the file three_point_backward.m, three_point_forward.m and three_point_mid.m, you will find the outlines of MATLAB functions. Modify them to return of derivative of \(f(x)\) by three point formulas.

Now you can run project9.m, and you should see the following output:

The approximation by three-point backward formula is   0.9969.
The absolute error is -3.09e-03.

The approximation by three-point forward formula is   0.9964.
The absolute error is -3.60e-03.

and

The approximation by three-point midpoint formula is   1.0017.
The absolute error is 1.67e-03.

We can see that three-point formulas are more accurate than two-point formulas.

Part 3 - Five-Point Formulas#

The third part of project9.m gives you practice with five-point formulas of numerical differentiation in MATLAB. Recall the forward, backward and midpoint difference formulas are:

  • Five-Point Backward Difference

    \[ f'(x_0)=\frac{3f(x_0-4h)-16f(x_0-3h)+36f(x_0-2h)-48f(x_0-h)+25f(x_0)}{12h}+O(h^4) \]
  • Five-Point Forward Difference

    • You need derive this formula by yourself. This is the third problem in your homework.

  • Five-Point Midpoint Differece

    \[ f'(x_0)=\frac{f(x_0-2h)-8f(x_0-h)+8f(x_0+h)-f(x_0+2h)}{12h}+O(h^4) \]

In the file five_point_backward.m, five_point_forward.m and five_point_mid.m, you will find the outlines of MATLAB functions. Modify them to return of derivative of \(f(x)\) by five point formulas.

Now you can run project9.m, and you should see the following output:

The approximation by five-point backward formula is   1.0000.
The absolute error is -1.70e-05.

The approximation by five-point forward formula is   1.0000.
The absolute error is -2.37e-05.

and

The approximation by five-point midpoint formula is   1.0000.
The absolute error is -3.34e-06.

We can see that five-point formulas are more accurate than three-point formulas.

9.3. Submit your code#

This is the last step. You should submit your code files to WTClass. After clicking Browse My Computer, you should select all files listed in Introduction to upload. Failure to do so will affect your grade.