| bruntmjust (49) | |||
|
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:
Please help, thank you. | |||
|
|
|||
| Chervil (1203) | |
|
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/ | |
|
|
|
| bruntmjust (49) | |
| 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. | |
|
|
|
| Chervil (1203) | |
|
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. | |
|
|
|
| bruntmjust (49) | |
| 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. | |
|
|
|
| Chervil (1203) | ||||||||
Again, this can be broken down into several sub-tasks. Putting in all the equations is relatively simple:
Putting them as options for the user to pick from could be like this:
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:
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
|
||||||||
| bruntmjust (49) | |||
okay so I have gotten this far:
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. | |||
|
|
|||
| Chervil (1203) | |||||
|
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
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
|
|||||
| bruntmjust (49) | |
|
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]? | |
|
|
|
| Chervil (1203) | |||
Sorry about that. The character strings should be declared as constant, like this:
| |||
|
Last edited on
|
|||
| bruntmjust (49) | |
|
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? | |
|
|
|
| Chervil (1203) | |
| Your line numbering may be different to mine. What is line 52? | |
|
|
|
| bruntmjust (49) | |
|
line 52: char method = " " ; | |
|
|
|
| Chervil (1203) | |||
That resembles line 43 in my code. But my version reads like this:
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
|
|||
| bruntmjust (49) | |||
okay so I changed that and now my code is:
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? | |||
|
|
|||
| bruntmjust (49) | |
| Figured it out!!! I got it to compile so ignore my last post. sorry. | |
|
|
|
| aperio (15) | |
|
Here is another place to look for an answer. http://www.cplusplus.com/forum/beginner/91810/ | |
|
|
|
| bruntmjust (49) | |
| That does help also. The only problem I am having right now is creating the code to calculate the integral. | |
|
|
|
| aperio (15) | ||
That's the part I show in that post. | ||
|
|
||
| bruntmjust (49) | |||
so when I have:
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. | |||
|
|
|||