How to get this example to work?

These are the variables:
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

In order to finish my integration problem I really need to see how this example works:
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

Can someone show me how to do this in code please? Once I see this I will be able to do the 4 other programs. Please help. I need to see mainly how to get the function to work with the user input.
Last edited on
Please post what you have written so far and then we can proceed from there.
Okay, this code compiles and I am just messing with things right now and it doesn't work.

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
#include<iostream>
  #include<cmath>
  #include<cstdlib>
 
  using std::cout;
  using std::cin;
  using std::endl;
 
  void check_f1(double x) {
 
     double n = x;
 
     double sum = 0;
 
          for(x=1; x<=n; x++)
          sum += (5*pow(x,4) + 3*pow(x,3) - 10*x +2);
          cout << sum << " is the area under a curve for 5x^4+3x^3-10x+2" << endl;
  }
  using std::cout;
  using std::cin;
  using std::endl;
 
  int main() {
 
 
     int choice_f;
     float a;
     float b;
     int n;
     float h;
     float x;
     float area;
 
  cout << "Choose a function (1,2,3,4,5, other(quit)):" << endl;
  cin >> choice_f;
 
 
  if (choice_f == 1)
          cout << "Please select a starting point, a: ";
          cin >> a;
 
          cout << "Please select a end point, b: ";
          cin >> b;
 
          cout << "How many trapezoids do you want? ";
          cin >> n;
 
          cout << check_f1;
 
 return 0;
 }


The whole assignment detail I am doing is in another post of mine.
If anyone can help me get this program running the way it should please let me know.
Ok. I have the first part for you. (I'm sure there is a much more efficient way of writing this, but this is what I had time for). This will calculate the total area under the curve of f(x) = sqrt(x-1). This code has been validated with the following parameters:

Start: 1
End: 6
Number of trapezoids: 5

area = 7.264

Please read the comments in the code to understand what each line is doing. For detailed information about how this function is calculated (which may help understand how the program works) look here:

http://www.youtube.com/watch?v=1p0NHR5w0Lc

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
#include<iostream>
#include<cmath>
#include<cstdlib>

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

/*This function returns the value of  the point on the graph of f(x) >>
    ex.  receives the value of the start point -- then returns the value of f(x) of that point.
           then takes the value of the end point -- then returns the value of f(x) of that point*/
  double calcPt(double x)
  {
      //returns the value of f(x) -- ONE POINT AT A TIME
      return (sqrt(x-1)); //change this equation to whatever f(x) you want to evaluate.
  }

/*This function calculates the area of 1 of the slices of the graph of f(x)*/
  double trapArea(double pt1, double pt2, double deltaX)
  {
      //returns the area of 1 particular slice of the graph
      return ((pt1+pt2)/2)*deltaX;
  }

/*This function takes the points (the user inputs and calculated width) sent to it by the call from main()*/
  void check_f1(double width, double start, double finish)
   {
       //initialize the value of each variable used in the calculation
       double sum = 0, value1 = 0, value2 = 0, deltax = width;

          /*for loop that starts at the first point, finishes at the end point and is incremented by >>
              the width of each slice of the graph of f(x)*/
          for(double k=start; k<finish; k+=width)
          {
              //sets value1 = to the value of f(x) of the first point
               value1 = calcPt(start);
               //sets value2 = the value of f(x) of the first point PLUS the width of the slice
               value2 = calcPt(start+deltax);
               //sets sum = the current sum PLUS the area calculated from the function trapArea
               sum += trapArea(value1,value2,deltax);
               //move the left-hand edge of the slice 'width' distance down the graph to the beginning of the next slice
               start += deltax;
          }
          //print out the answer for the area under the curve of f(x) from a to b
          cout << sum << " is the area under a curve for sqrt(x-1)" << endl;
  }

  int main()
   {

     int choice_f;
     float a;
     float b;
     int n;
     double delta;

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


  if (choice_f == 1)
          cout << "Please select a starting point, a: ";
          cin >> a;

          cout << "Please select a end point, b: ";
          cin >> b;

          cout << "How many trapezoids do you want? ";
          cin >> n;

          /*After the user inputs the operation they want to perform, we need to do some preliminary calculations to determine >>
             the width of each slice of the graph that we want to analyze and send them to a function to finish the calculations*/

          delta = (b - a)/n; //takes the inputs and determines the width of each 'slice'

          //the check_f1 function is called and sent 3 parameters to run calculation based on user inputs

          check_f1(delta,a,b); //a = starting point
                                         //b = end point
                                         //delta = width of each slice

 return 0;
 }
Last edited on
Topic archived. No new replies allowed.