Math, asked by mrkwoledge3576, 1 year ago

Find the value of non-integral by simpson's one therd rule c++

Answers

Answered by monish48kumar
6

A C++ Program To evaluate a Definite Integral by Simpson’s 1/3 Rule.

*/

#include<iostream>  //Header file for cin & cout

#include<cmath>  //Header file for mathematical operartions

using namespace std;  //calling the standard directory

//Taking a function f(x)

float f(float(x))

{

   return (pow(x,3)+pow(x,2)-(4*x)-5);

}

//Taking diffrentiation of f(x) i.e. g(x)

float g(float(x))

{

   return (3*pow(x,2)+2*x-4);

}

//Taking double diffrentiation of f(x) i.e. h(x)

float h(float(x))

{

   return (6*x+4);

}

int main()  //Main Program

{

   int i;

   long double a,b,d,n,I=0,J=0,A,K=0,E=0;

   cout<<" Given f(x)= x^3 + 2x^2 - 4x - 5 "<<endl;

   cout<<"Enter lower limit "<<endl;

   cin>>a;

   cout<<"Enter Upper Limit "<<endl;

   cin>>b;

   cout<<"Enter the number of intervals : "<<endl;

   cin>>n;

   d=(b-a)/n;

   //Steps of solving by Simpson's 1/3-Rule

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

   {

       if((i%2)!=0)

       {

           I=I+f(a+(i*d));

       }

   }

   for(i=2;i<n-1;i++)

   {

       if((i%2)==0)

       {

           J=J+f(a+(i*d));

       }

   }

   A=(d/3)*(f(a)+(4*I)+(2*J)+f(b));

   //Printing the value of Integral

   cout<<"The Value of integral under the enterd limits is : "<<endl;

   cout<<A<<endl;

   //Calculation of Error

   E=-((b-a)*d*d*d*d*6/180);

   //Printing the Total Error

   cout<<"The Total Error is : "<<endl;

   cout<<E<<endl;

   return 0;

}

/*

Given f(x)= x^3 + 2x^2 - 4x - 5

Enter lower limit

0

Enter Upper Limit

5

Enter the number of intervals :

100

The Value of integral under the enterd limits is :

122.917

The Total Error is :

-1.04167e-06

*/

Similar questions