Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Monte Carlo Integration GUI

A comprehensive MATLAB graphical user interface for numerical integration using Monte Carlo methods. This tool provides an intuitive way to compute single and double integrals with real-time visualization and error analysis.

Single Integral Integration

Compute 1D integrals with real-time function visualization and statistical analysis

Single Integral Interface

Double Integral Integration

Compute 2D integrals with stunning 3D surface plots, contour maps, and comprehensive analysis

Double Integral Interface


Table of Contents


What is Monte Carlo Integration?

Monte Carlo integration is a numerical method that uses random sampling to estimate integrals.

Instead of calculating the exact area under a curve, the method works by:

  1. Generating random points in the integration region.
  2. Evaluating the function at those points.
  3. Averaging the results to approximate the mean value of the function.
  4. Scaling by the region size (interval length, rectangle area, etc.) to estimate the integral.
  • The more samples you use, the closer the estimate gets to the true value.
  • This method is especially powerful for high-dimensional problems where traditional techniques are inefficient.

Monte Carlo Integration Method

Monte Carlo integration estimates definite integrals using random sampling from a uniform distribution. The method is based on the law of large numbers and is particularly useful for high-dimensional integration problems.

Single Integral Formula

For a function f(x) over interval [a,b]:

∫ₐᵇ f(x)dx ≈ (b-a) · 1/N · Σₙ₌₁ᴺ f(xₙ)

Where:

  • xₙ = random samples uniformly distributed in [a,b]
  • N = number of random samples
  • (b-a) = length of integration interval
  • 1/N · Σf(xₙ) = average value of function over samples

Double Integral Formula

For a function f(x,y) over rectangular domain [a,b] × [c,d]:

∫ₐᵇ ∫ₖᵈ f(x,y)dydx ≈ (b-a)(d-c) · 1/N · Σₙ₌₁ᴺ f(xₙ,yₙ)

Where:

  • (xₙ, yₙ) = random sample points uniformly distributed in the domain
  • (b-a)(d-c) = area of the integration domain
  • N = number of random samples

Error Analysis

Standard Error Estimation

The standard error of the Monte Carlo estimate is calculated as:

σ = (domain_size) · σf / √N

Where:

  • σf = standard deviation of f values: √(1/N · Σ(f(xₙ) - μ)²)
  • μ = mean of function values: 1/N · Σf(xₙ)
  • N = number of samples

Confidence Interval

A 95% confidence interval for the integral is given by:

[Estimate - 1.96·σ, Estimate + 1.96·σ]

This means we can be 95% confident that the true value lies within this range.

Relative Error

The relative error (in percentage) is computed as:

Relative Error (%) = (σ / |Estimate|) · 100

Convergence Rate

Monte Carlo integration converges at a rate of O(1/√N):

Error ∝ 1/√N

Practical Implications:

  • To halve the error: multiply samples by
  • To reduce error by 10×: multiply samples by 100×
  • Independent of dimension: Error rate doesn't worsen with more dimensions (unlike deterministic methods)

Convergence Example

For the function f(x) = x² on [0,1] with exact answer 1/3:

Samples Estimate Error Rel. Error
1,000 0.3342 0.0009 0.27%
10,000 0.3332 0.0003 0.09%
100,000 0.3334 0.0001 0.03%
1,000,000 0.3333 0.00003 0.01%

Features

Core Functionality

  • Single Integral (1D): Compute ∫f(x)dx over [a,b]
  • Double Integral (2D): Compute ∫∫f(x,y)dxdy over rectangular domains
  • Real-time Visualization: Interactive plots showing function behavior and sampling
  • Statistical Analysis: Automatic error estimation and confidence intervals

Advanced Analytics

  • Monte Carlo Sampling: Efficient random sampling with user-defined sample sizes
  • Error Analysis: Standard error calculation and 95% confidence intervals
  • Convergence Visualization: Multiple plot types for comprehensive analysis

Installation

Method 1: Direct Download

  1. Download the MonteCarloIntegrationGUI.m file
  2. Place it in your MATLAB working directory
  3. Run the function:
    MonteCarloIntegrationGUI()

Method 2: Clone Repository

git clone https://github.com/yourusername/monte-carlo-integration-gui.git
cd monte-carlo-integration-gui

Then in MATLAB:

MonteCarloIntegrationGUI()

Usage

Getting Started

  1. Launch the GUI: MonteCarloIntegrationGUI()
  2. Choose a tab: Single Integral or Double Integral
  3. Enter your function using MATLAB syntax
  4. Set integration limits and sample size
  5. Click Calculate to see results and visualizations

Function Syntax

  • Single Integral: @(x) x.^2 + sin(x)
  • Double Integral: @(x,y) x.^2 + y.^2 + x.*y

Sample Sizes

  • Quick Test: 10,000 samples
  • Standard: 100,000 samples (recommended)
  • High Precision: 1,000,000+ samples

Examples

Single Integral Examples

Polynomial Function

Function: @(x) x.^3 + 2*x.^2 - x + 1
Domain: [0, 2]
Expected Result: ~8.67

Gaussian Function

Function: @(x) exp(-(x-2).^2) .* sqrt(x)
Domain: [0, 5]
Expected Result: Beautiful bell curve visualization

Double Integral Examples

Simple Polynomial

Function: @(x,y) x.^2 + y.^2
Domain: [0,1] × [0,1]
Expected Result: 0.6667 (exact: 2/3)

3D Gaussian Hill Most Spectacular

Function: @(x,y) exp(-(x.^2 + y.^2))
Domain: [-2,2] × [-2,2]
Expected Result: ~3.14159 (≈ π)

Requirements

System Requirements

  • MATLAB: R2019b or later (App Designer required)
  • Toolboxes: Statistics and Machine Learning Toolbox (recommended)
  • RAM: 4GB+ recommended for large sample sizes
  • Display: 1200×800 minimum resolution

MATLAB Functions Used

  • uifigure, uitabgroup, uipanel (App Designer)
  • rand, mean, std (Core functions)
  • surf, contourf, scatter (Visualization)
  • histogram, plot, colorbar (Graphics)

References

  1. Numerical Recipes: Press, W. H., et al. Numerical Recipes in C
  2. Monte Carlo Methods: Rubinstein, R. Y. Simulation and the Monte Carlo Method
  3. MATLAB Documentation: MathWorks App Designer Guide

About

A comprehensive MATLAB graphical user interface for numerical integration using Monte Carlo methods. This tool provides an intuitive way to compute single and double integrals with real-time visualization and error analysis.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages