For my computer engineering class, I was asked to write a C++ program to compute the area of y=0, y=x^3, and x=1 and display the results on the screen (And set a domain of x from 0 to 1 and intervals dx=0.001). How would one go about finding the area of the functions stated previously? Thank you.
Most Helpful Guy
I assume you're talking about the AREA under the function? Functions themselves don't technically have an area.
The operation that gives you the area under a curve of a function is called integration. You might have heard of it if you've taken any calculus courses.
There are actually a few ways to compute the definite integral of a function (look up numerical integration if you want to know more). The easiest one to conceptualize, and the one your teacher probably had in mind to use was something called a Riemann Sum:
Here's how it works:
We can easily calculate area of a rectangle (A = width * height), yeah? So the idea behind it is this: if we have some arbitrary function f (x), we can split the area under the curve between the points x=a and x=b into rectangles of width dx, and height of f (x). You simply calculate the area of each rectangle and add them all up and this will give you an approximation of the area under the curve. The smaller the value of dx, the more rectangles, and hence, the more accurate the approximation.
So given a function f (x) and the values a, b and dx we can compute the area under it by looping through the x values starting at a and ending at b, incrementing x by dx every iteration. For every value of x, calculate the area of that rectangle (f (x) * x) and adding it to a sum counter variable.
Hope that helps.
By the way: if you have programming questions you might want to try a proper programming forum like stackoverflow. com. GirlsAskGuys isn't the best place for computer questions.1