Help with Integration function

Pages: 123
Yes that makes sense.
Now I have this and it gets me stuck in my for loop. Can you see what is wrong?

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
double rect_integral(double a, double b, int n, int choice)
{

double (* fx) (double);

switch (choice)
{
        case 1: fx = func_1; break;
        case 2: fx = func_2; break;
        case 3: fx = func_3; break;
        case 4: fx = func_4; break;
        case 5: fx = func_5; break;
}

double height;
double x;
double total_area;
double width;
double area;


width = (b-a)/n;
total_area = 0;
height = fx(x);
x = a;

          for(x = a; x < b; x+width) {
              total_area+=fx(x);
           }
         cout << total_area;

}   


}
The reason why the loop gets stuck is this x+width should be x+=width

However, I don't recommend using floating-point values to control a for loop. Because of possible rounding errors, unexpected behaviour may occur. So I'd recommend using an integer, counting from 1 to n as preferable.


Line 24 isn't needed at that point (it belongs inside the loop, step 5.1).

I don't see where you do height * width inside the loop (step 5.2)?
My impression is that the height is the function.
Ok. I don't see anything * width inside the loop.

Remember, this is calculating the area of each rectangle.
Last edited on
okay so how should I make this for loop look?
Something like this:
for(int i=1; i<=n; i++)
would I be using i=1; i<=n; i++ ?
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
ouble rect_integral(double a, double b, int n, int choice)
{

double (* fx) (double);

switch (choice)
{
        case 1: fx = func_1; break;
        case 2: fx = func_2; break;
        case 3: fx = func_3; break;
        case 4: fx = func_4; break;
        case 5: fx = func_5; break;
}

double height;
double x;
double total_area;
double width;
double area;


width = (b-a)/n;
total_area = 0;
x = a;

          for(int i=1; i<=n; i++) {
              x = a;
              x+=width;
              total_area+=width*fx(x);
           }
         
return total_area;

}   



Does this code have everything required? The numbers do not seem right. Do I have all the steps from the steps you gave me?
I don't have much time right now. I think line 27 x = a; is not needed. (that is correct where it is at line 24).

See if that gives better results. When I have more time I'll take another look, but I think the rest looks very good.
Okay I will try that. Thank you again.

would you like to see all of my code? when you are not busy.
Hey, I was wondering how you declared func_1, func_2, etc. in the rectangle function? I'm doing something similar and it says they're not declared.
@ jdowning The rest of the code was listed on previous pages in this thread. But the main thing is the signature of the function pointer (return type and parameters) must match the actual function. In other respects there's nothing special to be done, other than declare the functions in the normal way.

@ bruntmjust
If you like, please post the full code you have so far. I hope it's working at least reasonably well so far.
@ Chervil
Thank you for all of your help! I am glad to say I got the code working pretty well! I am playing with the equations to make it better but it makes a lot more sense to me now what I am doing. I just wanted to thank you for the explanations and the examples.
I'm glad that you've got this working now - that's good to hear.

There are a couple of comments I'd like to make, one is to refer back to an earlier post, where I said this:
Note, mathematically the area of the rectangle may be calculated using the value of the function at its left or right edge. One will give an over-estimate, the other an under-estimate. You may get a more accurate result by using the mid-point. Do that by setting the initial value of x to be a + width/2. However, consider that as a refinement for later consideration.

I don't know whether you've considered or experimented with that.

The second comment is that when a program is more or less complete, in the sense that it is working, could be a good time to review the code, consider whether it can improved, for example by simplifying duplicated code, or by improving the execution time, as well as checking the style, such as consistent indentation and so on.

And not least, I might go back and re-read the original specifications to check that the program achieves precisely what was asked for - I'm not saying this is a problem here, it's just a part of the reviewing process.
Topic archived. No new replies allowed.
Pages: 123