When I integrate from -a to 0, the sum of rectangles or the sum of trapezoids should be negative right?

My teacher didn't mention anything about restricting user input to [a,b] where a < b. If user inputs b < a, I included a simple swap vars test to prevent the program from integrating, say, from 10 to 0, resulting in negative result. That swap var step doesn't violates $\int_a^b = -\int_b^a$ or does?

Test ran: the simplest function f(x) = x

Better explanation: user chooses a, b and m, the only restriction is that m must be integer and positive.

First time I did (b - a)/m. If b > a, then that value is never negative. Program calculates $\int_0^{10} xdx = 50$ and $\int_{10}^0 xdx = -50$ with trapezoids, with rectangles it gets close, but it's never exact no matter if user inputs m = 1 trillion.

If user inputs 10 and 0, the result is -50. The guy who is going to grade the programs said "area cannot be negative, do |a - b|/m". Tried that, but for 10 to 0 case, program calculated 150 (it calculated the area between 10 to 20, because it started at 10 and moved to the right, to 20, rather than starting at 10 and going to the left, to 0).

Next attempt, inserted a swap a and b, if b < a (that's before (b - a)/m). Now it calculates $\int_0^{10} xdx = 50$ and $\int_{10}^0 xdx = 50$. I guess, swap should be done after (b - a)/m ? Because the sum of rectangles in this program can only start at "a" and go to the right, I didn't bother adding a second case, to do the sum right to left, starting at "b".

  • $\begingroup$ You do not supply details. I assume that for example for the "From $x$ to $0$, positive" you chose a particular positive number $c$, and used your program to approximate $\int_c^0 x\,dx$. You should have obtained a negative number. It is possible to write two types of programs (i) a program that for $\int_a^b f(x)\,dx$ "swaps" $a$ and $b$ if $b<a$, but then you need to change the sign of the answer at the end or (ii) a program that does not swap, but then you must use $\Delta x=(b-a)/n$, whatever the sign of $\Delta x$ happens to be. $\endgroup$ Oct 19, 2011 at 22:35
  • 1
    $\begingroup$ If your environment supports $\min$, $\max$, and $\mathrm{sign}$ functions, it may be more expedient for you to use those instead of fussing over swaps. $\endgroup$ Oct 20, 2011 at 0:21

2 Answers 2


You are failing to take into account the fact that $\int_a^b f(x) dx = -\int_b^a f(x) dx$. Here are the correct results for your test function $f(x) = x$:

If $a>0$, $$\int_0^a x dx = \left[\frac{x^2}2\right]_0^a = \frac{a^2}2 - 0 = \frac{a^2}2$$ is positive, but $$\int_a^0 x dx = \left[\frac{x^2}2\right]_a^0 = 0-\frac{a^2}2 = -\frac{a^2}2$$ is negative. $$\int_{-a}^0 x dx = \left[\frac{x^2}2\right]_{-a}^0 = 0-\frac{(-a)^2}2 = -\frac{a^2}2$$ is negative, but $$\int_0^{-a} x dx = \left[\frac{x^2}2\right]_0^{-a} = \frac{(-a)^2}2-0 =\frac{a^2}2$$ is positive.


$\int_a^b f(x) dx = -\int_b^a f(x) dx$ I think I see it now.

|b - a|/m this is right, because subdivision with negative values doesn't make sense, it'd result in rectangles with negative bases if user inputs a > b. If user inputs a < b, then |x| = x.

Next, if user inputted a > b, then, after |b - a|/m, program assigns the value of b in a. Why? Because the program can only calculate from the lower limit to the upper limit, it can't do it backwards, from the upper to the lower. Swapping a and b prevents the program from starting at the upper limit and calculating rectangles beyond, which resulted in integrals with value greater than it should be.

For the function that calculates it using the formula, not rectangles or trapezoids, added an IF case. First it calculates $\frac{\text{b}^2 - \text{a}^2}{2}$ and assigns the result in a var named "integral", then, if b < a, the function returns -integral instead of integral.

$$\int_0^{10} x dx = -\int_{10}^{0} x dx$$


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .