Implement these two methods in programs (java), and use each of these to approxi
ID: 3784607 • Letter: I
Question
Implement these two methods in programs (java), and use each of these to approximately integrate the function f(x) = sin(x) over the interval [0, 1], using successively the following values of M: M = 2, 4, 8, 16, · · · . For each of these values of M print the error, i.e., the absolute value of the difference between the approximation and the known exact value of the integral. Make sure to represent to sufficient precision in your program!
Furthermore, for each of the two methods, determine the smallest value of M for which the error is less than 107 . How many function evaluations are required in each of these two cases?
Can you describe the observed behavior of the error? In particular, for each of the two methods, can you say approximately how the error depends on h ? More specifically, it is known that the errors will be approximately proportional to h p , where p is an integer that depends on the method. Can you tell from the numerical results what p is for each of the two methods? Also can you explain what happens to the error when M gets “very large”.
Secure h ps://us ers encs concordia ca c361 4/a Apps R Facebook D operating system Cor r COMP 335 Introd D Introduction To Auto G internship computers Programming problen P Perfect Resume Builde D sec71.70pdf A Dr. Aiman Hanna De al dvi Problem 3. (50%) Consider the approximate integration of a function f(z) over the interval 0,1 et M be a positive integer, and let h 1/M, and kh, for M. Thus zo 0 and 1. Then 0,1,2,3 f( f aCM a M. Another approximate integration formula is +2. f(z M-2 This method, which requires M to be even, is known as Simpson's Rule. Ana nectPal A lE Ana ConnectPa Ana ConnectP A 4 Ana Connect Pal Con n Ana ConnectPa mp4 A Show all X A 40 ENG 32 AM US 2017-01-2 Ask me anythingExplanation / Answer
import java.lang.Math.*;
import static java.lang.Math.pow;
import static java.lang.Math.sin;
import java.util.Scanner;
public class ApproxIntegration {
public static void main(String[] args) {
ApproxIntegration ai=new ApproxIntegration();
Scanner sc = new Scanner(System.in);
//System.out.println("Enter The Value of M");
//int M=sc.nextInt();
int M=0;
double req_err=.0000001;
double error_m1=1;
double error_simpson=1;
int i=1;
do
{
M=(int)Math.pow(2.0,i);
error_m1=ai.CalculateApproxIntegrationM1(M);
//System.out.println("Approx Error Method 1: "+error_m1);
i++;
}while(error_m1>req_err);
System.out.println("Final Error Method 1: "+error_m1+", Final M Value: "+M);
M=0;
i=1;
do
{
M=(int)Math.pow(2.0,i);
error_simpson=ai.CalculateApproxIntegrationSimpson(M);
//System.out.println("Approx Error Simpson: "+error_simpson);
i++;
}while(error_simpson>req_err);
System.out.println("Final Error Simpson: "+error_simpson+", Final M Value: "+M);
}
public double CalculateApproxIntegrationSimpson(int M )
{
double integral_value=0;
double h;
double x0=0;
double xM=1;
double pi=3.14159265359;
h=(xM-x0)/M;
double exact_value=(-1/pi)*(Math.cos(pi*1)-Math.cos(pi*0));
for(int i=0;i<M;i++)
{
if(i==0)
{
double value=(i*h)+x0;
integral_value+=sin(pi*value);
}
if(i%2==1)
{
double value=(i*h)+x0;
integral_value+=4*sin(pi*value);
}
if(i%2==0 && i!=0)
{
double value=(i*h)+x0;
integral_value+=2*sin(pi*value);
}
}
integral_value+=sin(pi*xM);
integral_value=integral_value*h/3;
//System.out.println("Calculated Value Simpson:"+integral_value+" Exact Value :"+exact_value+" for M="+M);
return Math.abs(integral_value-exact_value);
}
public double CalculateApproxIntegrationM1(double M )
{
if(M%2==1)
{
System.out.println("Unable to calculate Simpson because M is odd ");
return 0.0;
}
double integral_value=0;
double h;
double x0=0;
double xM=1;
double pi=3.14159265359;
h=(xM-x0)/M;
double exact_value=(-1/pi)*(Math.cos(pi*1)-Math.cos(pi*0));
for(int i=0;i<M;i++)
{
double value=(i*h)+x0;
integral_value+=sin(pi*value);
}
integral_value=integral_value*h;
//System.out.println("Calculated Value :"+integral_value+" Exact Value :"+exact_value+" for M="+M);
return Math.abs(integral_value-exact_value);
}
}
// If the value of M is too large then the approximate value and the actual values become same. Error=0