Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Mathematics ProgramRSS Feeds

Program to estimate the Integral value of the function at the given points from the given data using Simpson's 1/3 Rule

Posted By: Easy Tutor     Category: C++ Programming     Views: 2484

A C++ Program to estimate the Integral value of the function at the given points from the given data using Simpson's 1/3 Rule.

Code for Program to estimate the Integral value of the function at the given points from the given data using Simpson's 1/3 Rule in C++ Programming

/*************************************************************************//*************************************************************************       A C++ Program to estimate the Integral value of the function at       the given points from the given data using Simpson's 1/3 Rule.  *************************************************************************//*************************************************************************/

 # include <iostream.h>
 # include   <stdlib.h>
 # include   <string.h>
 # include    <stdio.h>
 # include    <conio.h>
 # include     <math.h>

 constint max_size=13;

 int n=0;
 int top=-1;
 int choice=0;

 longdouble h=0;
 longdouble a=0;
 longdouble b=0;
 longdouble estimated_value=0;

 longdouble xn[max_size]={0};
 longdouble fx[max_size]={0};

 char Fx[100]={NULL};
 char Stack[30][30]={NULL};
 char Postfix_expression[30][30]={NULL};

 /*************************************************************************//*************************************************************************///------------------------  Funcion Prototypes  -------------------------///*************************************************************************//*************************************************************************/void push(constchar *);
 void convert_ie_to_pe(constchar *);

 constchar* pop( );
 constlongdouble evaluate_postfix_expression(constlongdouble);

 void show_screen( );
 void clear_screen( );
 void get_input( );
 void apply_simpsons_rule( );
 void show_result( );

 /*************************************************************************//*************************************************************************///------------------------------  main( )  ------------------------------///*************************************************************************//*************************************************************************/int main( )
    {
       clrscr( );
       textmode(C4350);

       show_screen( );
       get_input( );
       apply_simpsons_rule( );
       show_result( );

       getch( );
       return 0;
     }

 /*************************************************************************//*************************************************************************///------------------------  Funcion Definitions  ------------------------///*************************************************************************//*************************************************************************//*************************************************************************///--------------------------  show_screen( )  ---------------------------///*************************************************************************/void show_screen( )
    {
       cprintf("\n********************************************************************************");
       cprintf("***************************-                         -**************************");
       cprintf("*--------------------------- ");

       textbackground(1);
       cprintf(" Numerical Integration ");
       textbackground(8);

       cprintf(" --------------------------*");
       cprintf("*-*************************-                         -************************-*");
       cprintf("*-****************************************************************************-*");

       for(int count=0;count<42;count++)
      cprintf("*-*                                                                          *-*");

       gotoxy(1,46);
       cprintf("*-****************************************************************************-*");
       cprintf("*------------------------------------------------------------------------------*");
       cprintf("********************************************************************************");

       gotoxy(1,2);
    }

 /*************************************************************************///-------------------------  clear_screen( )  ---------------------------///*************************************************************************/void clear_screen( )
    {
       for(int count=0;count<37;count++)
      {
         gotoxy(5,8+count);
         cout<<"                                                                        ";
      }

       gotoxy(1,2);
    }

 /*************************************************************************///--------------------------  push(const char*)  ------------------------///*************************************************************************/void push(constchar* Operand)
    {
       if(top==(max_size-1))
      {
         cout<<"Error : Stack is full."<<endl;
         cout<<"\n        Press any key to exit.";

         getch( );
         exit(0);
      }

       else
      {
         top++;
         strcpy(Stack[top],Operand);
      }
    }

 /*************************************************************************///------------------------------  pop( )  -------------------------------///*************************************************************************/constchar* pop( )
    {
       char Operand[40]={NULL};

       if(top==-1)
      {
         cout<<"Error : Stack is empty."<<endl;
         cout<<"\n        Press any key to exit.";

         getch( );
         exit(0);
      }

       else
      {
         strcpy(Operand,Stack[top]);
         strset(Stack[top],NULL);
         top--;
      }

       return Operand;
    }

 /*************************************************************************///--------------------  convert_ie_to_pe(const char*)  ------------------///*************************************************************************/void convert_ie_to_pe(constchar* Expression)
    {
       char Infix_expression[100]={NULL};
       char Symbol_scanned[30]={NULL};

       push("(");
       strcpy(Infix_expression,Expression);
       strcat(Infix_expression,"+0)");

       int flag=0;
       int count_1=0;
       int count_2=0;
       int equation_length=strlen(Infix_expression);

       if(Infix_expression[0]=='(')
      flag=1;

       do
      {
         strset(Symbol_scanned,NULL);

         if(flag==0)
        {
           int count_3=0;

           do
              {
             Symbol_scanned[count_3]=Infix_expression[count_1];

             count_1++;
             count_3++;
              }
           while(count_1<=equation_length &&
               Infix_expression[count_1]!='(' &&
                  Infix_expression[count_1]!='+' &&
                 Infix_expression[count_1]!='-' &&
                    Infix_expression[count_1]!='*' &&
                       Infix_expression[count_1]!='/' &&
                      Infix_expression[count_1]!='^' &&
                         Infix_expression[count_1]!=')');


           flag=1;
        }

         elseif(flag==1)
        {
           Symbol_scanned[0]=Infix_expression[count_1];

           count_1++;

           if(Infix_expression[count_1]!='(' &&
             Infix_expression[count_1]!='^' &&
                Infix_expression[count_1]!='*' &&
                   Infix_expression[count_1]!='/' &&
                  Infix_expression[count_1]!='+' &&
                     Infix_expression[count_1]!='-' &&
                    Infix_expression[count_1]!=')')
              flag=0;

           if(Infix_expression[count_1-1]=='(' &&
               (Infix_expression[count_1]=='-' ||
                 Infix_expression[count_1]=='+'))
              flag=0;
        }

         if(strcmp(Symbol_scanned,"(")==0)
        push("(");

         elseif(strcmp(Symbol_scanned,")")==0)
        {
           while(strcmp(Stack[top],"(")!=0)
              {
             strcpy(Postfix_expression[count_2],pop( ));

             count_2++;
              }

           pop( );
        }

         elseif(strcmp(Symbol_scanned,"^")==0 ||
               strcmp(Symbol_scanned,"+")==0 ||
                 strcmp(Symbol_scanned,"-")==0 ||
                       strcmp(Symbol_scanned,"*")==0 ||
                         strcmp(Symbol_scanned,"/")==0)
        {
           if(strcmp(Symbol_scanned,"^")==0)
              {  }

           elseif(strcmp(Symbol_scanned,"*")==0 ||
                          strcmp(Symbol_scanned,"/")==0)
              {
             while(strcmp(Stack[top],"^")==0 ||
                     strcmp(Stack[top],"*")==0 ||
                       strcmp(Stack[top],"/")==0)
                {
                   strcpy(Postfix_expression[count_2],pop( ));

                   count_2++;
                }
              }

           elseif(strcmp(Symbol_scanned,"+")==0 ||
                    strcmp(Symbol_scanned,"-")==0)
              {
             while(strcmp(Stack[top],"(")!=0)
                {
                   strcpy(Postfix_expression[count_2],pop( ));

                   count_2++;
                }
              }

           push(Symbol_scanned);
        }

         else
        {
           strcat(Postfix_expression[count_2],Symbol_scanned);

           count_2++;
        }
      }
       while(strcmp(Stack[top],NULL)!=0);

       strcat(Postfix_expression[count_2],"=");
       count_2++;
    }

 /*************************************************************************///----------  evaluate_postfix_expression(const long double)  -----------///*************************************************************************/constlongdouble evaluate_postfix_expression(constlongdouble x)
    {
       longdouble function_value=0;

       int count_1=-1;

       char Symbol_scanned[30]={NULL};

       do
      {
         count_1++;

         strcpy(Symbol_scanned,Postfix_expression[count_1]);

         if(strcmp(Symbol_scanned,"^")==0 ||
              strcmp(Symbol_scanned,"*")==0 ||
                strcmp(Symbol_scanned,"/")==0 ||
                  strcmp(Symbol_scanned,"+")==0 ||
                    strcmp(Symbol_scanned,"-")==0)

        {
           char Result[30]={NULL};
           char Operand[2][30]={NULL};

           strcpy(Operand[0],pop( ));
           strcpy(Operand[1],pop( ));

           longdouble operand[2]={0};
           longdouble result=0;

           char *endptr;

           for(int count_2=0;count_2<2;count_2++)
              {
             int flag=0;

             if(Operand[count_2][0]=='-')
                {
                   int length=strlen(Operand[count_2]);

                   for(int count_3=0;count_3<(length-1);count_3++)
                  Operand[count_2][count_3]=Operand[count_2][(count_3+1)];

                   Operand[count_2][count_3]=NULL;

                   flag=1;
                }

             if(strcmp(Operand[count_2],"x")==0)
                operand[count_2]=x;

             elseif(strcmp(Operand[count_2],"e")==0)
                operand[count_2]=2.718282;

             elseif(strcmp(Operand[count_2],"sinx")==0)
                operand[count_2]=sinl(x);

             elseif(strcmp(Operand[count_2],"cosx")==0)
                operand[count_2]=cosl(x);

             elseif(strcmp(Operand[count_2],"tanx")==0)
                operand[count_2]=tanl(x);

             elseif(strcmp(Operand[count_2],"lnx")==0)
                operand[count_2]=logl(x);

             elseif(strcmp(Operand[count_2],"logx")==0)
                operand[count_2]=log10l(x);

             else
                operand[count_2]=strtod(Operand[count_2],&endptr);

             if(flag)
                operand[count_2]*=-1;
              }

           switch(Symbol_scanned[0])
              {
             case'^' : result=powl(operand[1],operand[0]);
                    break;

             case'*' : result=operand[1]*operand[0];
                    break;

             case'/' : result=operand[1]/operand[0];
                    break;

             case'+' : result=operand[1]+operand[0];
                    break;

             case'-' : result=operand[1]-operand[0];
                    break;
              }

           gcvt(result,25,Result);

           push(Result);
        }

         elseif(strcmp(Symbol_scanned,"=")!=0)
        push(Symbol_scanned);
      }
       while(strcmp(Symbol_scanned,"=")!=0);

       char Function_value[30]={NULL};
       char *endptr;

       strcpy(Function_value,pop( ));
       function_value=strtod(Function_value,&endptr);

       return function_value;
    }

 /*************************************************************************///-----------------------------  get_input( )  --------------------------///*************************************************************************/void get_input( )
    {
       do
      {
         clear_screen( );

         gotoxy(6,9);
         cout<<"Number of Sub-Intervals :";

         gotoxy(6,10);
         cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

         gotoxy(20,13);
         cout<<"[ min. n = 2  |  max. n = 12 ]";

         gotoxy(6,12);
         cout<<"Enter the max. number of sub-intervals = n = ";

         cin>>n;

         if(n<2 || n>12)
        {
           gotoxy(12,25);
           cout<<"Error : Wrong Input. Press <Esc> to exit or any other key";

           gotoxy(12,26);
           cout<<"        to try again.";

           n=int(getche( ));

           if(n==27)
              exit(0);
        }
      }
       while(n<2 || n>12);

       gotoxy(6,16);
       cout<<"Enter the value of Lower limit = a = ";

       cin>>a;

       gotoxy(6,18);
       cout<<"Enter the value of Upper Limit = b = ";

       cin>>b;

       h=((b-a)/n);

       gotoxy(6,24);
       cout<<"Input Mode :";

       gotoxy(6,25);
       cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍ";

       gotoxy(8,28);
       cout<<"Press : ";

       gotoxy(10,30);
       cout<<"- 'Y' or <Enter> to enter function";

       gotoxy(10,32);
       cout<<"- 'N' or <Any other key> to enter values of the function";

       gotoxy(8,35);
       cout<<"Enter your choice : ";

       char Choice=NULL;

       Choice=getch( );

       if(Choice=='y' || Choice=='Y' || int(Choice)==13)
      {
         choice=1;

         gotoxy(28,35);
         cout<<"Y";
      }

       else
      {
         gotoxy(28,35);
         cout<<"N";
      }

       gotoxy(25,43);
       cout<<"Press any key to continue...";

       getch( );

       if(choice)
      {
         clear_screen( );

         gotoxy(6,10);
         cout<<"Non-Linear Function :";

         gotoxy(6,11);
         cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

         gotoxy(6,37);
         cout<<"Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3";

         gotoxy(6,40);
         cout<<"Available Operators  :  ^ (raised to power) , * , / , + , -";

         gotoxy(6,42);
         cout<<"Available Operands   :  x , e , sinx , cosx , tanx , lnx , logx ,";

         gotoxy(6,44);
         cout<<"                        n = any number";

         gotoxy(6,14);
         cout<<"Enter the Function : f(x) = ";

         cin>>Fx;

         convert_ie_to_pe(Fx);
      }

       clear_screen( );

       gotoxy(6,9);
       cout<<"Data Points & Values of Function :";

       gotoxy(6,10);
       cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

       gotoxy(25,12);
       cout<<"ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿";

       gotoxy(25,13);
       cout<<"³       x       ³     f(x)      ³";

       gotoxy(25,14);
       cout<<"ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´";

       gotoxy(25,15);
       cout<<"³               ³               ³";

       for(int count_1=0;count_1<=n;count_1++)
      {
         gotoxy(25,(wherey( )+1));
         cout<<"³               ³               ³";

         gotoxy(25,(wherey( )+1));
         cout<<"³               ³               ³";
      }

       gotoxy(25,(wherey( )+1));
       cout<<"ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ";

       xn[0]=a;

       for(int count_2=0;count_2<n;count_2++)
      xn[(count_2+1)]=(xn[count_2]+h);

       gotoxy(25,16);

       for(int count_3=0;count_3<=n;count_3++)
      {
         gotoxy(27,wherey( ));
         cout<<xn[count_3];

         if(choice)
        {
           fx[count_3]=evaluate_postfix_expression(xn[count_3]);

           gotoxy(43,wherey( ));
           cout<<fx[count_3];
        }

         else
        {
           gotoxy(43,wherey( ));
           cin>>fx[count_3];
        }

         if(choice)
        gotoxy(25,(wherey( )+2));

         else
        gotoxy(25,(wherey( )+1));
      }

       gotoxy(25,43);
       cout<<"Press any key to continue...";

       getch( );
    }

 /*************************************************************************///----------------------  apply_simpsons_rule( )  -----------------------///*************************************************************************/void apply_simpsons_rule( )
    {
       longdouble temp=0;

       estimated_value=(fx[0]+fx[n]);
       estimated_value*=h;
       estimated_value/=3;

       temp=0;

       for(int count_1=2;count_1<=(n-2);count_1+=2)
      temp+=fx[count_1];

       temp*=(2*h);
       temp/=3;

       estimated_value+=temp;

       temp=0;

       for(int count_2=1;count_2<=(n-1);count_2+=2)
      temp+=fx[count_2];

       temp*=(4*h);
       temp/=3;

       estimated_value+=temp;
    }

 /*************************************************************************///-----------------------------  show_result( )  ------------------------///*************************************************************************/void show_result( )
    {
       clear_screen( );

       gotoxy(6,9);
       cout<<"Simpson's 1/3 Rule :";

       gotoxy(6,10);
       cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

       gotoxy(8,12);
       cout<<" bô";

       gotoxy(10,13);
       cout<<"³f(x)dx ÷ (h/3)(f0+fn) + (2h/3)[f2+f4+...+f(n-2)] +";

       gotoxy(50,15);
       cout<<"(4h/3)[f1+f3+...+f(n-1)]";

       gotoxy(8,14);
       cout<<" aõ";

       gotoxy(6,17);
       cout<<"              bô";

       gotoxy(6,19);
       cout<<"              aõ";

       gotoxy(6,18);
       cout<<"Estimation of  ³f(x)dx :";

       gotoxy(6,20);
       cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";

       gotoxy(8,23);
       cout<<"Estimated Integral Value = ";

       cout<<estimated_value;

       gotoxy(1,2);
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program to estimate the Integral value of the function at the given points from the given data using Simpson's 1/3 Rule is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!