help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
scipy-scientific
/
notebooks
RSK World
scipy-scientific
Scientific Computing with SciPy
notebooks
  • 01_optimization.ipynb19.5 KB
  • 02_integration.ipynb7.4 KB
  • 03_interpolation.ipynb8.8 KB
  • 04_statistics.ipynb8.8 KB
  • 05_signal_processing.ipynb16.6 KB
02_integration.ipynb
notebooks/02_integration.ipynb
Raw Download
Find: Go to:
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SciPy Numerical Integration\n",
    "\n",
    "<!--\n",
    "Author: RSK World\n",
    "Website: https://rskworld.in\n",
    "Email: help@rskworld.in\n",
    "Phone: +91 93305 39277\n",
    "-->\n",
    "\n",
    "This notebook demonstrates numerical integration with SciPy.\n",
    "\n",
    "## Topics Covered:\n",
    "1. Basic Definite Integration\n",
    "2. Integration with Infinite Limits\n",
    "3. Double Integration\n",
    "4. Integration with Singularities\n",
    "5. Integration Methods Comparison\n",
    "6. Cumulative Integration\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Author: RSK World - https://rskworld.in\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import integrate\n",
    "\n",
    "print(\"SciPy Numerical Integration Examples\")\n",
    "print(\"Author: RSK World - https://rskworld.in\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Basic Definite Integration\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Function: f(x) = x²\n",
    "def func(x):\n",
    "    return x**2\n",
    "\n",
    "# Integrate from 0 to 2: ∫x²dx from 0 to 2 = x³/3|₀² = 8/3\n",
    "result, error = integrate.quad(func, 0, 2)\n",
    "exact = 8/3\n",
    "\n",
    "print(f\"Function: f(x) = x²\")\n",
    "print(f\"Integration from 0 to 2\")\n",
    "print(f\"Numerical result: {result:.8f}\")\n",
    "print(f\"Exact result: {exact:.8f}\")\n",
    "print(f\"Absolute error: {abs(result - exact):.2e}\")\n",
    "print(f\"Estimated error: {error:.2e}\")\n",
    "\n",
    "# Visualize\n",
    "x = np.linspace(0, 2, 100)\n",
    "y = func(x)\n",
    "\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.plot(x, y, 'b-', linewidth=2, label='f(x) = x²')\n",
    "plt.fill_between(x, 0, y, alpha=0.3, color='blue', label=f'Area = {result:.4f}')\n",
    "plt.xlabel('x', fontsize=12)\n",
    "plt.ylabel('f(x)', fontsize=12)\n",
    "plt.title('Definite Integration: ∫x²dx from 0 to 2', fontsize=14, fontweight='bold')\n",
    "plt.legend()\n",
    "plt.grid(True, alpha=0.3)\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Integration with Infinite Limits\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Gaussian function: f(x) = e^(-x²)\n",
    "def gaussian(x):\n",
    "    return np.exp(-x**2)\n",
    "\n",
    "# Integrate from -∞ to +∞: ∫e^(-x²)dx = √π\n",
    "result, error = integrate.quad(gaussian, -np.inf, np.inf)\n",
    "exact = np.sqrt(np.pi)\n",
    "\n",
    "print(f\"Function: f(x) = e^(-x²)\")\n",
    "print(f\"Integration from -∞ to +∞\")\n",
    "print(f\"Numerical result: {result:.8f}\")\n",
    "print(f\"Exact result: {exact:.8f}\")\n",
    "print(f\"Absolute error: {abs(result - exact):.2e}\")\n",
    "\n",
    "# Visualize\n",
    "x = np.linspace(-4, 4, 200)\n",
    "y = gaussian(x)\n",
    "\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.plot(x, y, 'b-', linewidth=2, label='f(x) = e^(-x²)')\n",
    "plt.fill_between(x, 0, y, alpha=0.3, color='blue', label=f'Area = {result:.4f}')\n",
    "plt.xlabel('x', fontsize=12)\n",
    "plt.ylabel('f(x)', fontsize=12)\n",
    "plt.title('Gaussian Integration: ∫e^(-x²)dx from -∞ to +∞', fontsize=14, fontweight='bold')\n",
    "plt.legend()\n",
    "plt.grid(True, alpha=0.3)\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Double Integration\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Function: f(x,y) = x² + y²\n",
    "def func(x, y):\n",
    "    return x**2 + y**2\n",
    "\n",
    "# Integrate over rectangle: x from 0 to 1, y from 0 to 1\n",
    "result, error = integrate.dblquad(func, 0, 1, lambda x: 0, lambda x: 1)\n",
    "exact = 2/3\n",
    "\n",
    "print(f\"Function: f(x,y) = x² + y²\")\n",
    "print(f\"Integration over [0,1] × [0,1]\")\n",
    "print(f\"Numerical result: {result:.8f}\")\n",
    "print(f\"Exact result: {exact:.8f}\")\n",
    "print(f\"Absolute error: {abs(result - exact):.2e}\")\n",
    "\n",
    "# Visualize\n",
    "x = np.linspace(0, 1, 50)\n",
    "y = np.linspace(0, 1, 50)\n",
    "X, Y = np.meshgrid(x, y)\n",
    "Z = func(X, Y)\n",
    "\n",
    "fig = plt.figure(figsize=(12, 5))\n",
    "\n",
    "# Contour plot\n",
    "ax1 = plt.subplot(1, 2, 1)\n",
    "contour = plt.contour(X, Y, Z, levels=20, cmap='viridis')\n",
    "plt.colorbar(contour, label='f(x,y)')\n",
    "plt.xlabel('x', fontsize=12)\n",
    "plt.ylabel('y', fontsize=12)\n",
    "plt.title('Function: f(x,y) = x² + y²', fontsize=12, fontweight='bold')\n",
    "plt.grid(True, alpha=0.3)\n",
    "\n",
    "# 3D surface plot\n",
    "ax2 = plt.subplot(1, 2, 2, projection='3d')\n",
    "surf = ax2.plot_surface(X, Y, Z, cmap='viridis', alpha=0.8)\n",
    "ax2.set_xlabel('x', fontsize=10)\n",
    "ax2.set_ylabel('y', fontsize=10)\n",
    "ax2.set_zlabel('f(x,y)', fontsize=10)\n",
    "ax2.set_title('3D Surface Plot', fontsize=12, fontweight='bold')\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Integration Methods Comparison\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Function: f(x) = sin(x)\n",
    "def func(x):\n",
    "    return np.sin(x)\n",
    "\n",
    "# Integration from 0 to π: ∫sin(x)dx = -cos(x)|₀^π = 2\n",
    "a, b = 0, np.pi\n",
    "exact = 2.0\n",
    "\n",
    "# Different methods\n",
    "methods = {\n",
    "    'quad': integrate.quad(func, a, b),\n",
    "    'fixed_quad': (integrate.fixed_quad(func, a, b, n=10)[0], 0),\n",
    "    'trapezoid': (integrate.trapezoid(func(np.linspace(a, b, 100)), \n",
    "                                      np.linspace(a, b, 100)), 0),\n",
    "    'simpson': (integrate.simpson(func(np.linspace(a, b, 100)), \n",
    "                                   np.linspace(a, b, 100)), 0),\n",
    "}\n",
    "\n",
    "print(f\"Function: f(x) = sin(x)\")\n",
    "print(f\"Integration from 0 to π (exact = 2.0)\")\n",
    "print(f\"\\n{'Method':<15} {'Result':<15} {'Error':<15}\")\n",
    "print(\"-\" * 45)\n",
    "\n",
    "for method, (result, error) in methods.items():\n",
    "    err = abs(result - exact)\n",
    "    print(f\"{method:<15} {result:<15.8f} {err:<15.2e}\")\n",
    "\n",
    "# Visualize\n",
    "x = np.linspace(0, np.pi, 100)\n",
    "y = func(x)\n",
    "\n",
    "plt.figure(figsize=(10, 6))\n",
    "plt.plot(x, y, 'b-', linewidth=2, label='f(x) = sin(x)')\n",
    "plt.fill_between(x, 0, y, alpha=0.3, color='blue', label=f'Area = {exact:.4f}')\n",
    "plt.xlabel('x', fontsize=12)\n",
    "plt.ylabel('f(x)', fontsize=12)\n",
    "plt.title('Integration Methods Comparison: ∫sin(x)dx from 0 to π', \n",
    "          fontsize=14, fontweight='bold')\n",
    "plt.legend()\n",
    "plt.grid(True, alpha=0.3)\n",
    "plt.tight_layout()\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
251 lines•7.4 KB
json
🚀 Support RSK World

Subscribe to our YouTube channel for latest tutorials & updates!



Click subscribe & support our work ❤️

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer