The Algorithms logo
The Algorithms
关于捐赠

The Rectangular Method

H
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The rectangle method (also called the midpoint rule) is the simplest method in Mathematics used to compute an approximation of a definite integral."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's check this method for the next function: $$f(x) = ({e^x / 2})*(cos(x)-sin(x))$$ with $\\varepsilon = 0.001$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math \n",
    "import numpy as np\n",
    "\n",
    "def integration(a,b,n):\n",
    "    h = (b-a)/n\n",
    "    r = f(a) + f(b)\n",
    "    i = 1\n",
    "    while i < n:\n",
    "        x = a + i*h\n",
    "        r = r + 4 * f(x)\n",
    "        i = i + 1\n",
    "        x = a + i * h\n",
    "        r = r +2*f(x)\n",
    "        i = i + 1\n",
    "    r = r * h / 3\n",
    "    print(\"Result: \", r) \n",
    "\n",
    "def rectangles(a,b,n):\n",
    "    \n",
    "    z = (b-a)/n\n",
    "    i = a\n",
    "    s1=0\n",
    "    s2=0\n",
    "    while i<b:\n",
    "        \n",
    "        s1=s1+f(i)*z\n",
    "        i=i+z\n",
    "    i=a \n",
    "    while i<b:\n",
    "        i=i+z\n",
    "        s2=s2+f(i)*z\n",
    "\n",
    "    print('Result of formula of the left rectangles: ',s1)\n",
    "    print('Result of formula of the left rectangles: ',s2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Some input data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Result:  -10.297317477276613\n",
      "Result of formula of the left rectangles:  -7.576924395545134\n",
      "Result of formula of the left rectangles:  -9.192576890365931\n"
     ]
    }
   ],
   "source": [
    "def f(x):\n",
    "    return  (math.e**x / 2)*(math.cos(x)-math.sin(x))\n",
    "\n",
    "n = 4  \n",
    "a = 2.\n",
    "b = 3.\n",
    "Si = []\n",
    "\n",
    "integration(a,b,n)\n",
    "rectangles(a,b,n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
关于这个算法

The rectangle method (also called the midpoint rule) is the simplest method in Mathematics used to compute an approximation of a definite integral.

Let's check this method for the next function: $$f(x) = ({e^x / 2})*(cos(x)-sin(x))$$ with $\varepsilon = 0.001$

import math 
import numpy as np

def integration(a,b,n):
    h = (b-a)/n
    r = f(a) + f(b)
    i = 1
    while i &lt; n:
        x = a + i*h
        r = r + 4 * f(x)
        i = i + 1
        x = a + i * h
        r = r +2*f(x)
        i = i + 1
    r = r * h / 3
    print("Result: ", r) 

def rectangles(a,b,n):
    
    z = (b-a)/n
    i = a
    s1=0
    s2=0
    while i&lt;b:
        
        s1=s1+f(i)*z
        i=i+z
    i=a 
    while i&lt;b:
        i=i+z
        s2=s2+f(i)*z

    print('Result of formula of the left rectangles: ',s1)
    print('Result of formula of the left rectangles: ',s2)

Some input data

def f(x):
    return  (math.e**x / 2)*(math.cos(x)-math.sin(x))

n = 4  
a = 2.
b = 3.
Si = []

integration(a,b,n)
rectangles(a,b,n)
Result:  -10.297317477276613
Result of formula of the left rectangles:  -7.576924395545134
Result of formula of the left rectangles:  -9.192576890365931