Making a loop and table for outputs

Hello,
I have a program here that can calculate the area of trapezoids from a given function F(x)=2ln(3x) . I have a loop here to ask for a different amount of trapezoids to find the area, but im not sure how to get it to spit out the results for EACH input of trapezoids in a table. The # of trapezoids, the area of said number of trapezoids, and the % difference. in a table. The percent difference is 100*(P-H)/H. Where P is the program values and H is the hand calculation answer (47.826723) .


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
55
56
/*-----------------------------------------------------------------*/
/* Program chapter6_12 */
/*                     */
/* This program estimates the area under a given curve */
/* using trapezoids with equal bases. */
#include<iostream> //Required for cin, cout
#include<cmath> //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,k;
  // Get input from user.
  cout << "Enter the interval endpoints, a and b\n";
  cin >> a >> b;
  cout << "Enter the number of trapezoids\n";
  cin >> num_trapezoids; //Starting with 5
  // Estimate area under the curve of 2ln(3x)
  area = integrate(a, b, num_trapezoids);
  
  for (k = 1; k <= 4; k = k + 1)
    {				//How many times we want it to ask
      cout << "Enter the number of trapezoids again x 10." << endl;
      cin >> 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(2*log(3*x));
}
/*-----------------------------------------------------------------*/
Last edited on
Sounds like you are trying something like this - so all you need do is extend it to tabulate the results and comparison percentage. You might have to adjust the line current_trapezoids = 10 * (k + 1) * num_trapezoids;.

Tabulation hint: #include <iomanip>

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
55
56
57
58
59
60
/*-----------------------------------------------------------------*/
/* Program chapter6_12 */
/*                     */
/* This program estimates the area under a given curve */
/* using trapezoids with equal bases. */

#include<iostream>  // cin, cout
#include<cmath>     // exp()

using namespace std;

double integrate(double a, double b, int n);
double f(double x);

int main()
{
    int num_trapezoids;
    double a{0}, b{0}, area{0};
    
    cout << "Enter the interval endpoints, a and b: ";
    cin >> a >> b;
    cout << "Enter the number of trapezoids: ";
    cin >> num_trapezoids;
    
    int no_times{0};
    cout << "Enter the number of trapezoids again x 10: ";
    cin >> no_times;
    
    int current_trapezoids{0};
    for(int k = 0; k < no_times; k++)
    {
        current_trapezoids = 10 * (k + 1) * num_trapezoids;
        area = integrate(a, b, current_trapezoids);
        
        cout
        << "Using " << current_trapezoids << " trapezoids, estimated area is "
        << area << endl;
    }
    
    return 0;
}

double integrate(double a, double b, int n)
{
    
    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(2 * log( 3 * x ) );
}


Enter the interval endpoints, a and b: 1 10
Enter the number of trapezoids: 5
Enter the number of trapezoids again x 10: 4
Using 50 trapezoids, estimated area is 47.8219
Using 100 trapezoids, estimated area is 47.8255
Using 150 trapezoids, estimated area is 47.8262
Using 200 trapezoids, estimated area is 47.8264
Program ended with exit code: 0


e.g.
1
2
3
4
5
6
7
8
9
10
int current_trapezoids{num_trapezoids};
    for(int k = 0; k < no_times; k++)
    {
        current_trapezoids *= 10;
        area = integrate(a, b, current_trapezoids);
        
        cout
        << "Using " << current_trapezoids << " trapezoids, estimated area is "
        << area << endl;
    }


Enter the interval endpoints, a and b: 1 10
Enter the number of trapezoids: 1
Enter the number of trapezoids again x 10: 5
Using 10 trapezoids, estimated area is 47.7083
Using 100 trapezoids, estimated area is 47.8255
Using 1000 trapezoids, estimated area is 47.8267
Using 10000 trapezoids, estimated area is 47.8267
Using 100000 trapezoids, estimated area is 47.8267
Program ended with exit code: 0
Last edited on
This is similar to what I was looking for but I was trying to get something more like:

# of Trapezoids Integration Value Percent Difference
5 47.3777 −0.93885378
50 47.8219 −0.01008432
500
5000 etc..

The interval is from 1 to 10, which I do not think I mentioned. The −0.93885378 is from 100*((47.3777-47.826723))/(47.826723). Which is the Percent difference formula from before. Where 47.3777 is the output of the program, and 47.826723 is from the hand calculation. I am confused as to how I can get it to generate these numbers and in a table format together.
Last edited on
This is similar to what I was looking for but I was trying to get something more like:
You're joking.
Okay, nevermind. Thank you for helping.
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
#include <iostream> 
#include <cmath>
#include <iomanip>
using namespace std;

double integrate( double a, double b, int n, double f( double ) );

int main()
{
   auto f = []( double x ){ return 2 * log( 3 * x ); };
   auto fintegral = []( double x ){ return 2 * x * ( log( 3 * x ) - 1 ); };
   
   double a = 1, b = 10;
   double exactI = fintegral( b ) - fintegral( a );
   
   #define SP << setw( 15 ) <<
   cout SP "intervals" SP "estimate" SP "% error" << '\n';
   for ( int n = 10; n <= 1000000; n *= 10 )
   {
      double I = integrate( a, b, n, f);
      cout SP n SP I SP 100 * ( I - exactI ) / exactI << '\n';
   }
}

double integrate( double a, double b, int n, double f( double ) )
{
   double sum = 0.5 * ( f( a ) + f( b ) );
   double dx = ( b - a ) / n;
   for ( int k = 1; k < n; k++ ) sum += f( a + k * dx );
   return sum * dx;
}


      intervals       estimate        % error
             10        47.7083      -0.247625
            100        47.8255    -0.00253966
           1000        47.8267   -2.54041e-05
          10000        47.8267   -2.54042e-07
         100000        47.8267   -2.54024e-09
        1000000        47.8267    -2.8911e-11


Quadratic convergence.
Last edited on
If'n you have a compiler capable of using C++20 there is the <format> library for creating tabular output.
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
#include <iostream> 
#include <cmath>
#include <format>
#include <string>

double integrate(double a, double b, int n, double f(double));

int main()
{
   auto f         = [ ] (double x) { return 2 * log(3 * x); };
   auto fintegral = [ ] (double x) { return 2 * x * (log(3 * x) - 1); };

   double a      { 1 };
   double b      { 10 };
   double exactI { fintegral(b) - fintegral(a) };

   std::cout << std::format("{:>10}{:>15}{:>15}\n", "intervals", "estimate", "% error");

   std::string fmt_tbl { "{:10}{:15.7}{:15.7}\n" };

   for (int n { 10 }; n <= 1000000; n *= 10)
   {
      double I { integrate(a, b, n, f) };

      std::cout << std::format(fmt_tbl, n, I, 100 * (I - exactI) / exactI);
   }
}

double integrate(double a, double b, int n, double f(double))
{
   double sum = 0.5 * (f(a) + f(b));
   double dx  = (b - a) / n;

   for (int k { 1 }; k < n; k++) sum += f(a + k * dx);

   return sum * dx;
}
 intervals       estimate        % error
        10       47.70829     -0.2476246
       100       47.82551   -0.002539661
      1000       47.82671  -2.540413e-05
     10000       47.82672  -2.540418e-07
    100000       47.82672  -2.540242e-09
   1000000       47.82672  -2.891095e-11

If'n your compiler doesn't have <format>/C++20 capability, or you don't want to use C++20 there is the fmt library:
https://github.com/fmtlib/fmt#readme

Both libraries' syntax is similar to Python's format string syntax:
https://fmt.dev/latest/syntax.html
Last edited on
Topic archived. No new replies allowed.