Outputting incorrect values

My program has an error in it but i'm not able to pinpoint where it is and what is causing it. I know I am not getting the correct values when it is outputted and I wasn't sure if someone could help me finding out what I did wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  // A program to approximate a definite integal by the trapezoidal rule.
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>

using namespace std;

double f(double); // function prototype
int main(void)
{
	const int s = 30; // maximum number of steps
	double a, b, tol; // lower and upper limits, tolerance
	double n = 1; // number of intervals; doubles each time through loop.
	double T, M; // trapezoidal and midpoint rule estimates
	double x; // value of x
	double sum; // sum in midpoint computation
	double h; // length of an interval
	char zzz; // holds screen
	const char filepath[] = "C:\\CoutputFiles\\File5.txt";
	const char ErrorMessage[] = "Can't open file ";
	fstream OutStream(filepath, ios::out);
	if (OutStream.fail())
	{
		cerr << ErrorMessage << filepath;
		cin >> zzz;
		exit(-1);
	}
	cout << "Enter the lower and upper limits and the tolerance, separated by spaces\n";
	cin >> a >> b >> tol;
	OutStream << setprecision(10);
	h = b - a;
	T = (f(a) + f(b)) / 2;
	for (int i = 1; i <= s; i++)
	{
		// compute Midpoint formula
		sum = 0;
		x = a + h / 2;
		for (int j = 1; j <= n; j++)
		{
			sum += f(x);
			x += h;
		}
		M = h*sum;
		// Output to file
		OutStream << i << "," << T << "," << M << ",";
		if (fabs(T - M) < tol)
			break;
		// Update Trapezoidal, h, n
		T = (T + M) / 2;
		n *= 2;
		h /= 2;
	}
	cout << "Enter a character to end.\n";
	cin >> zzz;
	return 0;
}
double f(double x)
{
	return x*x*x;
}
Why do you think it's wrong?

It's a slightly obscure method of doing it on successively finer meshes, but it seems to give the right answer for the limits that I tried.

Integrate x3 and you will get x4/4. Integrate between 2 and 4, say, and you will get
(44-24)/4 = 60.

I tried with a = 2, b = 4, with tolerance 0.00001, and got the correct answer: 60 (though I had to re-format your output to read it sensibly, and rename the output file.)


You could helpfully make the output line
OutStream << i << "," << T << "," << M << "\n";
or, better still,
cout << i << "," << T << "," << M << "\n";


Answer as in the last two values below.

Enter the lower and upper limits and the tolerance, separated by spaces
2 4 0.00001

1,36,54
2,45,58.5
3,51.75,59.625
4,55.6875,59.9063
5,57.7969,59.9766
6,58.8867,59.9941
7,59.4404,59.9985
8,59.7195,59.9996
9,59.8596,59.9999
10,59.9297,60
11,59.9649,60
12,59.9824,60
13,59.9912,60
14,59.9956,60
15,59.9978,60
16,59.9989,60
17,59.9995,60
18,59.9997,60
19,59.9999,60
20,59.9999,60
21,60,60
22,60,60
23,60,60
Last edited on
Yea i'm not really sure what is wrong with the program though, we need to use a lower limit of 0 and upper limit of 2 with epsilon 1e-10 (don't think that makes a difference though), I was just told that the values that were outputted were wrong but im not sure how or why.
Well, it's correct to a sensible tolerance when the limits are 0 and 2 (answer is 4).

However, epsilon = 1e-10 is an incredibly demanding (ridiculously demanding?) tolerance and seems to require more than the 30 halvings of intervals that you are allowing for. You may need to increase the size of all integers (unsigned long long) and boost the precision of doubles (to long double) to get that.

If it's being machine-marked, then check that your output format is OK - your current code has no spaces in the output, for example, and the precision manipulator isn't sticky, so items aren't coming out to 10 sig figs.
Last edited on
Topic archived. No new replies allowed.