Help to find the area under a curve

I need to write a code to find the area under this curve:
f(x) = 5x^3 + 7x^2 – 3x + 4
and I've been trying to look at examples online to understand how but I'm extremely lost. The program should calculate areas for w, w/2, w/4, …, starting with w=(b-a)/10, and should keep calculating more accurate areas until the difference between the last two calculations is less than .0001. Then the program should display the last area value calculated.

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 <stdio.h>
#include <cmath>

using namespace std;

int main(){
  double a;
  double b;
  double w;
  double area;
  cout << "What is the value of a?: ";
  cin >> a;
  cout << "What is the value of b?: ";
  cin >> b;
  if(a > 0 && b > 0 && a <= b){
    w = (b-a)/10;
    area = (f(a)+f(b))/2;
    for(int i = 1; i < w; i++){
      area += f(a+i*w);
    }
    area = area*w;
    cout << "\nResult is ";
    cout.precision(2);
    cout << area;
  }else if(a < 0 || b < 0){
    perror ("\nInvalid data, either a or b < 0\n");
  }else{
    perror("\nInvalid data, a > b");
  }
}


I really need help; I've been trying but I cannot visualize how the math should look translated into code.
Edit: I changed the code more and got this:
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
#include <iostream>
#include <cmath>
#include <stdio.h>

using namespace std;

double func(double x){
  return (5.0*(pow(x,3.0))+7.0*(pow(x,2.0))-3.0*x+4.0);
}

int main(){
  double a, b;
  double x = 0;
  double w, y=func(x);
  double area;
  
  cout << "What is the value of a?: ";
  cin >> a;
  cout << "What is the value of b?: ";
  cin >> b;
  if(a > 0 && b > 0 && a <= b){
    w = (b-a)/10;
    x = a;
    while(x >= a && x < b){
      y = func(x);
      x += w;
      area += (w*y);
      cout << "The area under the curve is: " << area << endl;
    }
  }else if(a < 0 || b < 0){
    perror ("\nInvalid data, either a or b < 0\n");
  }else{
    perror("\nInvalid data, a > b");
  }
  return 0;
}

Which now gives me this as an output:
What is the value of a?: 2
What is the value of b?: 7
The area under the curve is: 33
The area under the curve is: 92.1875
The area under the curve is: 188.688
The area under the curve is: 335.5
The area under the curve is: 547.5
The area under the curve is: 841.438
The area under the curve is: 1235.94
The area under the curve is: 1751.5
The area under the curve is: 2410.5
The area under the curve is: 3237.19

Which is much closer than I had before. Please, could someone help me make the last few changes to this so it works properly? According to my notes, the final answer here should be 3715.42
This assumes you understand some calculus, and the idea of a limit. Use the 'rectangle method' to sample your function at every interval w along your curve, and compute the area of the rectangle under it. The area under the curve is the sum of the areas of the rectangles.

Now, the thinner the rectangles, the better an approximation of the area under the curve.

You can see it visually here:
http://www.drcruzan.com/Images/Mathematics/DefiniteIntegrals/IntegralBoxVsNComparison.png
(From a site I just googled: http://www.drcruzan.com/DefiniteIntegrals.html)


In order to do this effectively, you should have a several functions in your code.

The curve: f(x)
1
2
3
4
double f( double x )
{
  return 5*x*x*x + 7*x*x - 3*x + 4;
}

Area of a rectangle
1
2
3
4
double area_of_rect( double width, double height )
{
  return width * height;
}

The integral of the curve in [a,b)
1
2
3
4
5
6
7
8
9
double integrate_f( double a, double b, double w )
{
  double sum = 0.0;
  // For each x in {a, a+w, a+2w, ..., b-w}
  //   sum += area of the rectangle at x
  // Hint: how high is the rectangle?
  // Hint: how wide is the rectangle?
  return sum;
}

Now you can write a function that computes the integral to a precision of 0.0001.
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
double integrate_f( double a, double b )
{
  // w is our 'precision' -- (Hint: how is this related to our rectangles?)
  double w = (b-a)/10;

  // we'll keep computing integrals, changing w each time, until the 
  // difference between these two is less than our 'epsilon' (0.0001).
  double last_area = 0.0;
  double new_area  = 0.0;

  do
  {
    // remember the previous integral
    last_area = new_area;

    // get the new integral
    new_area  = integrate_f( a, b, w );

    // update w for the next time around (if needed)
    w = ... // Hint: what do I do to w for the next round?

    // repeat if we are not done
  } while (!(abs(last_area - new_area) < .0001));
  
  return new_area;
}

w is modified as a simple geometric sequence:

    w/1, w/2, w/4, w/8, etc.

You can rewrite that as:

    wr1, wr2, wr3, wr4, etc.

What is the value of r (called the common ratio)? And how do you use r to modify w in the loop (line 20)? (Hint: it's really simple; each iteration of the loop is a power of r.)

Hope this helps.

[edit] Fixed a couple of stoopid typos...
Last edited on
Topic archived. No new replies allowed.