Help with Integration function

Pages: 123
bruntmjust (49)
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;

}   


}
Chervil (1203)
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)?
bruntmjust (49)
My impression is that the height is the function.
Chervil (1203)
Ok. I don't see anything * width inside the loop.

Remember, this is calculating the area of each rectangle.
Last edited on
bruntmjust (49)
okay so how should I make this for loop look?
Chervil (1203)
Something like this:
for(int i=1; i<=n; i++)
bruntmjust (49)
would I be using i=1; i<=n; i++ ?
bruntmjust (49)
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?
Chervil (1203)
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.
bruntmjust (49)
Okay I will try that. Thank you again.

would you like to see all of my code? when you are not busy.
jdowning (9)
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.
Chervil (1203)
@ 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.
bruntmjust (49)
@ 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.
Registered users can post here. Sign in or register to post.
Pages: 123