#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Integral calculus using analytically and Simpson methods


    Hello,
    I have problem with my program. I am trying to calculate analytically the value of integral with two different set of parameters



    and then for both I calculate the value using Simpson rule.

    I don't know how to modify my code to calculate a) with Simpson rule, because now all the results are the same n and I don't know why after n=15 all my results are 0.

    I was reading about double representation and I was analysing the Simpson rule and my analytically code for integral but I can't figure it out why there are 0, because I calculate it with Mathematica to improve my results and it gives
    my some numbers not 0.

    For now I have this code, the results from section b) and analytically from a) are ok expect n>15 where I have 0.

    Code:
     
        #include <iomanip>
        #include <iostream>
        #include <cmath>
        #include <fstream>
         
        using namespace std;
        const int N = 1;
         
        double f(double x)
        {
            double dx, b=2.0, z, p;
            int n;
            for(n=1;n<=20;n++){
                p=1 - (pow(10.0, (-n)));
                z=1/(pow(x, p));
            return z;}
        }
         
        double fx(double x)
        {
            double n;
            double p=0.5;
            double b=1 + (pow(10.0, (-n)));
            double dx=(b-1)/N;
            double z=dx/(pow(x, p));
            return z;
        }
         
        int main()
        {
           
            ofstream PLIK;
           
           
            double a, b, s1,s2,s3,s4,x, p; int i;
            int n;
            s1=0;s3=0;
            s2=0;s4=0;
           
            cout.unsetf(ios::floatfield);            
            cout.precision(16);
           PLIK.open("RESULTS.txt");
           
         
            cout << "ANALYTICALLY." << endl;
            PLIK<<"ANALYTICALLY A<<endl;
           cout << "SECTION A:" << endl; // Podpunkt pierwszy
           for (n = 1; n <= 20; n++)
           {  
               
               a=1.0; b=2.0;
               cout  << n << " ";
               p = 1 - pow(10.0, (-n));
               
               s1 = (pow(b, (1 - p)) - (pow(a, (1 - p))))/(1 - p);
               cout <<s1<<" " << endl;
               PLIK<<s1<<setprecision(16)<<" "<<endl;
               
           }
         
           
           PLIK<<"ANALYTICALLY B"<<endl;
           cout << "ANALYTICALLY " << endl;
           cout << "SECTION B:" << endl; // Podpunkt drugii
           for (n = 1; n <= 20; n++)
           {
               a=1.0; p=0.5;
               cout << n << " ";
               b = 1 + pow(10.0, (-n));
               
               s2 = (pow(b, (1 - p)) - (pow(a, (1 - p))))/(1 - p);
               cout << s2<<" " << endl;
               PLIK<<s2<<setprecision(16)<<" "<<endl;
               
           }
           
         
           
           PLIK<<"SIMPSON A"<<endl;
           cout << "SIMPSON RULE" << endl;
           cout << "SECTION A" << endl;
           
           for (n = 1; n <= 20; n++)
           {  
               p=1 - (pow(10.0, (-n)));
               a=1.0;
               cout  << n << " ";
               b=2.0;
               
               s3 = (b-a)/ 6 * (f(a) + (4*(f((a+b)/2)))+f(b));
               cout << s3<<" " << endl;
               PLIK<<s3<<setprecision(16)<<" "<<endl;
               
           }
         
           
           
           PLIK<<"SIMPSON B"<<endl;
           cout << "SIMPSON RULE " << endl;
           cout << "SECTION B" << endl;
           
           
           for (n = 1; n <= 20; n++)
           {  
               a=1.0;p=0.5;
               cout << n << " ";
               b=1 + pow(10.0, (-n));
             
               s4 = (b-a)/ 6 * (fx(a) + (4*(fx((a+b)/2)))+fx(b));
               cout <<  s4 << " "  << endl;
               PLIK<<s4<<setprecision(16)<<" "<<endl;
               
           }
         
           PLIK.close();
           
           cout<<endl;
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo