Skip to main content
Engineering LibreTexts

8.1: Numerical Methods

  • Page ID
    14957
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    In Calculus, you learn how to use Riemann sums, the Trapezoidal Rule and Simpson’s rule to approximate definite integrals, which represent area under a curve for positive functions \(f(x)\).

    The area between the function and the x-axis = \[\int_{a}^{b}f(x)\,dx\nonumber\]

    Each involves finding the sum of areas of approximating shapes. Given a function f(x) defined on an interval [a, b], and a value n, the process is as follows.

    1. First subdivide the interval [a, b] into n equal subintervals.
    2. Assign x0 = a, x1 as the right endpoint of the first subinterval, x2 as the right endpoint of the second subinterval, · · · , xn as the right endpoint of the nth interval (that is xn = b).For example, if a = 0, b = 1, n = 4, then x0 = 0, x1 = .25, x2 = .5, x3 = .75 and x4 = 1. In this example, note that even though n = 4, there are five x values.
    3. Next, calculate the values f(x0), f(x1), f(x2), · · · , f(xn) by substituting the xi values into f(x).

    The formulas for each of the methods are:

    Riemann Sum: \[\int_{a}^{b}f(x)\,dx\approx \left ((b-a)/n\right )\left [ f(x_{0})+f(x_{1})+f(x_{2})+...+f(x_{n-2})+f(x_{n-1}) \right ]\nonumber\]

    Trapezoidal Rule: \[\int_{a}^{b}f(x)\,dx\approx \left ((b-a)/2n\right )\left [ f(x_{0})+2f(x_{1})+2f(x_{2})+...+2f(x_{n-2})+2f(x_{n-1})+f(x_{n}) \right ]\nonumber\]

    Simpson’s Rule: \[\int_{a}^{b}f(x)\,dx\approx \left ((b-a)/3n\right )\left [ f(x_{0})+4f(x_{1})+2f(x_{2})+...+2f(x_{n-2})+4f(x_{n-1})+f(x_{n}) \right ]\nonumber\]

    Note

    The pattern of coefficients in the Trapezoidal rule is 1, 2, 2, 2, · · · , 2, 2, 1 (each of the middle terms is a 2). In Simpson’s rule, the pattern is 1, 4, 2, 4, 2, 4, · · · , 2, 4, 2, 4, 1 where the middle terms start with a 4, alternate between 2 and 4, and end on a 4 (important!!). This last fact forces n to be an even number for Simpson’s rule to work.

    Homework

    Write a Matlab function with the following details.

    • Name the function appropriately (mine could be siemerstj num methods)
    • Comment the function appropriately including a description of the usage.
    • The function should have three inputs a, b, n
    • The function should have three outputs Rsum, Tsum, Ssum
    • Once the function is run, a menu should appear with three choices of functions to choose from:

    \[x^2, \sin x, \sqrt{\dfrac1{2π }}e^{{-x^2}/2}\nonumber\]

    • Once the user clicks on a function from the menu, the function should compute the three approximations and output the values in a nice format (Hint: use fprintf).
    • If n is not even, then only the Rsum and Tsum should be displayed with a message saying the Ssum could not be calculated.

    Here is a sample run. Suppose I typed this :

    >> [Rsum,Tsum,Ssum]=siemerstj num methods(0,1,6)

    and then clicked on the x2 button in the menu. Then the output should be:

    Rsum=.25463, Tsum=.3380, Ssum=0.33333


    This page titled 8.1: Numerical Methods is shared under a CC BY-NC 3.0 license and was authored, remixed, and/or curated by Troy Siemers (APEX Calculus) via source content that was edited to the style and standards of the LibreTexts platform.