Numerical Integration for multiple rectangles

Hi, for my class I have to code a program which will find the integral of x^2 + 2 from 0 to 2 using the rectangle method for multiple rectangles n. I have to output n in one column and the corresponding integral in the other in a text file. In the code below I have loaded in my values of n from the n.txt file. This file is just a column containing 1 and 2. Therefore h will have 2 values. I then compute the area by letting the right hand side of the rectangle lie on the curve.
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

float f(float x);

int main(){


	int i;
	float area, height=0, h, n, a=0, b=2, x, y;
	
	ifstream inFile("n.txt");
	ofstream outFile("out.txt");
	

	outFile<<setw(7)<<"n";
	outFile<<setw(20)<<"Integral"<<endl;
	

	while(!inFile.eof()){
	inFile>>n;
	outFile<<setw(7)<<n;
		
	h=(b-a)/n;
	

	
	for(i=1; i<=n; i++){
	
	
 	height+=f(a+i*h);

	}
	area=height*h;
	
	outFile<<setw(20)<<area<<endl;
	}

	return 0;
	
}

float f(float x){
return pow(x,2)+2;
}


When n=1, I get the correct answer which is f(2)*h = 6*2 = 12. However, for n=2, I get an answer of 15 which shouldn't be the case if we consider two uniform rectangles: f(1)*h + f(2)*h with h=(2-0)/2 = 1. Therefore for 2 rectangles, the result should be (3*1)+(6*1)=9. I'm not sure how to correct my code. Could someone see what I'm doing wrong?

Thanks :)

Last edited on
I think your logic is wrong.

So n should be the number of rectangles you use to compute the integral right? The sum of the area of the rectangles would be the integral. Larger n means a more accurate answer.

Now how do you compute the area of a rectangle? You need to multiply its width and length, or base and height, whatever you want to call it. In this case, we know that the integral is from 0 to 2, represented by n rectangles. So width (or base) of each rectangle is 2.0 / n.

The length (or height) is computed from the calculation x^2 + 2. The x in this case would be i * width for the i-th rectangle. So to calculate the integral, you would need to compute the sum of width * (pow(i * width, 2) + 2) for n rectangles with i representing the current rectangle.

NOTE: I am assuming that the upper right corner of the rectangle touches the curve.

EDIT: Looking at your code, you actually have most of it right. You just need to fix line 35.
Last edited on
Thanks for the reply. I'm not sure what I have to do to line 35. Wouldn't f(a+i*h) cover it because a=0? And you're right, having more rectangles should give me a more accurate answer, but my code computes the calculation incorrectly and I'm not sure why.
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
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>

using namespace std;

float f(float x);

int main()
{
    int numRects;
    float base, height, area, totalArea;
    const float RANGE_BEGIN = 0.0;
    const float RANGE_END = 2.0;
    const string STR = "1 2 3 4 5 6 7 8";
    istringstream in(STR);

    cout << setw(12) << "Rectangles" << setw(12) << "Area\n";
    cout << string(24, '-') << endl;

    while (in >> numRects)
    {
        totalArea = 0.0;
        base = (RANGE_END - RANGE_BEGIN) / numRects;
        for (int i = 1; i <= numRects; ++i)
        {
            height = f(i * base);
            area = base * height;
            totalArea += area;
        }
        cout << setw(12) << numRects;
        cout << setw(12) << setprecision(4) << totalArea << endl;
    }
    return 0;
}

float f(float x){
return pow(x,2)+2;
}
Thanks for this! But how do I manipulate the code so the number of rectangles is loaded from a .txt file, and the table is outputted in a .txt file?
You showed that you could do file input/output in the code in your first post. Did someone else write that for you? If so, well then you need to at least do some work of your own.
Nope, I wrote it myself and have been able to change it. Thanks for your help :)
Topic archived. No new replies allowed.