Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Description: The area under a curve may be approximated by dividing the

ID: 3565313 • Letter: P

Question

Problem Description:

The area under a curve may be approximated by dividing the area into trapezoids, calculating the area of each trapezoid, and then summing those smaller areas. For example, using the diagram below,

the area under the curve for the region from a to b could be approximated by dividing the region into 5 smaller areas. The first area can be defined by a, f(a), x1, and f(x1). The area of a trapezoid is given by the equation area = 0.5 base (height1+ height2). In our example of the first trapezoid the base is equal to x1 a, height1is equal to f(a), and height2is equal to f(x1). Therefore the area of the first trapezoid is equal to 0.5(x1 a)(f(a) + f(x1)). Similarly, the area of the second trapezoid can be calculated as 0.5(x2-x1)(f(x1) + f(x2)) and the area for the third trapezoid is 0.5(x3 x2)(f(x3) + f(x2)). The total area under the curve from a to b would be the sum of the 5 trapezoids.

For this assignment you will find x, y data in a file called xydata.dat.

xydata.dat:

x values   y values
20.00       0
20.02       15
20.04       27
20.06       39
20.08       54
20.10       65
20.12       75
20.14       84
20.16       93
20.18       101
20.20       108
20.22       113
20.24       116
20.26       115
20.28       112
20.30       107
20.32       100
20.34       92
20.36       83
20.38       74
20.40       64
20.42       53
20.44       39
20.46       27
20.48       15
20.50       0

Consecutive pairs of x-y data should be used to designate the trapezoids, e.g. the first trapezoid would be defined by x1, y1, x2, and y2; the second trapezoid would be defined as x2, y2, x3, and y3; the third trapezoid by x3, y3, x4, and y4, etc.

So I have to write a program that finds the area under the curve depicted by the x/y coordinates in the given data file, and it must be written without arrays.

I understand what I am supposed to do but I am having some trouble, especially with retrieving the correct values from the file for use in my calculations.

I have been trying to figure this out for many long hours without success, so if anyone could show me how they would go about writing this program it would be greatly appreciated. Thanks.

Problem Description: The area under a curve may be approximated by dividing the area into trapezoids, calculating the area of each trapezoid, and then summing those smaller areas. For example, using the diagram below, the area under the curve for the region from a to b could be approximated by dividing the region into 5 smaller areas. The first area can be defined by a, f(a), x1, and f(x1). The area of a trapezoid is given by the equation area = 0.5 base (height1+ height2). In our example of the first trapezoid the base is equal to x1½ a, height1is equal to f(a), and height2is equal to f(x1). Therefore the area of the first trapezoid is equal to 0.5(x1½ a)(f(a) + f(x1)). Similarly, the area of the second trapezoid can be calculated as 0.5(x2-x1)(f(x1) + f(x2)) and the area for the third trapezoid is 0.5(x3½ x2)(f(x3) + f(x2)). The total area under the curve from a to b would be the sum of the 5 trapezoids. For this assignment you will find x, y data in a file called xydata.dat. Consecutive pairs of x-y data should be used to designate the trapezoids, e.g. the first trapezoid would be defined by x1, y1, x2, and y2; the second trapezoid would be defined as x2, y2, x3, and y3; the third trapezoid by x3, y3, x4, and y4, etc. So I have to write a program that finds the area under the curve depicted by the x/y coordinates in the given data file, and it must be written without arrays. I understand what I am supposed to do but I am having some trouble, especially with retrieving the correct values from the file for use in my calculations. I have been trying to figure this out for many long hours without success, so if anyone could show me how they would go about writing this program it would be greatly appreciated. Thanks.

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string.h>

using namespace std;

int main(){
ifstream infile;
infile.open("xydata.dat");

double x1,x2,y1,y2;

infile >> x1;
infile >> y1;

double totalArea = 0;

while(!infile.eof()){
infile >> x2;
infile >> y2;

double area = ((x2 - x1)*(y2 + y1))/2;
//cout << area << endl;
totalArea += area;

x1 = x2;
y1 = y2;
}

cout << "The Area under the curve depicted by the x/y coordinates in the ";
cout << "given data file is " << totalArea << endl;

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote