Program Computing Area Under a Curve

Hello, I am trying to create a program that computes the area under a specific function and am stuck.

The user inputs the starting point, ending point, and width of each interval, but I cannot seem to figure out how to compute the while loop for the computation of the area. Any help on this matter is greatly appreciated.

The function is f(x)= 5x^3 + 7x^2 - 3x + 4

Here is my code so far:

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
//This program calculates the area under the curve of f(x)= 5x3 + 7x2 – 3x + 4 for a <= x <= b where a and b are greater than or equal to 0.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
//Variables and Constants:
    double a, b, x, y, w;

//Prompt the user to enter values for a and b.
    cout<<"What is the value of the starting point? ";
    cin>> a;
    cout<<"\n What is the value of the end point? ";
    cin>> b;

//Show what x is.
    x=(a+w);

//This is the equation that the program computes the area under given the variables input by the user.
    y = 5*(pow(x,3))+7*(pow(x,2))-3*x+4;

//Check to make sure both the values of a and b are above zero, as well as making sure that a <= x <= b.
    if (a<0)
{
    cout<< "The starting point must be greater than 0.\n";
}
    else if (b<0)
{
    cout<< "b must be greater than 0.\n";
}
    else if (b<a)
{
    cout<< "b must be greater than a.\n";
}
    else
{
    cout<< "\n what is the width of each partitioned segment? ";
    cin>> w;


//Make sure that the width is less than the distance between the end point and starting point.
        if (w>(b-a))
    {
        cout<< "The width must be less than the size of the interval between the starting point and ending point.\n";
    }
        else if (w=(0))
    {
        cout<< "Your width cannot be 0. \n";
    }
        else (while((a+w)<b))
    {
        //HERE IS WHERE I THINK I NEED TO PUT THE CODE FOR THE ACTUAL COMPUTING OF THE AREA BUT IM NOT SURE HOW TO START.
    }
}

    return 0;
}




Thanks for any help regarding the matter.
Ok, I have done a bit more work and have come up with this, however i do not get a display of the area... and I am not sure if my while loop is set up correctly.

The x value needs to increase by +w until it reaches the value b input by the user.

The area value also needs to be an addition of every partition.

This code is an extension of the above code starting after

cin>>w;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
        if (w>(b-a))
    {
        cout<< "The width must be less than the size of the interval between the starting point and ending point.\n";
    }
        else if (w=0)
    {
        cout<< "Your width cannot be 0. \n";
    }
        else

        while(x<b)
    {
        x = (a += w);
        area = y*=w;

        cout<< "The area under the curve is" <<area<< "\n";
    }
}
    return 0;
}


Thanks for any help regarding this matter.
There are some things that are wrong.
In the code above
1
2
// Here you are setting the value of w to zero
else if (w=0)

Should be
1
2
// Here you are checking the value of w to see if it is equal to zero
else if (w==0)

The next thing is you need to compute the y value for each new value of x. I would do something like
1
2
3
4
// This function returns the value of the equation:  f(x)= 5x^3 + 7x^2 – 3x + 4
double func(double x) {
   return (5*(pow(x,3))+7*(pow(x,2))-3*x+4);
}

Then down in the while loop do something like
1
2
3
4
5
6
7
8
9
// While x is still between the values of a - b
        // Calculate the value of y by calling func(x)
        y = func(x);
        // Compute the area by taking the length*height
        area += 
        // Move the value of x by w
        x += 
// end of while
cout << "Area:  " << area << endl;

Fill in the blanks, leaving some for you to do.

Last edited on
Thanks for your help histrungalot, I have edited my code and tried a few different variations but am getting some interesting errors.

Here is my new while loop:

1
2
3
4
5
6
7
8
        while (a<x, x<b)
    {
        y = func(x);

        area += (w*y);

        x += w;
      }


And the error i am getting is that the left hand operand of the comma in while(a<x, x<b) has no effect.


The other error I am getting is with this section of code:

1
2
3
4
5
6
7
    double a, b;
    double y, w, x, func;
    double area = 0.0;

    double func(double x) {
    return (5*(pow(x,3))+7*(pow(x,2))-3*x+4); 
}


The error i am getting is that a function-definition is not allowed here before '{' token.

I have tried splitting up the double func and double x, and not defining them in the scope, but am still having issues.

If i leave them out of the scope it tells me that i need to add them in even though put in exactly what you wrote in line 2,3,4 of the function that returns the value of the equation.

then this section as well:

y = func(x);

The error i am getting is that func cannot be used as a function.

I have tried naming the function differently throughout the program, but this yielded no different results.
 
while (a<x, x<b)

Is not doing what you think. When you want to check if you are between two values you have to check that you are both greater some value and less than the other value
The while loop check should look like:
1
2
3
4
while(x >= a && x < b ) {
     // Your code
     // ....
}

Not see your current code, what I think is that you are putting the function definition in the wrong spot. It can not be defined inside the main. Put it at the top of the file after the #include:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cmath>

using namespace std;

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

int main()
{
    // Your code
    // ...
    double x(4);
    double y = func(x);
    cout << "f(x) = " << y << endl;
}

Output:
f(x) = 424
Last edited on
Thanks again for your help.

The code runs again with no errors, but I am not getting the correct answer. I think I have something wrong with my area function, but I cannot seem to figure out what it is.

Here is my code so far...

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
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cmath>

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()
{
//Variables and Constants:
    double a, b;
    double w, x, y=func(x);
    double area = 0.0;

//Prompt the user to enter values for a and b.
    cout<<"What is the value of the starting point? ";
    cin>> a;
    cout<<"\n What is the value of the end point? ";
    cin>> b;

//Check to make sure both the values of a and b are above zero, as well as making sure that a <= x <= b.
    if (a<0)
{
    cout<< "The starting point must be greater than 0.\n";
}
    else if (b<0)
{
    cout<< "b must be greater than 0.\n";
}
    else if (b<a)
{
    cout<< "b must be greater than a.\n";
}
    else
{
    cout<< "\n what is the width of each partitioned segment? ";
    cin>> w;


//Make sure that the width is less than the distance between the end point and starting point.
        if (w>(b-a))
    {
        cout<< "The width must be less than the size of the interval between the starting point and ending point.\n";
    }
        else if (w==0)
    {
        cout<< "Your width cannot be 0. \n";
    }
        else
    {

        while(x >= a && x < b ) {

        y = func(x);

        x += w;

        area += (w*y);

                                }

    cout<< "The area under the curve is: " <<area<< endl;
    }
}

    return 0;
}



I put in a= 2 / starting point = 2

b = 2 / ending point = 6

and then width = .01

and I am getting 0 for my area.

If i output y instead of the area i just get 4.

i believe the answer should be something like 2053.


Also for curiosities sake, why can I not include the function in the main program?


EDIT:

I tried using:

double area;

instead of using:

double area = 0.0;

and I got that the area is equal to 5.1x10^303

I thought I should set area = 0.0 for the purpose of using an accumulator or a running total... but it seems that I don't have the understanding necessary to utilize such a process yet.
Last edited on
You just need to set the value of x before you go into the while loop
1
2
3
4
5
6
        x = a;
        while(x >= a && x < b ) {
             y = func(x);
             x += w;
             area += (w*y);
       }

Output for a=2, b=6 and w=0.1
The area under the curve is: 2122.98

Output for a=2, b=6 and w=0.000001
The area under the curve is: 2053.33

Think of main as just another function (a special one). You can not define functions inside functions.
http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
Got it.

Thanks for all of your help histrungalot.

Topic archived. No new replies allowed.