5 Replies - 378 Views - Last Post: 07 December 2012 - 11:39 PM Rate Topic: -----

#1 frenchcookie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-December 12

Calculate integral using analytically and Simpson methods

Posted 06 December 2012 - 04:27 AM

Hello,
I have problem with my program. I am trying to calculate analytically the value of integral with two different set of parameters
a) a=1 b=2 p=1-10^{-n} n=1,..,20
B) a=1 b=1+10^n p=0.5 n=1,..,20

Posted Image

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.
#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;
    


Is This A Good Question/Topic? 0
  • +

Replies To: Calculate integral using analytically and Simpson methods

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1306
  • View blog
  • Posts: 4,495
  • Joined: 19-February 09

Re: Calculate integral using analytically and Simpson methods

Posted 06 December 2012 - 08:56 PM

This return is in the for loop, so the loop is only run once.

016	    return z;}


Was This Post Helpful? 0
  • +
  • -

#3 frenchcookie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-December 12

Re: Calculate integral using analytically and Simpson methods

Posted 07 December 2012 - 12:32 AM

So I should return nothing? When I doing it my program results are #qnan..
Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Calculate integral using analytically and Simpson methods

Posted 07 December 2012 - 01:34 AM

Instead of using return in the for loop, you can return it after the for loop closes.Like this.

013	    for(n=1;n<=20;n++){
014	        p=1 - (pow(10.0, (-n)));
015	        z=1/(pow(x, p));
016	    }
017         return z;
018	}


regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#5 frenchcookie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-December 12

Re: Calculate integral using analytically and Simpson methods

Posted 07 December 2012 - 03:23 AM

I was trying write it like this before and It's not working the result for Simposn rule from n=1 to n=20 is the same.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1306
  • View blog
  • Posts: 4,495
  • Joined: 19-February 09

Re: Calculate integral using analytically and Simpson methods

Posted 07 December 2012 - 11:39 PM

You can print p and pow(10.0, (-n)) and see that the number loses precision when the power is very small.

    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, s1=0;
        cout  << setw(2)  << n << " ";
        p = 1.0 - pow(10.0, (-n));
        cout  << setw(20) << scientific << p << " ";
        cout  << setw(20) << scientific << pow(10.0, (-n)) << " ";

        if(p < 1.0)
          s1 = (pow(b, (1 - p)) - (pow(a, (1 - p))))/(1 - p);

        cout << s1 << " " <<endl;
        // PLIK<<s1<<setprecision(16)<<" "<<endl;

    }

    cin.get();




The data type long double can also be used.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1