# Integral calculus using analytically and Simpson methods

• Born
• Posts: 1

3+ Months Ago

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.

CPP Code: [ Select ]
#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;

1. #include <iomanip>
2. #include <iostream>
3. #include <cmath>
4. #include <fstream>
5.
6. using namespace std;
7. const int N = 1;
8.
9. double f(double x)
10. {
11.     double dx, b=2.0, z, p;
12.     int n;
13.     for(n=1;n<=20;n++){
14.         p=1 - (pow(10.0, (-n)));
15.         z=1/(pow(x, p));
16.     return z;}
17. }
18.
19. double fx(double x)
20. {
21.     double n;
22.     double p=0.5;
23.     double b=1 + (pow(10.0, (-n)));
24.     double dx=(b-1)/N;
25.     double z=dx/(pow(x, p));
26.     return z;
27. }
28.
29. int main()
30. {
31.
32.     ofstream PLIK;
33.
34.
35.     double a, b, s1,s2,s3,s4,x, p; int i;
36.     int n;
37.     s1=0;s3=0;
38.     s2=0;s4=0;
39.
40.     cout.unsetf(ios::floatfield);
41.     cout.precision(16);
42.    PLIK.open("RESULTS.txt");
43.
44.
45.     cout << "ANALYTICALLY." << endl;
46.     PLIK<<"ANALYTICALLY A<<endl;
47.    cout << "SECTION A:" << endl; // Podpunkt pierwszy
48.    for (n = 1; n <= 20; n++)
49.    {
50.
51.        a=1.0; b=2.0;
52.        cout  << n << " ";
53.        p = 1 - pow(10.0, (-n));
54.
55.        s1 = (pow(b, (1 - p)) - (pow(a, (1 - p))))/(1 - p);
56.        cout <<s1<<" " << endl;
57.        PLIK<<s1<<setprecision(16)<<" "<<endl;
58.
59.    }
60.
61.
62.    PLIK<<"ANALYTICALLY B"<<endl;
63.    cout << "ANALYTICALLY " << endl;
64.    cout << "SECTION B:" << endl; // Podpunkt drugii
65.    for (n = 1; n <= 20; n++)
66.    {
67.        a=1.0; p=0.5;
68.        cout << n << " ";
69.        b = 1 + pow(10.0, (-n));
70.
71.        s2 = (pow(b, (1 - p)) - (pow(a, (1 - p))))/(1 - p);
72.        cout << s2<<" " << endl;
73.        PLIK<<s2<<setprecision(16)<<" "<<endl;
74.
75.    }
76.
77.
78.
79.    PLIK<<"SIMPSON A"<<endl;
80.    cout << "SIMPSON RULE" << endl;
81.    cout << "SECTION A" << endl;
82.
83.    for (n = 1; n <= 20; n++)
84.    {
85.        p=1 - (pow(10.0, (-n)));
86.        a=1.0;
87.        cout  << n << " ";
88.        b=2.0;
89.
90.        s3 = (b-a)/ 6 * (f(a) + (4*(f((a+b)/2)))+f(b));
91.        cout << s3<<" " << endl;
92.        PLIK<<s3<<setprecision(16)<<" "<<endl;
93.
94.    }
95.
96.
97.
98.    PLIK<<"SIMPSON B"<<endl;
99.    cout << "SIMPSON RULE " << endl;
100.    cout << "SECTION B" << endl;
101.
102.
103.    for (n = 1; n <= 20; n++)
104.    {
105.        a=1.0;p=0.5;
106.        cout << n << " ";
107.        b=1 + pow(10.0, (-n));
108.
109.        s4 = (b-a)/ 6 * (fx(a) + (4*(fx((a+b)/2)))+fx(b));
110.        cout <<  s4 << " "  << endl;
111.        PLIK<<s4<<setprecision(16)<<" "<<endl;
112.
113.    }
114.
115.    PLIK.close();
116.
117.    cout<<endl;
118.
119.
Attachments:

My integral calculus

## Post Information

• Total Posts in this topic: 1 post
• Users browsing this forum: No registered users and 33 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum