write a c++ You need to write a program to calculate the area under a curve f(x)
ID: 3637861 • Letter: W
Question
write a c++You need to write a program to calculate the area under a curve f(x) = 5x3 + 7x2 – 3x + 4 for a <= x <= b where a and b are greater than or equal to 0.
You will be given a file containing values of a and b. For each pair of values:
Check whether a and b are larger than 0, and whether a is smaller than or equal to b. If NOT, error message must be displayed and your program should go on to the next set of input values. If YES, your program approximates the area under the curve between a and b by partitioning the area into small segments where each segment has width w.
For each area calculation, your program should calculate areas for w, w/2, w/4, … using a function findarea(a,b,w) which takes as input a,b,w and returns the area. Your initial value of w should be (b – a) / 10.
Your program should keep calculating more accurate areas until the difference between two of your calculations is less than .0001. Then the program should output the last area value calculated to a file myoutput.txt.
Example data file myinput.txt:
2 3
-1 4
2 1
3.5 7
Example output.txt:
Areas under a curve defined by f(x) = 5x^3 + 7x^2 – 3x + 4:
For a = 2 and b = 3, the calculated area is [value your program calculated]
For a = -1 and b = 4, a is less than zero, calculation not done
For a = 2 and b = 1, b is less than or equal to a, calculation not done
For a = 3.5 and b = 7, the calculated area is [value your program calculated]
Explanation / Answer
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
int main()
{
//PROTOTYPES
double findArea(double a, double b, double w);
//LOCAL DECLARATIONS
double a = 0.0;
double b = 0.0;
double w = 0.0;
const double PRECISION = .0001;
double temp = 0.0;
int count = 0;
fstream fin("myinput.txt", ios::in);
if (!fin)
{
cout << "Cannot open data file. Program aborting.";
exit(1);
}
fstream fout("output.txt", ios::out);
if (!fout)
{
cout << "Cannot open output file. Program aborting.";
exit(1);
}
//PROCEDURES
fout << "Areas under a curve defined by f(x) = 5x^3 + 7x^2 – 3x + 4:" << endl;
cout << "Areas under a curve defined by f(x) = 5x^3 + 7x^2 - 3x + 4:" << endl;
while (fin >> temp)
{
if (count % 2)
{
b = temp;
fout << "For a = " << a
<< " and b = " << b << ", ";
cout << "For a = " << a
<< " and b = " << b << ", ";
if (a < 0)
{
fout << "a is less than zero, calculation not done. ";
cout << "a is less than zero, calculation not done. ";
}
else if (b < 0)
{
fout << "b is less than zero, calculation not done. ";
cout << "b is less than zero, calculation not done. ";
}
else if (a >= b)
{
fout << "b is less than or equal to a, calculation not done. ";
cout << "b is less than or equal to a, calculation not done. ";
}
else
{
w = (b - a) / 10.0;
while (fabs(findArea(a, b, w) - findArea(a, b, w / 2.0)) > PRECISION)
{
w /= 2.0;
}
fout << "the calculated area is "
<< findArea(a, b, w)
<< endl;
cout << "the calculated area is "
<< findArea(a, b, w)
<< endl;
}
}
else
{
a = temp;
}
count++;
}
fin.close();
fout.close();
cin.get();
return 0;
}
//---------------------------------------------------------
// FUNCTION DEFINITIONS
//---------------------------------------------------------
//f(x) = 5x^3 + 7x^2 – 3x + 4
double f(double x)
{
return 5 * x * x * x + 7 * x * x - 3 * x + 4.0;
}
//---------------------------------------------------------
// This function use trapezoidal rule to calculate the approximate area.
//See wikipedia.org/wiki/Trapezium_rule for more info about the rule
// this function should be findArea(double a, double b, int n)
//because we cannot compare 2 doubles when check condition x < b
double findArea(double a, double b, double w)
{
// (b-a)/w if w = (b-a)/n may result n-e or n+e,
//(e: very small error ~0.0000000000001 or so)
//if n+e it is ok to cast to int,
//but n-e will result n-1 when cast to int
//so we need to add 0.5 in both case
// check if w == 0
int n = (w ? (b - a) / w + .5 : 0);
double accumulator = f(b) - f(a);
double x = a;
for (int i = 0; i < n; i++, x+= w)
{
accumulator += 2.0 * f(x);
}
return accumulator * w / 2.0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.