Integration of a function

Hi, I'm having trouble. I'm writing a code to integrate a function. What I've done seems right to me but my calculation seems to be wrong some where. I've been entering 1 for a, 10 for b, and 10, for n. The answer should be like 125000 but I'm only getting 53153.1. Can anyone see what I'm doing wrong?
Thank you. :)

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
#include <iostream>
#include <cmath>

using namespace std;

void user_input( double &a, double &b, double &n )
{

	cout << "Enter lower bounds: ";
	cin >> a;

	cout << "Enter upper bounds: ";
	cin >> b;

	cout << "Enter number of rectangles: ";
	cin >> n;

}

double iterative( double &a, double &b, double &n, double &f, double &width, double &area )
{

	width = ( (b-a)/n );

	while ( a < b )
	{
	
		f = ( pow(a, 5) + 10 );
		area = ( f*width );
		a++;
	
	}

	return area;

}

int main()
{

	double a, b, n, f, width, area;

	user_input(a, b, n);
	cout << a << "  " << b << "  " << n << endl;

	cout << iterative( a, b, n, f, width, area ) << endl;

	return 0;

}
I am getting the same results with the same input.

Enter lower bounds: 1
Enter upper bounds: 10
Enter number of rectangles: 10
1  10  10
53153.1


What do you want your output to be?
What makes this wrong?
Last edited on
It's a left hand reimann sum. The correct answer is 125126.
Last edited on
closed account (D80DSL3A)
@Bdanielz
His function is for approximating a definite integral (calculus).
Symbolic evaluation (using calculus) gives an exact value = 166,756.5
He's expecting about 12500 from his approximation over just 10 sub intervals.

@brosephius.
Two problems in the function.
You should be adding the areas. Line 29 should be: area += f*width;
line 30: the sub interval length is width, not 1.
should be a += width;
Also, f, width and area should be local variables in the function, not uninitialized values being passed by reference from outside the function.

I get 165,074 for N=10, but I'm evaluating at the interval midpoints.

EDIT: Just saw your 2nd post. When I evaluate at left end of intervals I get 125126 as you say.
Last edited on
Awesome thank you! That's been frustrating me all day. It's now giving me the correct answer. So now there's the deal with making f, width, and area local. I had a TA tell me earlier that I went about that a little weird. I now have to write a recursive function that does the same thing and I'm going to have the user choose which method to use. My TA said that my variables will give me problems when I do that.
So, how should I go about making f, width, and area local? I have yet to successfully pass by value. There's probably something minor I'm missing. Or should I not be passing at all? I'm still not very good in the area of getting variables/values/addresses moved between functions. The pass by reference is the only thing I could get to work on my last code which is why I tried to stick with it. Thanks again. :)
closed account (D80DSL3A)
You're welcome.
The only values needed from outside the function are those supplied by the user, namely a,b and n.
The rest can be declared and used within the function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double iterative( double a, double b, double n )
{
        double f=0.0;
        double area=0.0;
	double width = ( (b-a)/n );

	while ( a < b )
	{
	
		f = ( pow(a, 5) + 10 );
		area += ( f*width );
		a += width;
	
	}

	return area;

}

Now I gotta try a recursive version, which I think will be a bit different.
sum over N areas = one area + sum over N-1 areas
EDIT: OK, got it.
Last edited on
Alrighty, my recursive part was designed in a group and some were so positive that it would work fine but the return on line 52 doesn't make any sense to me. It also gives an error. I tried simply, return area;, but obviously that wasn't going to work with the current code in the function. Any hints on how I might want to go about a recursive function? All I really understand about recursion is that I can't use loops and I'm not seeing how I can do the summing without them. Is it allowed to call a function within it self like is written below along with the adding being done within that call? Sorry if that was a confusing question, I kinda just confused myself. Thanks again. :)

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <cmath>

using namespace std;

void user_input( double &a, double &b, double &n )
{

	cout << "Enter lower bounds: ";
	cin >> a;

	cout << "Enter upper bounds: ";
	cin >> b;

	cout << "Enter number of rectangles: ";
	cin >> n;

}

double iterative( double &a, double &b, double &n )
{

	double f = 0.0, area = 0.0;
	double width = ( (b-a)/n );

	while ( a < b )
	{
	
		f = ( pow(a, 5) + 10 );
		area += ( f*width );
		a += width;
	
	}

	return area;

}

double recursive( double &a, double &b, double &n )
{

	double area = 0.0;
	double f = ( pow(a, 5) + 10 );
	double width = ( (b-a)/n );

	if ( a >= b )
		return 0;
	else
	{
	
		area = f*width;
		return (area + recursive(a+width, b, width));
	
	}


}

int main()
{

	int it_or_rec;
	double a, b, n, f, width, area;

	cout << "How would you like to integrate, iterative (1) or recursive (2): ";
	cin >> it_or_rec;
	cout << endl;

	user_input(a, b, n);

	if ( it_or_rec == 1 )
		cout << iterative( a, b, n ) << endl;
	else if ( it_or_rec == 2 )
		cout << recursive( a, b, n ) << endl;
	else
		cout << "Error, restart program and try again." << endl;

	return 0;

}
closed account (D80DSL3A)
That's actually close to workable. The error (that I got) comes from the 1st argument in the recursive call: a+width. Because you are passing by reference, which I recommend not doing, you must pass an actual variable. You can't pass a numeric value.

The problem with pass by reference is that the function called can change the value of the variable referenced. That change occurs to the variable in the calling function.

I'm trying to think of a way to prove that pass by ref. is a bad idea here, but because the rec. call comes at the end, you don't need the value of a after the call and are not affected by this.

You could keep the pass by ref., but you'll need a workaround. Changing the values of a and n before calling recursive() on line 52 will work. Your other problem is that the 3rd parameter is to be n-1, not width.

If you do this, it should work:
1
2
3
4
area = f*width;
a += width;// modify values before passing to function
n -= 1;
return (area + recursive(a, b, n));

I can show the problem with pass by ref. this way:
Suppose that instead of calling only one of the functions in main(), you call both instead:
1
2
3
4
5
6
7
cout << "Both the iterative and recursive functions will be called, to compare results\n ";
	
	user_input(a, b, n);
	
	cout << iterative( a, b, n ) << endl;
	cout << "a = " << a << '\n';// note the changed value
	cout << recursive( a, b, n ) << endl;

You'll find that recursive gives a wrong value because iterative changed the value of a.

EDIT: I'm not saying never pass by reference. I just mean that in this problem it's not appropriate. Pass by reference is indispensable when it is called for.
Last edited on
Awesome thank you so much!! It works perfectly now. And thank you for that example of the pass by reference problem. It totally makes sense. I need to work on my passing by value, and really I need to get a grip on pointers and arrays, since that would supposedly make this easier, but I finally feel like I'm starting to somewhat figure this stuff out. Thanks again. :)

Topic archived. No new replies allowed.