10. Project: The Trapezoidal and Simpson’s Rules#

10.1. Introduction#

In this project, you will write a MATLAB code to calculate the integral of \(f(x)=e^{-x^2}\) on \([-2,2]\) by trapezoidal and Simpson’s rule. 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

  • project10.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.

  • * trapezoidal.m - The trapezoidal rule.

  • * simpson.m - The Simpson’s rule.

You need to complete the files with *.

Throughout the project, you will be using the script project10.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.

10.2. Assignments#

Part 1 - Trapezoidal Rule#

The first part of project10.m gives you practice with evaluating the integral by trapezoidal rule in MATLAB. Recall the formula of trapezoidal rule are

\[ T_n(f)=\frac{h}{2}\Big(f(x_0)+2f(x_1)+2f(x_2)+\cdots+2f(x_{n-1})+f(x_n)\Big) \]

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

In the file trapezoidal.m, you will find the outline of a MATLAB function. Modify it to return the integral of \(f(x)\) on the interval \([a,b]\).

Hint

Use a for loop to compute the sum \(f(x_1)+f(x_2)+\cdots+f(x_{n-1})\), say s, then the integral is h/2*(f(a)+2*s+f(b))

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

=============================================
==    n |                Tn |      Error
==   -- |           ------- |    -------
==    2 | 2.036631277777468 |  -2.72e-01
==    4 | 1.754074521231619 |   1.01e-02
==    8 | 1.761237268249079 |   2.93e-03
==   16 | 1.763407582664267 |   7.55e-04
==   32 | 1.763972490531554 |   1.90e-04
==   64 | 1.764115115602423 |   4.77e-05
==  128 | 1.764150859221589 |   1.19e-05
==  256 | 1.764159800585126 |   2.98e-06
==  512 | 1.764162036267170 |   7.45e-07
== 1024 | 1.764162595209002 |   1.86e-07
=============================================

This table shows \(T_n(f)\) when \(n=2,4,8,...,1024\). We can figure out that the error is decreasing as n is increasing. The error is calculated by

\[ E^T_n(f)=I(f)-T_n(f) \]

where \(I(f)\approx 1.764162781524843\).

Part 2 - Simpson’s Rule#

In this part of project, you will complete the code in simpson.m to evaluate the integral by Simpson’s rule in MATLAB. Recall the formula of Simpson’s rule is

\[ S_n(f)=\frac{h}{3}\Big(f(x_0)+4f(x_1)+2f(x_2)+4f(x_3)+2f(x_4)+\cdots+2f(x_{n-2})+4f(x_{n-1})+f(x_n)\Big) \]

In the file simpson.m, you will find the outline of a MATLAB function. Modify it to return the integral of \(f(x)\) on the interval \([a,b]\) by Simpson’s rule.

Hint

  • Use a for loop to compute the sum of '4'-terms \(f(x_1)+f(x_3)+\cdots+f(x_{n-1})\), say s1.

  • Use another for loop to compute the sum of '2'-terms \(f(x_2)+f(x_4)+\cdots+f(x_{n-2})\), say s2.

  • Then the integral is h/3*(f(a)+4*s1+2*s2+f(b))

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

=============================================
==    n |                Sn |      Error
==   -- |           ------- |    -------
==    2 | 2.691087518518312 |  -9.27e-01
==    4 | 1.659888935716336 |   1.04e-01
==    8 | 1.763624850588232 |   5.38e-04
==   16 | 1.764131020802664 |   3.18e-05
==   32 | 1.764160793153984 |   1.99e-06
==   64 | 1.764162657292713 |   1.24e-07
==  128 | 1.764162773761309 |   7.76e-09
==  256 | 1.764162781039639 |   4.85e-10
==  512 | 1.764162781494518 |   3.03e-11
== 1024 | 1.764162781522949 |   1.89e-12
=============================================

This table shows \(S_n(f)\) when \(n=2,4,8,...,1024\). We can figure out that the error is decreasing as \(n\) is increasing. The error is calculated by

\[ E^S_n(f)=I(f)-S_n(f) \]

Comparing trapezoidal and Simpson’s rules, we find the Simpson’s rule has a higher accuracy in most cases.

10.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.