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

New Free Tools on Dev Shed!

#1
December 6th, 2012, 06:56 AM
Registered User

Join Date: Dec 2012
Posts: 1
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;

```

#2
December 6th, 2012, 07:19 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,266
Time spent in forums: 2 Months 4 Weeks 1 Day 18 h 47 m 43 sec
Reputation Power: 1827
__________________
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

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Integral calculus using analytically and Simpson methods