Problem Statement
Given two real numbers x1 and x2, calculate an approximation to the integral of e-x^2 evaluated between the limits from x1 to x2, which is accurate to the nearest 0.00001.
Return the answer in a
For example: x1 = -0.5 and x2 = 0.5 returns "0.92256"
Definition
- Class:
- NumericalIntegral
- Method:
- integrate
- Parameters:
- double, double
- Returns:
- String
- Method signature:
- String integrate(double x1, double x2)
- (be sure your method is public)
Notes
- e-x^2 can be calculated in C++ with exp(-x*x) in math.h. e-x^2 can be calculated in C# with Math.Exp(-x*x). The Math class is in the System namespace.e-x^2 can be calculated in Java with Math.exp(-x*x). e-x^2 can be calculated in Visual Basic with Exp(-x*x) in the System.Math namespace.
- The integral of a function is the area inside the closed figure formed by (on the top) the function between the limits of x=x1 and x=x2, (on the sides) vertical line segments at x=x1 and x=x2, and (on the bottom) the portion of the x axis between x=x1 and x=x2. This is shown by the shaded area above (the graph shows the function we are integrating, e-x^2).
- The integral of e-x^2 is known to have no closed form, so don't waste time looking in a table of integrals for an exact formula.
- Because of the 2e-6 constraint, about 40% of randomly chosen x1 and x2 values will be too close to a possible rounding error and will be rejected. This is not an error. It gives you more room for numerical errors.
Constraints
- x1 will be less than x2.
- x2-x1 will be between 0.00001 and 1.00000 inclusive.
- x1 will be between -10.0 and 10.0 inclusive.
- x2 will be between -10.0 and 10.0 inclusive.
- To avoid rounding errors the inputs x1 and x2 must be chosen so that the answer is not within 2e-6 of 0.000005 + a multiple of 0.00001
Examples
-0.5
0.5
Returns: "0.92256"
The example from above. This is the largest possible answer given the constraints of this problem.
0.0
0.1
Returns: "0.09967"
2.0
2.451
Returns: "0.00368"
-9.0001
-9.0
Returns: "0.00000"
Values are very small out here.
2.5
3.0
Returns: "0.00034"
0.0
0.98
Returns: "0.73932"
0.1
1.01
Returns: "0.65080"
0.000015
1.0
Returns: "0.74681"
0.500001
1.00000
Returns: "0.28554"
1.5
2.5
Returns: "0.02968"
0.32
1.3
Returns: "0.51834"
0.4
1.4
Returns: "0.46429"
-1.0001
-0.003
Returns: "0.74386"
-10
-9
Returns: "0.00000"
1.22
1.23
Returns: "0.00223"
This is where the derivative is highest.
2.71828183
3.14159265
Returns: "0.00010"
9
10
Returns: "0.00000"
-0.4111084
0.4111084
Returns: "0.77815"
0.6
0.9000111
Returns: "0.17109"
1.200005
1.8
Returns: "0.06981"
1.300074915
2.3
Returns: "0.05746"
0.0
1.0E-5
Returns: "0.00001"
0.0
1.0E-5
Returns: "0.00001"