Write the C++ program that will compute for the area under the curve of the equation,
Input:
- The lower limit a.
- The upper limit b.
- The number of trapezoids.
Compute for the area using trapezoids:
- Declare an array whose size will be equal to the number of trapezoids.
- Use a looping structure.
- Pass arguments to a module that will compute for the area of the trapezoid and return the area back to the calling method.
- Each element in the array will be assigned the returned area for every trapezoid.
- Use an accumulator to compound the sum of each element in the array.
Compute for the area using integral:
- Pass arguments to a module that will compute for the area under the curve and return the area back to the calling method.
- Assign the returned area to a variable.
Output:
- The area of each trapezoid.
- The area using the trapezoid method.
- The area using integral calculus.
- Percentage error (absolute).
#include<iostream>
using namespace std;
float y(float x)
{
return 1/(1+x*x);
}
float trapezoidal(float a, float b, float n)
{
float h = (b-a)/n;
float p = y(a)+y(b);
for (int i = 1; i < n; i++)
p += 2*y(a+i*h);
return (h/2)*p;
}
double Area(int a1, int a2,
int h)
{
return ((a1 + a2) / 2) * h;
}
int main()
{
int base1,base2,height;
cout<<"Input the lower limit a: ";
cin>>base1;
cout<<"Input the upper limit b: ";
cin>>base2;
cout<<"Enter the number of trapezoids: ";
cin>>height;
double area = Area(base1, base2,
height);
cout << "Area of each trapezoid is: " << area;
float x0 = base1;
float xn = base2;
int n = height;
float percentage=(trapezoidal(x0,xn,n)/area)*100;
cout<<"\nArea using trapezoidal method is: "<<trapezoidal(x0, xn, n);
cout<<"\nArea using integral calculus is: "<<area;
cout<<"\nThe percentage is: "<<percentage;
return 0;
}
Comments
Leave a comment