Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static double mathFunc(double x) {
- // function = e^x + x^3
- return Math.pow(Math.E, x)+Math.pow(x, 3);
- }
- // Simpsons 3/8th rule
- // a is begging of interval b is the end
- // n is the amount of times we are splitting
- //that function into smaller segments that are gonna be added together
- static double simpsons38th(double a, double b, double n) {
- double result = 0;
- double h = (b-a)/n;
- result += (mathFunc(a)+mathFunc(b));
- for(int i = 0; i < n ; i++) {
- double x;
- if(i%3 != 0) {
- x = (a + i * h);
- result += 3.0 * mathFunc(x);
- //System.out.println(i);
- }
- }
- for(int i = 0; i < n/3 ; i++) {
- double x = (a + i * h * 3);
- result += 2.0 * mathFunc(x);
- //System.out.println(i*3);
- }
- return (3.0 * h * result) / 8.0 ;
- }
- public static void main(String[] args) {
- System.out.println("f(x) = e^x + x^3");
- double a = 0, b = 1;
- int n = 10000;
- System.out.println("Simpson 3/8th = " + simpsons38th(a, b, n));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement