Midpoint (Rectangular) Numerical Integration Matlab Code

by admin in , , on April 30, 2019

This code calculates the Numerical Integration of a given equation on a predefined interval using Midpopint Rule (Rectangular Integration). This codes uses variable step size. The method is fast and gives good accuracy.

Code Outputs:

  • Chart of Integration History
  • Chart of Residuals
  • Printed values of Final Iteration value, Accuracy and Number of Iterations.

Result is: 0.86841, Accuracy: 0.00085441, NO Iterations: 10

Input Requirements:

  • The equation you intended to integrate
  • Integration interval [a b].
  • predefined error (default 0.001).
  • Maximum number of iterations (default 1000).

About The Method (Rectangle Method):

In mathematics, specifically in integral calculus, the rectangle method (also called the midpoint or mid-ordinate rule) computes an approximation to a definite integral, made by finding the area of a collection of rectangles whose heights are determined by the values of the function.

Specifically, the interval [a, b] over which the function is to be integrated is divided into N equal subintervals of length h = (b-a)/N. The rectangles are then drawn so that either their left or right corners, or the middle

 of their top line lies on the graph of the function, with bases running along the x-axis. The approximation to the integral is then calculated by adding up the areas (base multiplied by height) of the N rectangles, giving the formula:

An animation showing how the rectangle rule approximation improves with more strips.

where h = (b-a)/N and xn = a + nh.

The formula for xn above gives xn for the Top-left corner approximation.

As N gets larger, this approximation gets more accurate. In fact, this computation is the spirit of the definition of the Riemann integral and the limit of this approximation as n–> inf is defined and equal to the integral of f on [a, b] if this Riemann integral is defined. Note that this is true regardless of which i’ is used, however the midpoint approximation tends to be more accurate for finite n.

The different rectangle approximations

Midpoint approximation Error

For a function which is twice differentiable, the approximation error in each section (a, a+delta) of the midpoint rule decays as the cube of the width of the rectangle. (For a derivation based on a Taylor approximation, see Midpoint method)

for some xi in (a, a+delta). Summing this, the approximation error for n intervals with width delta is less than or equal to

where n+1 is the number of nodes

in terms of the total interval, we know that n.delta = b – a so we can rewrite the expression:

which is equal to:

for some xi in [a, b].



[1] Leader, Jeffery J. (2004). Numerical Analysis and Scientific Computation. Addison Wesley. ISBN 978-0-201-73499-7.

[2] Stroud, A. H. (1971). Approximate Calculation of Multiple Integrals. Cliffs, NJ: Prentice-Hall Inc.

[3] Briol, François-Xavier; Oates, Chris J.; Girolami, Mark; Osborne, Michael A. (2015-06-08). ‘Frank-Wolfe Bayesian Quadrature: Probabilistic Integration with Theoretical Guarantees’. arXiv:1506.02681 [stat.ML].

[4] Philip J. Davis and Philip Rabinowitz, Methods of Numerical Integration.

[5] George E. Forsythe, Michael A. Malcolm, and Cleve B. Moler, Computer Methods for Mathematical Computations. Englewood Cliffs, NJ: Prentice-Hall, 1977. (See Chapter 5.)

[6] Press, W.H.; Teukolsky, S.A.; Vetterling, W.T.; Flannery, B.P. (2007), ‘Chapter 4. Integration of Functions’, Numerical Recipes: The Art of Scientific Computing (3rd ed.), New York: Cambridge University Press, ISBN 978-0-521-88068-8

[7] Josef Stoer and Roland Bulirsch, Introduction to Numerical Analysis. New York: Springer-Verlag, 1980. (See Chapter 3.)

0 Sale

Share Now!

Release Information

  • Price


  • Released

    April 30, 2019

  • Last Updated

    May 28, 2019

Share Your Valuable Opinions