Question

Programming Assignment - Numerical Integration In MATH 1775 last semester, you used Riemann sums in Excel...

Programming Assignment - Numerical Integration In MATH 1775 last semester, you used Riemann sums in Excel to approximate the value of the definite integral that represents the area of the region between the x-axis and the graph of 2 5 y xx = − 8 shown below. The purpose of this assignment is to increase the accuracy of such approximations of integrals. Write a program (using java) that uses the Midpoint Rule, the Trapezoid Rule, and Simpson’s Rule to find separate approximations to a given definite integral ( ) b a f x dx ∫ . The program should accept the following as user inputs: • the limits of integration a and b; • the number of subintervals n, which may be as large as one million; • the coefficients of an arbitrary 5th-degree polynomial 543 2 f x Ax Bx Cx Dx Ex F ( ) = + + + ++ ; Program outputs should include: • the exact value of the integral, as found using the Fundamental Theorem of Calculus. • values of the approximations for each of the three approximation methods; • the percent error for each method.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Integrate

{

public static Scanner sc = new Scanner (System.in);

public static void main (String[] args) {

System.out.print("Enter the limits of integration a and b : ");

double a = sc.nextDouble();

double b= sc.nextDouble();

  


System.out.print("\nEnter the number of sub-intervasl : ");

int n = sc.nextInt();

  


System.out.print("\nEnter the coefficients of an arbitary 5th degree polynomial: ");

System.out.println("Ax^5 + Bx^4 + Cx^3 + Dx^2 + Ex^1 + F");

System.out.print("\n A = ");

double A = sc.nextDouble();

System.out.print("\n B = ");

double B = sc.nextDouble();

System.out.print("\n C = ");

double C = sc.nextDouble();

System.out.print("\n D = ");

double D = sc.nextDouble();

System.out.print("\n E = ");

double E = sc.nextDouble();

System.out.print("\n F = ");

double F = sc.nextDouble();

  

System.out.print("\nEnter the actual integral value : ");

double actual = sc.nextDouble();

  

System.out.println("Integral value using Mid-point formula : ");

double approx = MidPoint(a, b, n, A, B, C, D, E, F);

System.out.println("Approximate value = " + approx);

System.out.println("Error = " + Error(actual, approx));

  

System.out.println("Integral value using Trapezoidal formula = ");

approx = Trapezoid(a, b, n, A, B, C, D, E, F);

System.out.println("Approximate value = " + approx);

System.out.println("Error = " + Error(actual, approx));

  

  

System.out.println("Integral value using Simpson formula = ");

approx = Simpson(a, b, n, A, B, C, D, E, F);

System.out.println("Approximate value = " + approx);

System.out.println("Error = " + Error(actual, approx));

}

  


public static double f(double x, double A, double B, double C, double D, double E, double F) {

double x2 = x*x;

double x3 = x*x*x;

double y = A*x3*x2 + B*x2*x2 + C*x3 + D*x2 + E*x + F;

return y;

}

public static double MidPoint (double a, double b, int n, double A, double B, double C, double D,

double E, double F) {

double h = (b - a)/n;

double x, x0 = a, x1;

double sum = 0;

  

for (int i = 1; i <= n; i++){

x1 = x0 + h;

x =(x0 + x1)/2;

sum += f(x, A, B, C, D, E, F);

x0 = x1;

}

  

return sum * h;

}

public static double Trapezoid (double a, double b, int n, double A, double B, double C, double D,

double E, double F) {

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, A, B, C, D, E, F);

}

sum = 2 * sum + (f(a, A, B, C, D, E, F) + f(b, A, B, C, D, E, F));

return sum * h / 2;

}


  

public static double Simpson (double a, double b, int n, double A, double B, double C, double D,

double E, double F) {

double h = (b - a)/n;

double x = a;

double mid = x + h/2;

double sum = f(x, A, B, C, D, E, F);

int i = 0;

while (i < n - 1)

{

i++;

x = a + i * h;

sum = sum + 4 * f(mid, A, B, C, D, E, F) + 2 * f(x, A, B, C, D, E, F);

mid = x + h/2;

}

sum = sum + 4 * f(mid, A, B, C, D, E, F) + f(b, A, B, C, D, E, F);

return sum * h / 6;

}

public static double Error (double actual, double approx) {

return (Math.abs(actual - approx));

}

}

Add a comment
Know the answer?
Add Answer to:
Programming Assignment - Numerical Integration In MATH 1775 last semester, you used Riemann sums in Excel...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT