Ok, so I'm working on the following assignment:

Here's what I created:

You'll notice there's a method called testAssignment3_2 in there, this method was provided by the assignment and it's meant for checking if the integral function is accurate.Code:import java.lang.Math; public class Assignment3_2 { private static double f(double x) { x = Math.pow(Math.E, (Math.log(x)*0.5)); return x; } private static double rie_sum(double start, double end, double res){ double sum=0; double current = start; while(current <= end){ sum += f(current); current += res; } return sum; } public static double integral(double start, double end, double res){ double integral = res * rie_sum(start, end, res); return integral; } public static void testAssignment3_2(){ System.out.println("f(3.0) == 1.7320508076 is " + (Math.abs(f(3.0) - 1.7320508076) <= 1E-9)); System.out.println("f(9.0) == 3.0 is " + (Math.abs(f(9.0) - 3.0) <= 1E-9)); System.out.println("integral(0,50,1) == 239.0358082329 is " + (Math.abs(integral(0.0,50,1) - 239.0358082329) <= 1E-9) + " Error: " + (integral(0.0,50,1) - 239.0358082329)); System.out.println("integral(0,50,0.1) == 235.3421927538 is " + (Math.abs(integral(0.0,50,0.1) - 235.3421927538) <= 1E-9) + " Error: " + (integral(0.0,50,0.1) - 235.3421927538)); System.out.println("integral(0,50,0.01) == 235.7374085139 is " + (Math.abs(integral(0.0,50,0.01) - 235.7374085139) <= 1E-9) + " Error: " + (integral(0.0,50,0.01) - 235.7374085139)); System.out.println("integral(0,50,0.001) == 235.7057893690 is " + (Math.abs(integral(0.0,50,0.001) - 235.7057893690) <= 1E-9) + " Error: " + (integral(0.0,50,0.001) - 235.7057893690)); } public static void main(String[] args) { System.out.println(integral(0.0, 50, 1)); System.out.println(integral(0.0, 50, 0.1)); System.out.println(integral(0.0, 50, 0.01)); System.out.println(integral(0.0, 50, 0.001)); System.out.println(); testAssignment3_2(); } }

As you've probably guessed, my integral function is not accurate. The weird thing is: it's pretty accurate, but not enough. Running my code gives:

The error margin gets progressively smaller with an increasing resolution and I have absolutely zero clue why.Code:239.03580060352076 235.342191990834 235.73740843760655 235.7057893613778 f(3.0) == 1.7320508076 is true f(9.0) == 3.0 is true integral(0,50,1) == 239.0358082329 is false Error: -7.6293792403703264E-6 integral(0,50,0.1) == 235.3421927538 is false Error: -7.629659819485823E-7 integral(0,50,0.01) == 235.7374085139 is false Error: -7.62934462272824E-8 integral(0,50,0.001) == 235.7057893690 is false Error: -7.622190878464608E-9

I know this kind of a complicated problem to expect someone to help me with free of charge, but if one of you java guru's could perhaps take a look at this, I'd be EXTREMELY grateful.

Thanks in advance!

Tweet This+ 1 thisPost To Linkedin