/**
* Most mathematical functions have an antiderivative, which can be "written down" in a closed form using primitive mathematical expressions.
* Unfortunately, many interesting functions do not have such an antiderivative, e.g. the function for computing the circumference of an ellipse.
* This application is intended to provide numerical integration for a given set of functions using two different algorithms:
* <ul>
* <li>Approximation through rectangles</li>
* <li>Monte-Carlo simulation</li>
* </ul>
*
* @see <a href="http://de.wikipedia.org/wiki/Numerische_Integration">Numerische Integration (on Wikipedia)</a>
*
*/
public class NumericalIntegration {
/**
* Roughly checks the command line arguments and delegates the execution to {@link #integrate(String[] args)}.
*
* If the preliminary check of the arguments fails, the user is given a help on how to start this application and the program terminates.
* Otherwise, the original arguments are passed unchanged to {@link #integrate(String[] args)} and the result is output.
*
* @param args The command line arguments provided by the user when starting the application.
*/
public static void main(String[] args) {
if (args.length == 5 && args[0].equalsIgnoreCase("SR") || args.length == 7 && args[0].equalsIgnoreCase("MC")) {
System.out.println("Result: " + integrate(args));
} else {
System.out.println("Usage: java NumericalIntegration TYPE FUNCTION Xmin Xmax STEPS [Ymin Ymax]");
System.out.println("where: TYPE is either \"SR\" or \"MC\" as String");
System.out.println(" FUNCTION is a positive integer denoting the desired function");
System.out.println(" STEPS is a positive long");
System.out.println(" Ymin and Ymax are required for MC only");
System.out.println(" Xmin, Xmax, Ymin, Ymax are doubles");
}
}
/**
* Checks and interprets the command line arguments and initiates the integration algorithm desired by the user with the corresponding parameters.
* <br/>
* <ul>
* <li>If the number of arguments is exactly 5 and the first argument is the string "SR",
* then the following arguments are interpreted as the parameters {@code function}, {@code xMin}, {@code xMax} and {@code steps}
* of the method {@link #integrateSumOfRectangles(int, double, double, long)}</li>
* <li>If the number of arguments is exactly 7 and the first argument is the string "MC",
* then the following arguments are interpreted as the parameters {@code function}, {@code xMin}, {@code xMax}, {@code steps}, {@code yMin} and {@code yMax}
* of the method {@link #integrateMonteCarlo(int, double, double, long, double, double)}</li>
* </ul>
*
* @param args The command line arguments provided by the user when starting the application.
* @return The approximation of the requested definite integral or 0 if the command line parameters are wrong.
*/
protected static double integrate(String[] args) {
}
/**
* This method "fills" the area between the given function and the x-axis with rectangles in order to "compute" the integral of the function.
*
* This algorithm approximates the "area" between the function plot and the x-axis by means of rectangles
* and computes the approximation by just summing up the areas of all the rectangles
* (counting the rectangles below the x-axis as having negative contribution to the integral).</li>
*
* @param function The id of the function, the integral of which should be approximated.
* @param xMin The lower bound of the interval for the definite integral.
* @param xMax The upper bound of the interval for the definite integral.
* @param steps The number of rectangles to be used. The higher this value is, the more precise will the resulting approximation be.
* @return The approximation of the integral.
*/
protected static double integrateSumOfRectangles(int function, double xMin, double xMax, long steps) {
// TODO
}
/**
* This method uses a statistical heuristic for "computing" the integral of a given function.
*
* This algorithm is based on statistical heuristics: Imagine the function to be integrated as being plotted in a frame.
* You just throw darts at random on this frame and count the number of times you hit the area between function plot and x-axis
* (again decrementing your count for the hits below the x-axis).
* The ratio between the number of hits counted and the total number of "darts thrown" is an approximation of the ratio between the
* integral and the total area of the frame.</li>
*
* @param function The id of the function, the integral of which should be approximated.
* @param xMin The lower bound of the interval for the definite integral.
* @param xMax The upper bound of the interval for the definite integral.
* @param steps The total number of "dart throws" to be applied. The higher this value is, the more precise will the resulting approximation be.
* @param yMin The lower bound of the "frame" for the function plot.
* @param yMax The upper bound of the "frame" for the function plot.
* @return The approximation of the integral.
*/
protected static double integrateMonteCarlo(int function, double xMin, double xMax, long steps, double yMin, double yMax) {
// TODO
}
/**
* Represents an overall function dispatcher, e.g. it subsequently calls/evaluates {@code func6(x)} if itself called with a value of 6 for its parameter {@code function}.
*
* This method takes the numerical identifier (as parameter {@code function}) of the function and the location on the x-axis (as parameter {@code x})
* where the desired function is to be evaluated and calls the method representing the corresponding function.
*
* @param function The numerical identifier of the function to be evaluated.
* @param x The position on the x-axis, for which the desired function is to be evaluated.
* @return The value of the requested function at the given location or 0 if the identifier is invalid.
*/
protected static double func(int function, double x) {
// TODO
}
private static double func1(double x) {
// TODO
}
private static double func2(double x) {
// TODO
}
// [url=http://www.matheboard.de/archive/1996/thread.html]Aufleitung von e^x^2[/url]
private static double func3(double x) {
// TODO
}
private static double func4(double x) {
// TODO
}
// [url=http://www.mathematik.ch/anwendungenmath/ellipsenumfang/]Berechnung Umfang Ellipse[/url]
private static double func5(double x) {
// TODO
}
// [url=http://de.wikipedia.org/wiki/Integralrechnung#Numerische_Verfahren]Integralrechnung ? Wikipedia[/url]
private static double func6(double x) {
// TODO
}