C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 6th, 2012, 05:56 AM
frenchcookie frenchcookie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 frenchcookie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 34 sec
Reputation 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

URL

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;
       
     

Reply With Quote
  #2  
Old December 6th, 2012, 06:19 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 9 m 51 sec
Reputation Power: 1774
__________________
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Integral calculus using analytically and Simpson methods

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap