Help with Integration function

Pages: 123
I need to write a program to use the area of rectangles and/or trapezoids to find the area under a curve. I will take the integral of a function to find the area under a curve.

These are the variables I will use:
n = number of rectangles and/or trapezoids
a = beginning x value
b = ending x value h = width of each rectangle/trapezoid, (b-a)/n
f(x) = height of rectangle
(f(x1)+f(x2))/2 = height of trapezoid
Area = width*height

Integration Program Definition: the program should determine the area under these different functions,
f1(x) = 5x4 + 3x3 – 10x + 2
f2(x) = x2 – 10
f3(x) = 40x + 5
f4(x) = x3
f5(x) = 20x2 + 10x – 2

The area calculated will be determined by the user's choice of function and method to use to calculate the integral, and it is possible for the user to choose to see the area for one function calculated by both methods. For example:

Choose a function (1, 2, 3, 4, 5, other(quit)): 1
Would you like to calculate the area using the rectangle, trapezoid, or both (1, 2, 3): 2
How many trapezoids do you want? 1000
Please select a starting point, a = 1
Please select an ending point, b = 2
The area under 5x4 + 3x3 – 10x + 2 between 1 and 2 is 29.25

The program needs to adhere to the following guidelines:

 If the user chooses to see the area calculated by both methods, each method should receive their own number of rectangles or trapezoids as input and return the value from the calculation.

 Your program should continue running until the user no longer wants to calculate the area under a curve.

 You should use procedural decomposition and have functions for f1(x), f2(x), f3(x), f4(x), and f5(x), as well as functions for calculating the area using the rectangle vs. trapezoid method.

Program Input:
- Starting and ending points for the area
- Function to calculate the area, i.e. f1, f2, f3, f4, f5
- Function/Procedure(s) for calculating the area, i.e. rect, trap, both
- Number of rectangles and/or trapezoids to use

Program Output:
- The function being evaluated
- Starting and ending points for the integral
- Number of rectangles and/or trapezoids used
- The area calculated by the method(s)

I tried to put as much info as I could, I really want to get this working.
Here is what I have unfortunately:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include<iostream>
 #include<cmath>
 #include<cstdlib>

 using std::cout;
 using std::cin;
 using std::endl;

 int main () {

    int n;
    int a;
    int b;
    int h;
    int width;
    int height;
    int Area = width*height;






Please help, thank you.
The area under the curve is divided into a series of vertical strips - see diagram: http://oi49.tinypic.com/2rm4bdf.jpg
This previous thread covers the same topic:
http://www.cplusplus.com/forum/beginner/83157/
The previous thread helps a little but I am still trying to put in multiple functions into this program and having the user pick one. I just don't know how ot put it all together.
Well, the way I see this is as two or three separate, but connected problems. One is how to display the appropriate items and get the input from the user. Secondly depending on the selections made by the user, go on to perform the number-crunching calculations, and output the result. Choosing from one of several different mathematical functions is another sub-task.

None of these tasks in itself is very difficult, the important thing is to not be overwhelmed by considering the entire project in one go. Just focus on one section at a time, and tackle it stage by stage.
Thank you for the advice! I am doing it one step at a time now, I was just hoping to see some kind of example of what this would look like or how I would put in all the equations as options for the user to pick from.
how I would put in all the equations as options for the user to pick from.

Again, this can be broken down into several sub-tasks.

Putting in all the equations is relatively simple:

1
2
3
4
5
6
7
8
9
10
11
double f1(double x)
{
    return 5.0*x*x*x*x + 3.0*x*x*x - 10.0*x + 2.0;
}

double f2(double x)
{
    return x*x - 10.0;
}

// etc. .... 


Putting them as options for the user to pick from could be like this:
1
2
3
    cout << "Choose a function (1, 2, 3, 4, 5, other(quit)): " << endl;
    char choice;
    cin >> choice;


Then at a later stage, during the calculation of the area itself, you could pass the "choice" value from the user as a parameter to another function which will calculate the area. Use that value with either a switch-case or series of if-else statements in order to choose which equation will be calculated.

As an alternative to the if-else at the calculation stage, you could perhaps use a function pointer, where the value is set just once before the calculation starts:
1
2
    double (* func) ( double x);
    func = f1; // set according to user choice 


then func() can be used to call whichever function was chosen.

If that's the method you decide to use, look at the topic "Pointers to functions" in the tutorial page http://www.cplusplus.com/doc/tutorial/pointers/
Last edited on
okay so I have gotten this 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
70
71
72
#include<iostream>
  #include<cmath>
 
  using std::cin;
  using std::cout;
  using std::endl;
 
 
  double func_1(double);
  double func_2(double);
  double func_3(double);
  double func_4(double);
  double func_5(double);
 
 
  int main () {
 
     int choice;
     double n;
     double a;
     double b;
 
 
     cout << "Choose a function (1, 2, 3, 4, 5, other(quit)) " << endl;
     cin >> choice;
 
     if (choice == 1)
     {
        cout <<"How many trapezoids do you want? " << endl;
        cin >> n;
 
        cout <<"Please select a starting point, a: " << endl;
        cin >> a;
 
        cout <<"Please select an ending point, b: " << endl;
        cin >> b;
 
        cout <<"The area under " << "5x^4+3x^3-10x+2" << "between a and b is: " << func_1 << endl;

     return 0;
     }
     }
 
 
 
 
  double func_1 (double x)
  {
     cout << 5.0*x*x*x*x + 3.0*x*x*x - 10.0*x + 2.0;
  }
 
  double func_2 (double x)
  {
     cout << x*x - 10.0;
  }
 
  double func_3 (double x)
  {
     cout << 40.0*x + 5;
  }
 
  double func_4 (double x)
  {
     cout << x*x*x;
  }
 
  double func_5 (double x)
  {
     cout << 20.0*x*x + 10.0*x - 2.0;
  }

 


This is what I have so far and this is my assignment:

Write a program to use the area of rectangles and/or trapezoids to find the area under a curve. We take the integral of a function to find the area under a curve.

variables used:
n = number of rectangles and/or trapezoids
a = beginning x value
b = ending x value
h = width of each rectangle/trapezoid, (b-a)/n
f(x) = height of rectangle
(f(x1)+f(x2))/2 = height of trapezoid
Area = width*height

Integration Program Definition: Your program should determine the area under these different functions,
f1(x) = 5x4 + 3x3 – 10x + 2
f2(x) = x2 – 10
f3(x) = 40x + 5
f4(x) = x3
f5(x) = 20x2 + 10x – 2
The functions are bounded by any interval on the x-axis, including both positive and negative values!!!. The area calculated will be determined by the user's choice of function and method to use to calculate the integral, and it is possible for the user to choose to see the area for one function calculated by both methods. For example:

Choose a function (1, 2, 3, 4, 5, other(quit)): 1
Would you like to calculate the area using the rectangle, trapezoid, or both (1, 2, 3): 2
How many trapezoids do you want? 1000
Please select a starting point, a = 1
Please select an ending point, b = 2
The area under 5x4 + 3x3 – 10x + 2 between 1 and 2 is 29.25.

Program Description in more detail:
Your program needs to adhere to the following guidelines:
 If the user chooses to see the area calculated by both methods, each method should receive their own number of rectangles or trapezoids as input and return the value from the calculation.
 Your program should continue running until the user no longer wants to calculate the area under a curve.
 You should use procedural decomposition and have functions for f1(x), f2(x), f3(x), f4(x), and f5(x), as well as functions for calculating the area using the rectangle vs. trapezoid method.

Program Input:
- Starting and ending points for the area
- Function to calculate the area, i.e. f1, f2, f3, f4, f5
- Function/Procedure(s) for calculating the area, i.e. rect, trap, both
- Number of rectangles and/or trapezoids to use

Program Output:
- The function being evaluated
- Starting and ending points for the integral
- Number of rectangles and/or trapezoids used
- The area calculated by the method(s)

Cant figure this out. Please help.
Let's move this on a bit.
Now all you have to do is fill in the actual calculation of the integral, in these two functions
1
2
double rect_integral(double a, double b, int n, int choice);
double trap_integral(double a, double b, int n, int choice);


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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <cmath>

using std::cin;
using std::cout;
using std::endl;

double func_1(double);
double func_2(double);
double func_3(double);
double func_4(double);
double func_5(double);
double rect_integral(double a, double b, int n, int choice);
double trap_integral(double a, double b, int n, int choice);

char * funcname[6] = {
    "",
    "5x^4 + 3x^3 - 10x + 2",
    "x^2 - 10",
    "40x + 5",
    "x^3",
    "20x^2 + 10x - 2" };

int main () {

    int choice;
    int n;
    double a;
    double b;
    bool trap = false;
    bool rect = false;

    cout << "Functions available: \n\n";
    for (int i=1; i<6; i++)
        cout << "  " << i << "  " << funcname[i] << '\n';
    cout << endl;

    cout << "Choose a function (1, 2, 3, 4, 5, other(quit)) " << endl;
    cin >> choice;

    if (choice >= 1 && choice <= 5)
    {
        char method = ' ';
        do {
            cout << "Would you like to calculate the area using the rectangle,"
                 << " trapezoid, or both (1, 2, 3): " << endl;
            cin >> method;
        } while (method < '1' || method > '3');

        switch (method) {
            case '1':
                rect = true;
                cout <<"How many rectangles do you want? " << endl;
                break;
            case '2':
                trap = true;
                cout <<"How many trapezoids do you want? " << endl;
                break;
            case '3':
                trap = true;
                rect = true;
                cout <<"How many rectangles/trapezoids do you want? " << endl;
        }

        cin >> n;

        cout <<"Please select a starting point, a: " << endl;
        cin >> a;

        cout <<"Please select an ending point, b: " << endl;
        cin >> b;

        if (rect) {
            cout << "The area under " << funcname[choice]
                 << " between " << a << " and " <<  b
                 << " is: " <<  rect_integral(a, b, n, choice) << endl;
        }

        if (trap) {
            cout << "The area under " << funcname[choice]
                 << " between " << a << " and " <<  b
                 << " is: " <<  trap_integral(a, b, n, choice) << endl;
        }
    }

    return 0;
}

double func_1 (double x)
{
    return 5.0*x*x*x*x + 3.0*x*x*x - 10.0*x + 2.0;
}

double func_2 (double x)
{
    return x*x - 10.0;
}

double func_3 (double x)
{
    return 40.0*x + 5;
}

double func_4 (double x)
{
    return x*x*x;
}

double func_5 (double x)
{
    return 20.0*x*x + 10.0*x - 2.0;
}

double rect_integral(double a, double b, int n, int choice)
{
    // here insert the code to calculate the integral
    return 1.0; // dummy value
}

double trap_integral(double a, double b, int n, int choice)
{
    // here insert the code to calculate the integral
    return 2.0; // dummy value
}



Edit: I may have mis-interpreted the specification here: "If the user chooses to see the area calculated by both methods, each method should receive their own number of rectangles or trapezoids as input and return the value from the calculation."

- Maybe two different values of n are required.

Edit 2: Also I overlooked this: "Your program should continue running until the user no longer wants to calculate the area under a curve."
Last edited on
Thank you this makes sense! but I keep getting these errors

integrationAUC.cpp:31: warning: deprecated conversion from string constant to âchar*â
integrationAUC.cpp:31: warning: deprecated conversion from string constant to âchar*â
integrationAUC.cpp:31: warning: deprecated conversion from string constant to âchar*â
integrationAUC.cpp:31: warning: deprecated conversion from string constant to âchar*â
integrationAUC.cpp:31: warning: deprecated conversion from string constant to âchar*â
integrationAUC.cpp:31: warning: deprecated conversion from string constant to âchar*â
integrationAUC.cpp:32: error: expected declaration before â}â token

and I do not know how to fix them. What is wrong in that chunk of cod for char * funcname[6]?
Sorry about that. The character strings should be declared as constant, like this:
1
2
const char * funcname[6] = {
  ... etc
Last edited on
oh okay, fixed those errors. thank you.

In the same chunk I am now getting this one last error:

integrationAUC.cpp: In function âint main()â:
integrationAUC.cpp:52: error: invalid conversion from âconst char*â to âcharâ

I have tried a couple of changes but sometimes it just makes it worse. do you know how to fix this?
Your line numbering may be different to mine. What is line 52?
line 52: char method = " " ;
That resembles line 43 in my code. But my version reads like this:
 
        char method = ' ';

Notice the single quotes. That is a character.
Double quotes are for a string (multiple characters).

Note, the options available are '1', '2' or '3'. It is simpler to use a char than an int for this purpose, as it handles non-numeric input with less trouble.
Last edited on
okay so I changed that and now my code is:

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <cmath>

using std::cin;
using std::cout;
using std::endl;

double func_1(double);
double func_2(double);
double func_3(double);
double func_4(double);
double func_5(double);
double rect_integral(double a, double b, int n, int choice);
double trap_integral(double a, double b, int n, int choice);

const char * funcname[6] = {
    "",
    "5x^4 + 3x^3 - 10x + 2",
    "x^2 - 10",
    "40x + 5",
    "x^3",
    "20x^2 + 10x - 2" };

int main () {

    int choice;
    int n;
    double a;
    double b;
    bool trap = false;
    bool rect = false;

    cout << "Functions available: \n\n";
    for (int i=1; i<6; i++)
        cout << "  " << i << "  " << funcname[i] << '\n';
    cout << endl;

    cout << "Choose a function (1, 2, 3, 4, 5, other(quit)) " << endl;
    cin >> choice;

    if (choice >= 1 && choice <= 5)
    {
        char method = ' ';
        do {
            cout << "Would you like to calculate the area using the rectangle,"
                 << " trapezoid, or both (1, 2, 3): " << endl;
            cin >> method;
        } while (method < '1' || method > '3');

        switch (method) {
            case '1':
                rect = true;
                cout <<"How many rectangles do you want? " << endl;
                break;
            case '2':
                trap = true;
                cout <<"How many trapezoids do you want? " << endl;
                break;
            case '3':
                trap = true;
                rect = true;
                cout <<"How many rectangles/trapezoids do you want? " << endl;
        }

        cin >> n;

        cout <<"Please select a starting point, a: " << endl;
        cin >> a;

        cout <<"Please select an ending point, b: " << endl;
        cin >> b;

        if (rect) {
            cout << "The area under " << funcname[choice]
                 << " between " << a << " and " <<  b
                 << " is: " <<  rect_integral(a, b, n, choice) << endl;
        }

        if (trap) {
            cout << "The area under " << funcname[choice]
                 << " between " << a << " and " <<  b
                 << " is: " <<  trap_integral(a, b, n, choice) << endl;
        }
    }

    return 0;
}

double func_1 (double x)
{
    return 5.0*x*x*x*x + 3.0*x*x*x - 10.0*x + 2.0;
}

double func_2 (double x)
{
    return x*x - 10.0;
}

double func_3 (double x)
{
    return 40.0*x + 5;
}

double func_4 (double x)
{
    return x*x*x;
}

double func_5 (double x)
{
    return 20.0*x*x + 10.0*x - 2.0;
}

//double rect_integral(double a, double b, int n, int choice)
{
    // here insert the code to calculate the integral
    return 1.0; // dummy value
}

double trap_integral(double a, double b, int n, int choice)
{
    // here insert the code to calculate the integral
    return 2.0; // dummy value
}


and I get this:
/tmp/ccsLyEYd.o: In function `main':
integrationAUC.cpp:(.text+0x23f): undefined reference to `rect_integral(double, double, int, int)'
integrationAUC.cpp:(.text+0x2fd): undefined reference to `trap_integral(double, double, int, int)'
collect2: ld returned 1 exit status

whats happening?
Figured it out!!! I got it to compile so ignore my last post. sorry.
Here is another place to look for an answer.

http://www.cplusplus.com/forum/beginner/91810/
That does help also. The only problem I am having right now is creating the code to calculate the integral.
creating the code to calculate the integral


That's the part I show in that post.
so when I have:

1
2
3
4
double rect_integral(double a, double b, int n, int choice)
{
       // here insert the code to calculate the integral
       return = 0; // dummy value 


what will my calculation look like with a,b,n, and choice.

Can you look at my code to see what my variables are and then tell me how I would put them in a calculation. I keep messing up somehow.
Pages: 123