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) .
/*-----------------------------------------------------------------*/
/* 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()
usingnamespace 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));
}
/*-----------------------------------------------------------------*/
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;.
/*-----------------------------------------------------------------*/
/* Program chapter6_12 */
/* */
/* This program estimates the area under a given curve */
/* using trapezoids with equal bases. */
#include<iostream> // cin, cout
#include<cmath> // exp()
usingnamespace 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
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.
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace 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;
}
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