simionx.Integration - Numerical integration routines

SYNOPSIS

local INT = require "simionx.Integration"

-- Calculate (moment of inertia)/(mass) for sphere
-- of radius 5 via volume integration.
local mass = (4/3)*math.pi*(5)^3
print("Expecting", 10)  -- equals (2/5)*(5)^2
local co = INT.montecarlo_integrate {
  func = function(x,y,z, ux,uy,uz)
    return (x^2 + y^2) / mass
  end,
  shape = INT.sphere_filled(0,0,0, 5),
  min_iterations = 100000, rel_err = 0.001
}
repeat
  local result, result_stdev, count, is_end = co()
  print(result, result_stdev, count, is_end)
until is_end
--[[OUTPUT:
Expecting       10
1.4500000000001 0.56568542494929        2       false
10.201601740376 3.8872192893064 4       false
9.0886283261102 2.6596025108814 8       false
8.8950043916104 1.7139967190583 16      false
9.6281491592971 0.97056736497474        32      false
10.788100218533 0.67870277939624        64      false
10.066399568868 0.39300818193915        128     false
9.9114927256598 0.21441777256434        256     false
10.023885127646 0.12484348660943        512     false
10.008016691338 0.068214699144542       1024    false
10.022640402046 0.038532430498274       2048    false
9.9992880768054 0.021081642551506       4096    false
9.9958532750136 0.011474286260492       8192    false
10.001257767875 0.0061146278362448      16384   false
10.000726899752 0.0034332557814447      30709   false
9.998853501986  0.0032347632669252      32768   false
10.00004175219  0.0016990547669547      65536   false
9.9999029933063 0.00089307044213873     131072  true
--]]

--- Compute integral from 0..2PI of sin(x) dx.
print("Expecting", 2)
local co = INT.montecarlo_integrate {
  func = function(x) return math.sin(x) end,
  shape = INT.line(0,0,0, math.pi,0,0),
  rel_err = 0.001
}
repeat
  local result, result_stdev, count, is_end = co()
  print(result, result_stdev, count, is_end)
until is_end

Note

This page is abridged from the full SIMION "Supplemental Documentation" (Help file). The following additional sections can be found in the full version of this page accessible via the "Help > Supplemental Documentation" menu in SIMION 8.1.1 or above:
  • INTERFACE

SOURCE

version: 20071209