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

Write a program that performs a numerical integration of a set of data points fr

ID: 3810284 • Letter: W

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 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.

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
  

Explanation / Answer

I'm assuming that your program is correct. Only i need to add reading data from file. The following is your program.

#include<iostream> //Required for cin, cout
#include<cmath> //Required for exp()
#include<fstream>
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;
cout << "Enter the number of trapezoids ";
cin >> num_trapezoids;
// Estimate area under the curve of 4e^-x
ifstream if1("data.txt");


while(!if1.eof()){
if1>>a>>b;
cout<<"x,y are "<<a<<" "<<b<<endl;
area = integrate(a, b, num_trapezoids);
// Print result.
cout << "Using " << num_trapezoids
<< " trapezoids, the estimated area is "
<< area << endl;
}
if1.close();
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));
}

Output:

Enter the number of trapezoids
4
x,y are 0 26
Using 4 trapezoids, the estimated area is 13.0391
x,y are 10 28.5
Using 4 trapezoids, the estimated area is 0.000428265
x,y are 20 28.8
Using 4 trapezoids, the estimated area is 1.13276e-08
x,y are 30 29.6
Using 4 trapezoids, the estimated area is -1.84246e-13
x,y are 40 32.8
Using 4 trapezoids, the estimated area is -2.85776e-14
x,y are 50 40.1
Using 4 trapezoids, the estimated area is -2.25243e-17
x,y are 60 46.6
Using 4 trapezoids, the estimated area is -4.15372e-20
x,y are 70 42.2
Using 4 trapezoids, the estimated area is -6.55572e-18
x,y are 80 48.8
Using 4 trapezoids, the estimated area is -9.99792e-21
x,y are 90 52.6
Using 4 trapezoids, the estimated area is -2.67934e-22
x,y are 100 55.8
Using 4 trapezoids, the estimated area is -1.29055e-23
x,y are 110 60.2
Using 4 trapezoids, the estimated area is -1.78515e-25
x,y are 120 60.6
Using 4 trapezoids, the estimated area is -1.42729e-25
x,y are 130 58.2
Using 4 trapezoids, the estimated area is -1.90176e-24
x,y are 140 53.7
Using 4 trapezoids, the estimated area is -2.05763e-22
x,y are 150 50.3
Using 4 trapezoids, the estimated area is -7.12283e-21
x,y are 160 45.6
Using 4 trapezoids, the estimated area is -8.98603e-19
x,y are 170 45.2
Using 4 trapezoids, the estimated area is -1.46243e-18
x,y are 180 43.2
Using 4 trapezoids, the estimated area is -1.1845e-17
x,y are 190 38.9
Using 4 trapezoids, the estimated area is -9.64227e-16
x,y are 200 35.1
Using 4 trapezoids, the estimated area is -4.70386e-14
x,y are 210 30.8
Using 4 trapezoids, the estimated area is -3.76737e-12
x,y are 220 27.2
Using 4 trapezoids, the estimated area is -1.48343e-10
x,y are 230 21
Using 4 trapezoids, the estimated area is -7.92378e-08
x,y are 240 22.2
Using 4 trapezoids, the estimated area is -2.48708e-08
x,y are 250 18.6
Using 4 trapezoids, the estimated area is -9.67066e-07
x,y are 250 18.6
Using 4 trapezoids, the estimated area is -9.67066e-07

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