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

Create a function that implements f ( x ) as defined above. Function for Midpoin

ID: 3566988 • Letter: C

Question

Create a function that implements f(x) as defined above.

Function for Midpoint Quadrature

Create a function for your assigned quadrature technique (Midpoint) to approximate the area under f(x).

Your function must have the following as parameters:

The method should, of course, compute the approximation of the area under f(x) between the given bounds and return it (and thus should produce no screen output).

When the function is operating in file output mode, it should also output the area of each subinterval used for approximation to a file called abc123-quadrature-output.txt, where abc123 is replaced with your username. Each interval should be given on its own line.

As this function returns a value, it should produce no screen output. The file output certainly should be documented in the postcondition.

If you opt to use other methods to assist with the implementation of this method, feel free. Place them directly above this method and, of course, document them appropriately.

Main Program

Create your main program to test drive your quadrature technique for f(x). Call your function multiple times and demonstrate the file output mode as well. In your testing, include tests of the same interval with different numbers of subintervals.

Sample Screen Outputs

Warning: I made the results up for the first and the results that are given for the second are theoretically correct values! This is only an example of what the output ought to look like.

Results of approximation of f(x) on [0.25, 2.25]:
Using 4 subintervals: 1.0011
Using 8 subintervals: 1.01
Using 20 subintervals: 1.02
Using 50 subintervals: 1.0332
Using 100 subintervals: 1.221

Sample File Output

Warning: I made these results up! This is only an example of what the output ought to look like and it's an example for some made-up function on the interval [0,1] with four subintervals.

2.8
3.8223
1.002
0.933

Explanation / Answer

#include<iostream>
#include<fstream>
using namespace std;
double compute_function(double x)
{
//this is a function to calculate the value of f(x)
//as f(x) is not specified, i am taking it as f(x)=x^3-2*x+6
//you can change the f(x) as you like
return (x*x*x-2*x+6);
}
void calculate_area(double start_point, double end_point,int num_interval,bool is_file)
{
//start_point is the starting point of the interval
//end_point is the ending point of interval
//num_interval is the number of intervals
//is_file is a flag for whether to write to a file or output to the screen
double a,b,h,f,m;
int i;
double area=0.0;
h=(end_point-start_point)/num_interval;

if(is_file)
{
ofstream my_file("result.txt"); //give any name you want
a=start_point;
for(i=0;i<num_interval;i++)
{
b=a+h;
m=(a+b)/2;
f=compute_function(m);
area=area+f;
my_file<<" area in the subinterval from "<<a<<" to "<<b<<" is "<<f;
a=b;

}
my_file<<" total area is "<<area*h;


}
else
{
a=start_point;
for(i=0;i<num_interval;i++)
{
b=a+h;
m=(a+b)/2;
f=compute_function(m);
area=area+f;
a=b;


}
area=area*h;
cout<<" total area for "<<num_interval<<" of intervals is "<<area<<endl;
}
}
int main()
{
double start_point,end_point;
int num_interval;
bool is_file;
cout<<" enter starting point : ";
cin>>start_point;
cout<<" enter end point : ";
cin>>end_point;
cout<<" enter number of intervals : ";
cin>>num_interval;
cout<<" enter 1 for to be written to a file, 0 for printing to the screen ";
cin>>is_file;
calculate_area(start_point,end_point,num_interval,is_file);
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