Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Integral {
- private static Scanner scnr = new Scanner(System.in);
- static int a, b, c, d, e, f;
- static double upperLimit, lowerLimit, numSubInterval;
- public static void getVal(Scanner scnr) {
- a = scnr.nextInt();
- b = scnr.nextInt();
- c = scnr.nextInt();
- d = scnr.nextInt();
- e = scnr.nextInt();
- f = scnr.nextInt();
- }
- public static void main(String args[]) {
- System.out.println("Enter a value for your lower limits: ");
- lowerLimit =scnr.nextDouble();
- System.out.println("Enter a value for your upper limits: ");
- upperLimit =scnr.nextDouble();
- System.out.println("Enter number of subintervals n");
- numSubInterval = scnr.nextDouble();
- System.out.println("Enter coefficients of an arbitrary 5th degree polynomial (i.e. A,B,C,D,E): ");
- getVal(scnr);
- System.out.println(leftPoint());
- System.out.println(rightPoint());
- System.out.println(midPoint());
- System.out.println(trapoezoid());
- System.out.println(simpsons());
- System.out.println(fExact(upperLimit)-fExact(lowerLimit));
- errorRate();
- }
- /**
- * My two methods that calculate f(x) and the exact value of f(x)
- * @param x
- * @return
- */
- public static double fVal(double x) {
- return (Math.pow(x, 5) * a) + (Math.pow(x, 4) * b) + (Math.pow(x, 3) * c) + (Math.pow(x, 2) * d) + (e * x) + f;
- }
- public static double fExact(double x) {
- return (Math.pow(x, 6) * a) / 6 + (Math.pow(x, 5) * b)/5 + (Math.pow(x, 4) * c)/4 + (Math.pow(x, 3) * d)/3 + (e *Math.pow(x, 2) )/2 + f*x ;
- }
- /**
- * All my methods below that calculate the numerical value using Left,Right, Mid point
- * Trapezoid rule and Simpsons.
- * @return f(deltaX)*deltaX
- */
- public static double leftPoint() {
- double deltaX = (upperLimit - lowerLimit) / numSubInterval;
- double sum = 0;
- double counter = lowerLimit;
- for (int i = 0; i < numSubInterval - 1; i++) {
- sum += fVal(counter);
- counter += deltaX;
- }
- return sum * deltaX;
- }
- public static double rightPoint() {
- double deltaX = (upperLimit - lowerLimit) / numSubInterval;
- double sum = 0;
- double counter = lowerLimit + deltaX;
- for (int i = 0; i < numSubInterval - 1; ++i) {
- sum += fVal(counter);
- counter += deltaX;
- }
- return sum * deltaX;
- }
- public static double trapoezoid() {
- return ( rightPoint() + leftPoint() ) / 2;
- }
- public static double midPoint(){
- double deltaX = (upperLimit - lowerLimit) / numSubInterval;
- double sum = 0;
- double counter = lowerLimit + (deltaX/2);
- for (int i = 0; i < numSubInterval - 1; ++i) {
- sum += fVal(counter);
- counter += deltaX;
- }
- return sum * deltaX;
- }
- public static double simpsons(){
- return ( (2*midPoint() ) +trapoezoid() ) /3;
- }
- /**
- * My method for calculating the error rate and printing each percentage respectively
- */
- public static void errorRate(){
- //I declare the exact numerical value of the integral as a variable and use it to make the calculation of the error rate.
- double correctVal= fExact(upperLimit)-fExact(lowerLimit);
- String percent = "%";
- // I use printf to format the percentage up to 5 decimals of precision.
- System.out.printf("The error rate for Left Point Rule is: %.5f%s%n", (leftPoint()/correctVal)*100 ,percent);
- System.out.printf("The error rate for Right Point Rule is: %.5f%s%n", (rightPoint()/correctVal)*100 ,percent);
- System.out.printf("The error rate for Mid Point Rule is: %.5f%s%n", (midPoint()/correctVal)*100,percent );
- System.out.printf("The error rate for Trapezoid Rule is: %.5f%s%n", (trapoezoid()/correctVal)*100,percent );
- System.out.printf("The error rate for Simpsons Rule is: %.5f%s", (simpsons()/correctVal)*100 ,percent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement