So I'm supposed to create a program that calculates the area under the curve for f(x) = x^4+3 using rectangles and/or trapezoids. I have no clue how to do this at all. The program is supposed receive:
- Starting and ending points for the area
- Function/Procedure(s) for calculating the area, i.e. rect, trap, both
- Number of rectangles and/or trapezoids to use
And from there, it should calculate and print the area. Any help would be much appreciated!
You can create a function that, given 'x', will return 'y' using your given formula:
1 2 3 4 5 6
double f(double x)
{
double y = 0.;
... // do stuff to y using x here
return y;
}
Then you will need to, depending on user input, calculate the area under each piece of curve, and add the results to find the total area. You will most likely need a separate loop for each method.
1 2 3 4 5 6
double totalarea = 0.;
double increment = /*see note below*/;
for(int i = 0; i < n; ++i)
{
totalarea += ...; // Calculate and add the partial area to the total
}
I left out the value for 'increment' on purpose because you should be able to figure it out. (If you wanted to cut something of 'w' width into 'j' pieces, what would the separation between each cut be?) Now instead of 'w', use 'a' and 'b' and instead of 'j', use 'n'.
You can use Google or Wikipedia to find out what the Trapezoidal and Rectangular Methods are and use 'i', 'increment' and f() to find the partial areas.
This problem piqued my interest and I ended up writing a quick program to solve it. I'd like to share it but that'd spoil the fun for the person doing the homework. Oh well. Fun little exercise!
None of this makes any sense to me. I just don't understand programming well enough to do this assignment. It's already three days overdue and I still don't know what I'm doing.
Also, the math requirement for this class is trigonometry, which hasn't even reached the level of doing calculus in the first place. I can't imagine I should be expected to do math that I'm not required to know yet.
This requires no calculus; perhaps you don't actually understand the maths. Here is an algorithm for approximating the area under the curve between two points by splitting it into a number of rectangles.
Get starting and ending x values.
Calculate the difference between them. Divide that by the number of rectangles to use. This is the step size.
First rectangle area is the step size multiplied by (x^4+3) at the starting x value.
Second rectangle area is the step size multiplied by (x^4+3) at (the starting x value plus step size)
Third rectangle area is the step size multiplied by (x^4+3) at (the starting x value plus (2 * step size))
Fourth rectangle area is the step size multiplied by (x^4+3) at (the starting x value plus (3 * step size))
Keep going until you reach the final x value (which will be the finishing x value, minus step size).
Add all the rectangle areas.
Done.
You will note that all this has done is take a curve, and draw on it lots of rectangles of equal width, with the height of the rectangle just about level with the curve at that location.
#include<iostream>
#include<cmath>
usingnamespace std;
double f(float);
int main(){
float a;
float b;
int n;
float h;
float x;
float area;
cout << "What is the left x value? ";
cin >> a;
cout << "What is the right x value? ";
cin >> b;
cout << "How many pieces do you want to use? ";
cin >> n;
h = (b-a)/n;
area = (f(a)+f(b))/2;
for(int i=1;i<n;i++){
area+= f(a+i*h);
}
area = area*h;
cout << "The area under the curve of f(x) = x^4+3 is ";
}
double f(float x){
return x*x*x*x+3;
}
At this point, I'm just not getting any answer. I'm sure something is wrong with how I did this, but I don't know what. Anyone see any obvious problems?
I am going to add my own $0.02 for what its worth.
I agree with Moschops. This is more of a math problem than a programming problem and you seem to not understand the math. And the programming IS very straightforward once you understand the math.
Rather than jump directly into it your y = x^4 + 3 curve, try it with a straight line such as y = x + 2. Find the area under that line between, say, x=10 and x=20. Please don't say you don't follow that! It is finding the area of a rectangle and a triangle, HIGH SCHOOL STUFF! (You ARE in college, right?)
Once you write the program for the area under a straight line using Moschops' directions, you can apply the same principle to the curved line.
As for what you posted, I see what I consider to be errors but it may be your notation. Remember that area is width times height. The width of each rectangle is the value of 'h' that you compute on line 23. However, the height is the value of y or f(x). You don't compute that anywhere, except in your return statement. But you want to return the area under the curve, not the height of the rectangle. What you write on line 24 is the average value of y over the span of x=a to x=b. It is NOT the area of anything. And you don't define f() as a function so you will just get an error.
At this point, I'm just not getting any answer. I'm sure something is wrong with how I did this, but I don't know what. Anyone see any obvious problems?
Thanks for posting your code so far. The biggest problem I see is the indentation which starts ok but is mis-aligned at the end (This may be a forum problem). Anyway, let's re-align the text:
int main()
{
float a;
float b;
int n;
float h;
float x;
float area;
cout << "What is the left x value? ";
cin >> a;
cout << "What is the right x value? ";
cin >> b;
cout << "How many pieces do you want to use? ";
cin >> n;
h = (b-a)/n;
area = (f(a)+f(b))/2;
for (int i=1;i<n;i++) {
area += f(a+i*h);
}
area = area*h;
cout << "The area under the curve of f(x) = x^4+3 is ";
}
double f(float x){
return x*x*x*x+3;
}
So, the most obvious problem, function main() ends but you forgot to output the result at the end.
Insert after line 27
cout << area;
I coded a version of this myself, and your code looks quite different to mine. However, you actually get the same result that I do, so well done!
One other comment, I'd strongly recommend the use of double rather than float throughout the program. (I regard float as a relic from the days of 16-bit computers, and there's hardly any good reason to prefer it over double nowadays).
@chervil: You found that his program ran correctly?????
I took another look at it and found that I didn't look closely enough at lines 30-32. I didn't realize that is his function under which he is finding the area. My training puts such functions above the main() statement.
Still, I don't see how the correct area is being computed. Line 20 is a constant. He needs to change the height of the rectangle for each iteration, as in line 23. Since the areas of the rectangles are to be summed, I would have put line 26 inside the for loop.
Furthermore, the same variable, 'area', is defined 3 different ways; on lines 20, 23, and 26. If you choose a=1 and b=2, do you find an area close to 9.2?
@nathan10 I don't know now what the result would be. I tested the code, then deleted it.
Furthermore, the same variable, 'area', is defined 3 different ways; on lines 20, 23, and 26.
Look again.
Line 20 assigns an initial value.
Line 23 uses the += operator to accumulate each slice.
Line 26 multiplies the result by h. (Similar to using the *= operator)
I did say previously, "your code looks quite different to mine". Actually G3PO's solution is more efficient than mine, since rather than multiplying by h every time, it does that just once at the end.
While our solutions look very different, mathematically I think they are the same.
Those lines do what you say. I believe they represent 3 different concepts and therefore should be represented by 3 different variables.
Line 20 is the average height, or value, of the function between x=a and x=b.
Line 23 is the accumulation of the heights of each rectangle slice. Since he starts the loop at i=1, the OP is using the right border value of the function on each rectangle slice.
Line 26 computes the actual area under the curve using the association principle, multiplying each rectangle slice height by the constant rectangle width.
Lines 20 and 23 are not areas and shouldn't be labeled as such. I don't think the OP realizes that the rectangle slice heights are overstated by the average value of the function, and therefore the area under the curve is overstated. I think that error would be more apparent if those 3 variables had more appropriate names.
In writing this post I had to think through the program again. Because the loop starts at 1 and ends at n-1 rather than n, he misses the first rectangle slice. He would pick it up if the height variable had an initial value of f(a). That is the OP's error in line 20. I would write sliceheight = f(a);, change 'area' in line 23 to 'sliceheight', and change line 26 to area = sliceheight * h;.
One could also start the loop at 0, at set the initial value of the rectangle height to zero.
Maybe sketch out a simple example. Say you used a simple curve split into just two sections. The points of the two triangles would be the start point (a)/end point (b), the mid point of a->b and the result of potting the x value of the midpoint into the equation. From that you can work out the dimensions and then the area. Clearly this would yield a very inaccurate result but it might help you get the idea of what needs to be done.
#include <iostream>
usingnamespace std;
double f(double x);
int main()
{
double a;
double b;
int n;
double h;
double x;
double area;
a = 1;
b = 2;
n = 10000;
h = (b-a)/n;
area = (f(a)+f(b))/2;
for (int i=1;i<n;i++) {
area += f(a+i*h);
}
area = area*h;
cout << "The area under the curve of f(x) = x^4+3 is ";
cout.precision(12);
cout << area;
}
double f(double x){
return x*x*x*x+3;
}
... and here is the output:
The area under the curve of f(x) = x^4+3 is 9.20000002333
That's accurate to better than 1 part in 10000, so I don't believe any slices are omitted or double-counted.
Also on a side note area = area*h; can be written as area*=h; which is similar to the operator you have used already area +=f(a+i*h);. Just if you want to know.
1) initial height is 0.0, i starts at 0 and runs to n-1.
2) initial height is 0.0, i starts at 1 and runs to n.
3) initial height is 4.0, i starts at 1 and runs to n.
4) initial height is 11.5, [f(a) + f(b)]/2, i starts at 1 and runs to n-1.
I got areas of 9.19925 for 1); 9.20075 for 2); 9.20115 for 3); and the same area as you for 4), the OP's code. A version that has initial height of 4.0 and runs from i=1 through n-1, yields the same area as version 1).
I don't understand why the OP's code is the most accurate. I am not a trained mathematician and this result is counter-intuitive for me. I am wondering if having a starting value equal to the average function value accounts for the estimation error with each rectangle slice. Each slice will under- or over-estimate the true area depending on whether the height used is less than or greater than the true height of the function on that rectangle slice. What the OP has done may compensate for that.
I hope that a mathematician is reading this thread and can contribute an explanation.