Help please

Please help me I have to write a program that use the trapezoidal rule for integration
f(x)= x^2.

The following is my code, but I get stuck when writing for define area.


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
#include <iostream>
#include <cmath>
using namespace std;

double trapezoid(int n, double a, double b);
// This function will take three arguments: the number of intervals, the limits and return the value of the definite integral.

int main()
{
	char yesno='y';
	while(yesno=='Y' || yesno=='y')
	{
		
		double area, h, a, b;
		int n;
		
		cout<< "Enter the lower and upper limits of the interval: \n";
		cin>>a>>b;
		cout << "Enter the number of intervals: \n";
		cin>>n;
		
		cout<< "The area under the curve (intergrall) is: \n";
		cin>> trapezoid>>endl;
		
	
	cout<<"continue? (y or n) \n";
	cin>>yesno; 
		
	} 



}


double trapezoid(int n, double a, double b)
{
	double h
	
	double h = ( ( b - a ) / n );
    double y = 0;
    double sum = 0;
 
    for ( int i = 0; i <= n; i ++ )
    {
        y += h;
        sum = sum + // dont know what have to write for f(x)= x*x;
    }
    sum = sum * ( h / 2 );
 
    return sum;


}
You can simply put x * x

or if sum is the thing you are trying to square you can simply do:
1
2
3
sum *= sum;
//or
sum = sum * sum;


Also it would be impossible to input into a newline. (line 23 remove endl)
sum is not the thing I try to square.
I change x*x to x * x and take out line 23 but the program still some error that I cant run.
sum is not the thing I try to square.


So then what are you trying to square?

but the program still some error that I cant run.


It helps if you tell us what the error is.
well um you are supposed to replace x with some variable otherwise that would be like trying to do ??? * ??? What exactly are you trying to square? Also I didn't mean remove line 23 completely I meant remove the endl on line 23.
missmango wrote:

I get stuck when writing for define area.

Indeed, you are wrong when define area of an elementary trapezoid. The area is:

area = medium_lineLength * trapezoid_height

so please calculate the two values and all wil be OK.

Last edited on
As an example (you have to extract the trapezoid function!)

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

int main(){
	double a, b, h, sum = 0.;
	double x1, x2, median;
	double area;
	int n;
	std::cout << "Enter the ends of the interval [a, b] : \n";
	std::cout << "a = ";
	std::cin >> a;
	std::cout << "b = ";
	std::cin >> b;
	if(b < a) return 1;
	std::cout << "Number of subintervals of [a, b]? ";
	std::cin >> n;
	
//	The length of a subinterval
	h = (b - a) / n;
	
	for(int i = 0; i < n; ++i){
		x1 = a + i * h; // abscissa of the left end
		x2 = x1 + h;    // abscissa of the right end
		
//	For the function f(x) = x^2 we have
		median = (x1 * x1 + x2 * x2) * .5;
		
//	The area of each trapezoid
		area = median * h;

//	The area under the curve
		sum += area;
	}
	
	std::cout << "Approx. area = " << sum << '\n';
	std::cout << "Exact  area  = " << (b * b * b - a * a * a) / 3. << '\n';

	return 0;
}
Example 1
**********
Enter the ends of the interval [a, b] : 
a = 0
b = 4
Number of subintervals of [a, b]? 10
Approx. area = 21.44
Exact  area  = 21.3333

Example 2
**********
Enter the ends of the interval [a, b] : 
a = 0
b = 4
Number of subintervals of [a, b]? 100
Approx. area = 21.3344
Exact  area  = 21.3333

Example 3
**********
Enter the ends of the interval [a, b] : 
a = 0
b = 4
Number of subintervals of [a, b]? 1000
Approx. area = 21.3333
Exact  area  = 21.3333
Topic archived. No new replies allowed.