Last active
November 15, 2020 20:47
-
-
Save TrevCan/30d16d2b651e208e97d942008da87ae6 to your computer and use it in GitHub Desktop.
Numeric Integration with Trapezoidal Method
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 numerical.integration; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
public class Main { | |
static double[][] function_data; | |
static double offset; | |
static final boolean IS_IN_DEBUG_MODE = false; | |
public static void main(String[] args) { | |
// write your code here | |
//integración numérica. | |
InputStreamReader input = new InputStreamReader(System.in); | |
/*formula trapecio = (b+B)/2 * h | |
b | |
h________ | |
/| \ | |
* /_|_________\ | |
B | |
*/ | |
//contains coefficients for each variable and exponent. | |
function_data = new double[][]{ | |
new double[]{1, 2} | |
}; | |
offset = 1; | |
double start_position = 0, end_position = 10; | |
//aka = h | |
double trapezoidHeight = 0.001; | |
double number_rectangles = (end_position - start_position) / trapezoidHeight; | |
double sum_of_area = 0; | |
int counter = 0; | |
for (double i = 0; i <= (end_position - trapezoidHeight); i += trapezoidHeight) { | |
double area, minor_base, major_base; | |
counter++; | |
minor_base = computeFunction(i + start_position); | |
major_base = computeFunction(i + start_position + trapezoidHeight); | |
area = (minor_base + major_base) / 2 * trapezoidHeight; | |
sum_of_area += area; | |
} | |
System.out.println("Area is: " + sum_of_area); | |
System.out.println("Count is: " + counter); | |
System.out.println("number_rects is: " + number_rectangles); | |
/* | |
int[][] foo = new int[][] { | |
new int[] { 1, 2, 3 }, | |
new int[] { 1, 2, 3, 4}, | |
}; | |
System.out.println(foo.length); //2 | |
System.out.println(foo[0].length); //3 | |
System.out.println(foo[1].length); //4 | |
*/ | |
} | |
public static Double computeFunction(double x, double[][] data, double offset) { | |
if (data[0].length < 2) { | |
System.out.println("invalid data from computeFunction"); | |
return null; | |
} | |
double sum = 0; | |
for (int i = 0; i < data.length; i++) { | |
double power = Math.pow(x, data[i][1]); | |
sum += power * data[i][0]; | |
} | |
return sum + offset; | |
} | |
public static Double computeFunction(double x) { | |
if (IS_IN_DEBUG_MODE) { | |
String function = "f(x) ="; | |
System.out.println("Row Values"); | |
for (int i = 0; i < function_data.length; i++) { | |
function += " " + function_data[i][0] + "x^" + function_data[i][1] + " +"; | |
} | |
function += " " + offset; | |
System.out.println("Function f(x): \n" + function); | |
} | |
if (function_data[0].length < 2) { | |
System.out.println("invalid data from computeFunction"); | |
return null; | |
} | |
double sum = 0; | |
for (int i = 0; i < function_data.length; i++) { | |
double power = Math.pow(x, function_data[i][1]); | |
sum += power * function_data[i][0]; | |
} | |
return sum + offset; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment