Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Simpson\'s Rule). For each integral below, determine an appropriate number n of

ID: 3716606 • Letter: #

Question

(Simpson's Rule). For each integral below, determine an appropriate number n of subintervals such that the error in Simpson's Rule |S_{n}-?_{a}^{b}f(x)dx|<10??. (Note: For #3, no such n can be found; use n=500.) Use the formula |S_{n}-?_{a}^{b}f(x)dx|?((K(b-a)?)/(180n?)), where K is a bound on |f???(x)| on [a,b]. Use Scientific Workplace to assist you. Write a C++ program to estimate the integral by Simpson's Rule; for each integral, input the value of n that you computed. . Run the program for

1.??²x?dx
2.???(x+(1/x))dx
3.???¹2?(1-x²)dx (Note: There is no bound on |f???(x)| for this problem. Use n=500.)
4.??¹(1/(x²+1))dx

Explanation / Answer

//program to evaluate definite integral using simpson's 1/3rd rule
#include<iostream>
#include<cmath>
using namespace std;
double f(double I)
{
double a;   
a=I;
return a;
}
int main()
{ cout.precision(4); //set the precision
cout.setf(ios::fixed);
int n,i,I; //n is for subintervals and i is for loop
double a,b,c,h,sum=0,integral;
cout<<" Enter the limits of integration, Initial limit,a= ";
cin>>a;
cout<<" Final limit, b="; //get the limits of integration
cin>>b;
cout<<" Enter the no. of subintervals(IT SHOULD BE EVEN), n="; //get the no. of subintervals
cin>>n;
cout<<"enter the integral to be calculated :";
cin>>I;

double x[n+1],y[n+1];
h=(b-a)/n; //get the width of the subintervals
for (i=0;i<n+1;i++)
{ //loop to evaluate x0,...xn and y0,...yn
x[i]=a+i*h; //and store them in arrays
y[i]=f(x[i]);
}
for (i=1;i<n;i+=2)
{
sum=sum+4.0*y[i]; //loop to evaluate 4*(y1+y3+y5+...+yn-1)
}
for (i=2;i<n-1;i+=2)
{
sum=sum+2.0*y[i]; /*loop to evaluate 4*(y1+y3+y5+...+yn-1)+
2*(y2+y4+y6+...+yn-2)*/
}
integral=h/3.0*(y[0]+y[n]+sum); //h/3*[y0+yn+4*(y1+y3+y5+...+yn-1)+2*(y2+y4+y6+...+yn-2)]
cout<<" The definite integral is "<<integral<<" "<<endl;
return 0;
}