//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;
}

Ph.D. researcher at Friedrich-Schiller University Jena, Germany. I’m a physicist specializing in computational material science. I write efficient codes for simulating light-matter interactions at atomic scales. I like to develop Physics, DFT, and Machine Learning related apps and software from time to time. Can code in most of the popular languages. I like to share my knowledge in Physics and applications using this Blog and a YouTube channel.

Hi there,
umm you could try integrating one of the variables first and then substitute the value of the definite integral back into the original equation in place of the variable, and then integrate w.r.t the other variable.
For example: If you have an equation: xy and you need to integrate w.r.t both ‘x’ and ‘y’ between some limits. Then you should first simply integrate ‘x’ within the limits given using the simpson’s method and then substitute this value in place of ‘x’ in the original equation and then integrate that w.r.t ‘y’.
Hope it helps.

Hi there,
I’m not familiar with what you’re asking. I would need a little more information than that. And also tell me what your approach is to the problem. Then i might be able to help you in a better way.

Hi there,
I’m not familiar with what you’re asking. I would need a little more information than that. And also tell me what your approach is to the problem. Then i might be able to help you in a better way.

dude, I think something is wrong with your code, I don’t know exactly what, but the definite integral of 1/(1+x*x) from 0 to 6 is 1.40564764938 instead 1.3662, I know that Simpsons 1/3 methode isn’t an exact methode, but I changed the function to 1/(pow((1-x),1/2)) from 0 to 0.9999 and the answer would be 1.98 and your code shows me 0.9999

Hi,

I want to know how to calculate double definite integration using simpson rule.

Thanks

Hi there,

umm you could try integrating one of the variables first and then substitute the value of the definite integral back into the original equation in place of the variable, and then integrate w.r.t the other variable.

For example: If you have an equation: xy and you need to integrate w.r.t both ‘x’ and ‘y’ between some limits. Then you should first simply integrate ‘x’ within the limits given using the simpson’s method and then substitute this value in place of ‘x’ in the original equation and then integrate that w.r.t ‘y’.

Hope it helps.

please upload c++ programming of HCP structure with its coordinates?????????please upload soon

Hi there,

I’m not familiar with what you’re asking. I would need a little more information than that. And also tell me what your approach is to the problem. Then i might be able to help you in a better way.

I want to newtons raphson method solution to vanderwalls equation

Hi there,

I’m not familiar with what you’re asking. I would need a little more information than that. And also tell me what your approach is to the problem. Then i might be able to help you in a better way.

Thank you sir

dude, I think something is wrong with your code, I don’t know exactly what, but the definite integral of 1/(1+x*x) from 0 to 6 is 1.40564764938 instead 1.3662, I know that Simpsons 1/3 methode isn’t an exact methode, but I changed the function to 1/(pow((1-x),1/2)) from 0 to 0.9999 and the answer would be 1.98 and your code shows me 0.9999

Hi there, The program is indeed correct.

For the example you get 1.3662 because I only used 6 sub-intervals which is less. If you increase that you get the expected 1.405.

Regarding the example you give : 1/(pow((1-x),1/2), this is written incorrectly.

In C++ you should be careful with floats, write is as 1/(pow((1-x),0.5) and use a lot of sub-intervals n=1000 then you get something around 1.98.

You can copy and paste the program here a and run and you will understand what I mean.

https://www.onlinegdb.com/online_c++_compiler