Question: 3) The following code in bold is a Java program for Numerical Integration which allows for Midpoint, Trapezoid, and Simpson rules in Linear Algebra. A
3) The following code in bold is a Java program for Numerical Integration which allows for Midpoint, Trapezoid, and Simpson rules in Linear Algebra. A client with no technical experience wants to know what is going on in this code so that they can then explain it line-for-line for their superiors. Explain this code past the comments (be as specific as possible).
package NumInteg;
import java.util.Scanner; import java.text.DecimalFormat;
public class Integration { public static DecimalFormat fmt = new DecimalFormat("0.################"); public static DecimalFormat fmt2 = new DecimalFormat("0.########"); public static Scanner scan = new Scanner (System.in);
// Give the user a menu of options until he // she wants to stop.
public static void main (String[] args) { String choice = "X"; int whichFunction = 1;
System.out.println (); System.out.println ("Numerical Integration"); System.out.println ("----------------------"); System.out.println ();
while (!choice.equals("Q")) { choice = menu(); if (choice.equals("C")) whichFunction = chooseFunction(); else if (choice.equals("I")) integrate(whichFunction); else if (!choice.equals("Q")) System.out.println ("Not a valid choice - try again!"); } }
// Print a menu of options; read and return the user's // choice as a String. public static String menu () { System.out.println(" Main Menu"); System.out.println ("Choose a function - C"); System.out.println ("Integrate a function using composite rules - I"); System.out.println ("Quit - Q"); System.out.print (" Enter your choice (C, I, or Q): "); String choice = scan.next(); return choice.toUpperCase(); }
// Print a menu of function choices; read and return the // user's choice as an integer. public static int chooseFunction() { int whichOne = 0; int n = 7; // number of functions to choose from
while (whichOne <= 0 || whichOne > n) { System.out.println (" Function Choices:"); System.out.println ("1. f(x) = 4 / (1 + x^2)"); System.out.println ("2. f(x) = Math.sqrt(x) * Math.log(x)"); System.out.println ("3. f(x) = cos(x)"); System.out.println ("4. f(x) = 1 / (1 + 100x^2)"); System.out.println ("5. f(x) = Math.sqrt(Math.abs(x))"); System.out.println ("6. f(x) = 100/x^2 * Math.sin(10/x)"); System.out.println ("7. f(x) = x^16 * cos(x^16)"); System.out.print (" Enter the number for the function: "); whichOne = scan.nextInt(); if (whichOne <= 0 || whichOne > n) System.out.println ("Invalid choice - try again."); } return whichOne; }
// Perform numerical integration using the three rules (Midpoint, Trapezoid, and Simpson) on the function whose // number is sent as a parameter. The endpoints of the interval // and the number of subdivisions of the interval are read in; the // results are printed out.
public static void integrate(int fcnNum) { System.out.print (" Enter the interval endpoints a and b: "); double a = scan.nextDouble(); double b = scan.nextDouble(); System.out.print ("Enter the number of subintervals n: "); int n = scan.nextInt(); double simp = Simpson (a, b, n, fcnNum); double trap = Trapezoid (a, b, n, fcnNum); double mid = MidPoint (a, b, n, fcnNum); System.out.println (" Integral Using Composite Rules... "); System.out.println ("Interval: [" + a + ", " + b + "]"); System.out.println ("Number of subdivisions: " + n); System.out.println ("Step size h: " + (b-a)/n); System.out.println (); System.out.println("Rule\t\tApproximation\t\t\tError"); System.out.println ("Midpoint\t" + fmt.format(mid) + "\t\t" + fmt.format(Error(mid,fcnNum))); System.out.println ("Trapezoid\t" + fmt.format(trap) + "\t\t" + fmt.format(Error(trap,fcnNum))); System.out.println ("Simpson\t\t" + fmt.format(simp) + "\t\t" + fmt.format(Error(simp,fcnNum))); }
// Compute the numerical approximation of the integral of the given // function (determined by the parameter fcnNum) on the interval // [a,b] using the composite Midpoint rule
public static double MidPoint(double a, double b, int n, int fcnNum) { double h = (b - a) / n; // step size double x = 0; // area double initVal = a; double sum ; for (int i = 1; i <= n; i++) { double tpp = ((initVal + h)+initVal/2); x += f(tpp,fcnNum); initVal += h; } return x*h; }
// Compute the numerical approximation of the integral of the given // function (determined by the parameter fcnNum) on the interval // [a,b] using the composite Trapezoid rule
public static double Trapezoid (double a, double b, int n, int fcnNum) { double h = (b - a)/n; double x = a; double sum = 0;
for (int i = 1; i < n; i++) { x = a + i * h; sum += f(x, fcnNum); }
sum = 2 * sum + (f(a, fcnNum) + f(b, fcnNum));
return sum * h / 2; }
// Compute the numerical approximation of the integral of the given // function (determined by the parameter fcnNum) on the interval // [a,b] using the composite Simpson rule
public static double Simpson (double a, double b, int n, int fcnNum) { double h = (b - a)/n; double x = a; double mid = x + h/2; double sum = f(x, fcnNum); int i = 0;
while (i < n - 1) { i++; x = a + i * h; sum = sum + 4 * f(mid, fcnNum) + 2 * f(x, fcnNum); mid = x + h/2; }
sum = sum + 4 * f(mid, fcnNum) + f(b, fcnNum); return sum * h / 6; }
// Compute and return f(x) for a given x and a function f determined // by the parameter fcnNum.
public static double f(double x, int fcnNum) { double value; // f(x)
switch (fcnNum) { case 1: value = 4 / (1 + x*x); break; case 2: value = Math.sqrt(x) * Math.log(x); break; case 3: value = Math.cos(x); break; case 4: value = 1 / (1 + 100*x*x); break; case 5: value = Math.sqrt(Math.abs(x)); break; case 6: value = 100/(x*x) * Math.sin(10/x); break; case 7: double x16 = Math.pow(x, 16); value = x16 * Math.cos(x16); break; default: value = 0; } return value; }
// Compute and return the absolute value of the error in approximating // the integral of the given function public static double Error (double approx, int fcnNum) { double actual; // correct value of the integral
switch (fcnNum) { case 1: actual = Math.PI; break; case 2: actual = -4./9; break; case 3: actual = 2 * Math.sin(1); break; case 4: actual = 0.2 * Math.atan(10); break; case 5: actual = 4./3; break; case 6: actual = -1.426024763463; break; case 7: actual = 0.0491217; break; default: actual = 0; } return (Math.abs(actual - approx)); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
