Question: Write a program that performs a numerical integration of a set of data points from a file using the trapezoidal method. The program should be

Write a program that performs a numerical integration of a set of data points from a file using the trapezoidal method. The program should be written so that it accepts a data file with x data in the first column and y data in the second column. DO NOT USE AN ARRAY FOR THIS PROBLEM, simply read the data from the file!

Your program should NOT assume that data is equally spaced in the x-coordinate, rather, it should compute the base for each trapezoid.

Use your program to solve the following physics program:

The net force along the linear path of a particle of mass 480 g has been measured at 10.0 cm intervals, starting at x = 0.0 to be 26.0, 28.5, 28.8, 29.6, 32.8, 40.1, 46.6, 42.2, 48.8, 52.6, 55.8, 60.2, 60.6, 58.2, 53.7, 50.3, 45.6, 45.2, 43.2, 38.9, 35.1, 30.8, 27.2, 21.0, 22.2, 18.6, all in Newtons. Determine the total work done on the particle over this entire range.

/*-----------------------------------------------------------------*/ /* Program chapter6_12 */ /* */ /* This program estimates the area under a given curve */ /* using trapezoids with equal bases. */ #include //Required for cin, cout #include //Required for exp() using namespace std; // Function prototypes. double integrate(double a, double b, int n); double f(double x); int main() { // Declare objects int num_trapezoids; double a, b, area; // Get input from user. cout << "Enter the interval endpoints, a and b "; cin >> a >> b; cout << "Enter the number of trapezoids "; cin >> num_trapezoids; // Estimate area under the curve of 4e^-x area = integrate(a, b, num_trapezoids); // Print result. cout << "Using " << num_trapezoids << " trapezoids, the estimated area is " << area << endl; return 0; } /*-----------------------------------------------------------------*/ /*-----------------------------------------------------------------*/ double integrate(double a, double b, int n) { // Declare objects. double sum(0), x, base, area; base = (b-a)/n; for(int k=2; k<=n; k++) { x = a + base*(k-1); sum = sum + f(x); } area = 0.5*base*(f(a) + 2*sum + f(b)); return area; } double f(double x) { return(4*exp(-x)); } /*-----------------------------------------------------------------*/ 

I think this is what the input file should look like, but I'm not sure:

x y 0 26 10 28.5 20 28.8 30 29.6 40 32.8 50 40.1 60 46.6 70 42.2 80 48.8 90 52.6 100 55.8 110 60.2 120 60.6 130 58.2 140 53.7 150 50.3 160 45.6 170 45.2 180 43.2 190 38.9 200 35.1 210 30.8 220 27.2 230 21.0 240 22.2 250 18.6

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!