[go: up one dir, main page]

100% found this document useful (3 votes)
11K views4 pages

Simpson's 1/3rd Rule C Program and Flowchart

1. The document describes Simpson's 1/3 Rule for numerically evaluating definite integrals. 2. The algorithm breaks the interval into even subintervals, calculates the function value at each subdivision point, and weights the function values based on their position to approximate the area under the curve. 3. The program implements Simpson's 1/3 Rule to evaluate sample definite integrals by prompting the user for limits of integration and number of subintervals, calculating the weighted sum of function values, and outputting the estimated integral.

Uploaded by

Richita Ghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
11K views4 pages

Simpson's 1/3rd Rule C Program and Flowchart

1. The document describes Simpson's 1/3 Rule for numerically evaluating definite integrals. 2. The algorithm breaks the interval into even subintervals, calculates the function value at each subdivision point, and weights the function values based on their position to approximate the area under the curve. 3. The program implements Simpson's 1/3 Rule to evaluate sample definite integrals by prompting the user for limits of integration and number of subintervals, calculating the weighted sum of function values, and outputting the estimated integral.

Uploaded by

Richita Ghosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Aim: To evaluate a definite integral by Simpson’s 1/3 Rule

Algorithm:
1. Given a function f(x):
2. (Get user inputs)
Input
a,b=endpoints of interval
n=number of intervals(Even)
(Do the integration)
3. Set h= (b-a)/n.
4. Set sum=0.
5. Begin For i= 1 to n -1
Set x =a + h*i.
If i%2=0
Then Set sum=sum+2*f(x)
Else
Set sum=sum+4*f(x)
End For
6. Set sum = sum + f(a)+f(b)
7. Set ans = sum*(h/3).
8. End
Flow Chart:

Start

Enter a,b and n.

h=(b-a)/n

Sum=0

Do i = 1 to n-1

x= a +h*i

No Is
i%2=0?

Yes
sum=sum+2*f(x)

i<n-1
sum=sum+4*f(x) Next i
i=n-1

sum = sum + f(a)+f(b)

ans= sum*(h/3).

Print ans

End

Program:
//Simpson's 1/3rd Rule for Evaluation of Definite Integrals
#include<iostream>
#include<cmath>
using namespace std;
double f(double x)
{
double a=1/(1+x*x); //write the function whose definite integral is to be calcuated here
return a;
}
int main()
{ cout.precision(4); //set the precision
cout.setf(ios::fixed);
int n,i; //n is for subintervals and i is for loop
double a,b,c,h,sum=0,integral;
cout<<"\nEnter the limits of integration,\n\nInitial limit,a= ";
cin>>a;
cout<<"\nFinal limit, b="; //get the limits of integration
cin>>b;
cout<<"\nEnter the no. of subintervals(IT SHOULD BE EVEN), \nn="; //get the no. of
subintervals
cin>>n;
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<<"\nThe definite integral is "<<integral<<"\n"<<endl;
return 0;
}

Output:
For f(x)=1/(1+x^2):

For f(x)= x^2:

You might also like