Main > Software > ruby_integrator

« Software | Main | The Love Affair and the Hate Campaign »

September 25, 2005

ruby_integrator

What is ruby_integrator?

ruby_integrator is a set of Ruby classes that provide numeric integration estimates using a variety of methods. The software was authored as a means of learning and demonstrating OO-design using Ruby.

ruby_integrator implements the following features:

  • A few integration techniques:
    • Right Endpoint Integral Estimation
    • Left Endpoint Integral Estimation
    • Midpoint Integral Estimation
    • Trapezoid Integral Estimation
    • Simpson's Rule Integral Estimation
  • A mathematical function definition and evaluation class.
    • Allows one to define a string function such as "f(x) = pi*sin(x^2)^(x/2)", check its syntax and repeatedly evaluate it for differing values of x.
  • A built-in inheritable terminal interface.

Implemation & Usage

There are two ways to use the estimator classes. You may use them either by the class construtor interface or by the terminal interaction interface, as demonstrated by the included test.rb program.

Here is an example usage of the class interface for the estimation of the function sin(x) over the interval [0,π] using n = 100:

    require 'right_integral_estimator'
    r = RightIntegralEstimator.new(0, "pi", 100, Function.new("x","sin(x)"))
    print "Right: " + r.calculate.to_s + "\n"
    

ruby_integrator can also interact on the user's terminal:

    require 'right_integral_estimator'
    r = RightIntegralEstimator.new
    r.interact
    print r.inspect + "\n"  # completely optional
    print "Right: " + r.calculate.to_s + "\n"
    

The terminal session would look like:

    Please enter a function in terms of the variable x on the line below.
    For example: (pi*(sin(x^2)))^2

    You may use the constants e and pi in your function.

    Enter f(x) (type q to quit): sin(x^2)
    Enter start bound (type q to quit): 0
    Enter stop bound (type q to quit): 2*pi
    Enter number of sub-intervals (type q to quit): 100
    Shall I show evaluations (y/n) (type q to quit): n

    Using:

        a: 0.0
        b: 6.28318530717959
        n: 100
        function: f(x) = sin(x**2)

    Right: 0.672010534481375
    

Extending ruby_integrator

It's easy to define and use new integration estimation techniques. Here is the implementation for Simpson's Rule:

    class SimpsonIntegralEstimator < IntegralEstimator
        def calculate()
            d_x = delta_x()
            sum = (delta_x / 3.0) * (@f.evaluate(get_ith_x(0,d_x)) +
                ((1..(@n-1)).to_a.inject(0) { |s,i|
                    coeff = ((i & 0x1) == 0x1) ? 4.0 : 2.0
                    s += coeff * @f.evaluate(get_ith_x(i,d_x))
                    }) + @f.evaluate(get_ith_x(@n,d_x)))
        end
    end
    

All one needs to do is define the summing function. Functions for calculating Δx, xi, and evalating the given integrand function at f(xi) are given for you.

Copyright Information

ruby_integrator is freely distributable Copyright (C) 2005 Jordan Husney

ruby_integrator may be used by public or private entities for any purpose. ruby_integrator may be distributed in source or binary form.

If changes are made to ruby_integrator I will accept patches via e-mail but this is not required.

Obtaining the Software

Latest release:

Version Date Filename Change Log README
1.0 9/25/2005 ruby_integrator-1.0.tar.gz CHANGELOG README

Latest, and other releases:

Version Date Filename Change Log README
1.0 9/25/2005 ruby_integrator-1.0.tar.gz CHANGELOG README

Posted by jordanh at September 25, 2005 11:47 AM |

Trackback Pings

TrackBack URL for this entry:
http://jordan.husney.com/mt/mt-tb.cgi/164.

Comments

Post a comment




Remember Me?