Created
September 1, 2012 00:30
-
-
Save aravindc26/3561822 to your computer and use it in GitHub Desktop.
integeration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.areaundercurve; | |
public class AreaUnderCurve { | |
public double getAreaUnderCurve(Term[] equation, int limit1, int limit2){ | |
double ret =0.0; | |
if(limit1 == limit2) return ret; //when limits are equal the area under the curve is equal | |
//Write your code here | |
int i; | |
Term[] res = new Term[equation.length]; //stores the equation after integeration | |
double lim1Res = 0.0; // stores the result after applying lower limit | |
double lim2Res = 0.0; // stores the result after applying upper limit | |
// finding the integeration | |
for(i = 0; i < equation.length; ++i){ | |
res[i] = new Term(equation[i].x_pow + 1, equation[i].coeff / (equation[i].x_pow + 1)); | |
} | |
//applying the limit | |
for(i = 0; i < equation.length; ++i){ | |
lim1Res += Math.pow(limit1, res[i].x_pow) * res[i].coeff; | |
lim2Res += Math.pow(limit2, res[i].x_pow) * res[i].coeff; | |
} | |
ret = (double)Math.round((lim2Res - lim1Res) * 10000) / 10000; | |
return ret; | |
} | |
// You could use this sample code to test your functions | |
// Following main fucntion contains 3 representative test cases | |
public static void main(String[] args) { | |
//TestCase 1 | |
try{ | |
AreaUnderCurve algo = new AreaUnderCurve(); | |
Term[] func = new Term[2]; | |
func[0] = new Term(1, 1); | |
func[1] = new Term(2, 3); | |
int limit1 = 4; | |
int limit2 = 8; | |
System.out.println(algo.getAreaUnderCurve(func, limit1, limit2)); | |
} | |
catch(Exception e){ | |
e.printStackTrace(); | |
} | |
//TestCase 2 | |
try{ | |
AreaUnderCurve algo = new AreaUnderCurve(); | |
Term[] func = new Term[1]; | |
func[0] = new Term(1, 1); | |
int limit1 = 1; | |
int limit2 = 1; | |
System.out.println(algo.getAreaUnderCurve(func, limit1, limit2)); | |
} | |
catch(Exception e){ | |
e.printStackTrace(); | |
} | |
//TestCase 3 | |
try{ | |
AreaUnderCurve algo = new AreaUnderCurve(); | |
Term[] func = new Term[1]; | |
func[0] = new Term(1, 1); | |
int limit1 = 2; | |
int limit2 = 1; | |
System.out.println(algo.getAreaUnderCurve(func, limit1, limit2)); | |
} | |
catch(Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.areaundercurve; | |
public class Term { | |
public double coeff; | |
public int x_pow; | |
public Term(int x_power, double coefficient) { | |
x_pow = x_power; | |
coeff = coefficient; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment