Absolute Value Expressions

by Shelley Walsh ©2001

Simplifying expressions that have absolute values in them can sometimes be difficult for students, because the problems that textbooks have about them require you to be aware of the meaning of the expressions. You can't get away with just following a set procedure. For this reason they are good problems to test your understanding of notation. Here are some tricks for using Python to help you gain this understanding.

Type the following into the IDE.

>>> def f(x):
... return abs(x+2)+abs(x-5)
...

This is how you define in Python the function corresponding to the expression

|x+2|+|x-5|.

This means that if you were to type in f(2) on the next line, you would get 7 returned on the next line, because

|2+2|+|2-5|=|4|+|-3|=4+3=7.

This function definition is a very compressed way of telling the computer to do lots of things. You can write it that way because Python is a powerful computer language and has built into it the capability to read our standard order of operations conventions. If it wasn't so advanced, you might have to define the same operations one at a time, using several lines. To see how that would go, you could type in this.

>>> def g(x):
... a=x+2
... b=x-5
... c=abs(a)
... d=abs(b)
... return c+d
...

This is another way of writing the same function. No matter what number you plug into this g function you should get the same thing as if you plug it into the f function. Try it out on some numbers.


>>> f(3)
7
>>> g(3)
7
>>> f(2)
7
>>> g(2)
7
>>> f(9)
15
>>> g(9)
15

Perhaps you can see more clearly what is going on when you plug numbers into the f function by looking at the g function, which does the same thing. From this you can see that what the expression

|x+2|+|x-5|

does to a number x is, that it adds 2 to the number and puts that aside, and then it subtracts 5 from the number and puts that aside. Then it finds the absolute value of the first number it set aside and the absolute value of the second one and finally it adds the two together.

Thinking about it this way should help with doing the kinds of things that my Math 107 textbook requires of my students. Suppose we want to simplify this expression when 5<x<8. This means we want to write down expression that is simpler than this one, but agrees with it whenever x is between 5 and 8. What kinds of numbers do we mean by that? Can you give some examples of numbers x so that 5<x<8? What I am talking about here is numbers that are bigger than 5 and smaller than 8, so 6 would be one example, and another one would be 5.1. We can even use Python to check ourselves on these. Replace x with your candidate in 5<x<8, and if it gives you a 1, then it is a true statement and this is one of the numbers we are talking about. If it give you a 0, then it is not one of the numbers we are talking about.

>>> 5<6<8
1
>>> 5<0<8
0

5<7<8 is true. You can check that if you want, but you probably know it anyway. What happens if we substitute 7 for x. We can also do this manually with Python without using the f or g functions.

>>> x=7
>>> x+2
9
>>> a=x+2
>>> a
9
>>> abs(a)
9

Notice that because x+2 was positive taking the absolute value didn't change it. The same would be true for x-5=2. So at least for this x, x=7, we would get the same result if we left out the absolute value. Would this be true for any number between 5 and 8? What about 5.1? Yes, if you think about it a bit, you will realize that really for any number bigger than 5, both x-5 and x+2 are going to be positive, so the absolute values are not really necessary for 5<x<8, and also really the less than 8 part was totally unnecessary, but watch out, it might be important another problem.

To test out this idea with Python we can define a new function like f, but without the absolute values, and see if it agrees with f for various values in our interval.

>>> def f2(x):
... return (x+2)+(x-5)
...

We could also define it the long way like we did g.

>>> def g2(x):
... a=x+2
... b=x-5
... return c+d
...
>>> f2(6)
9
>>> f(6)
9
>>> f2(7)
11
>>> f(7)
11
>>> f2(1)
-1
>>> f(1)
7

Notice we get the same values for numbers bigger than 5, but different values for numbers less than 5.

Also now that the absolute values are gone we can simplify the function further and write a new function that should also give us the same values.

>>> def f3(x):
... return 2*x-3
...
>>> f3(7)
11
>>> f(7)
11
>>> f(6)
9
>>> f(5)
7
>>> f3(5)
7
>>> f(0)
7
>>> f3(0)
-3

It gives us the same values for x>5, but not when x<5.

Putting this all together, this means that our original expression

|x+2|+|x-5|

simplifies to

2x-3

for 5<x<8.

Actually we could say more. We could say that this simplification works for x>5, but the instructions of the problem only required finding out what it simplified to for 5<x<8, so that is all we needed to say.

Now lets do a slightly more difficult problem. Is there something we can simplify it to for 3<x<5?

Again we need to think about what kind of x's we are talking about. We are talking about numbers that a bigger than 3 and smaller than 5, like 4 or 3.1.

>>> 3<4<5
1
>>> 3<3.1<5
1
>>> 3<1<5
0
>>> 3<5<5
0

For numbers like that what can we say about |x+2| and |x-5|? Is there a way we can write them with using absolute value? For numbers like 4 or 3.1 or 4.9 what can we say about the sizes of x+2 and x-5?

>>> x=4
>>> x+2
6
>>> x=3.1
>>> x+2
5.0999999999999996

Don't worry about the round off error, still it is clear that for the size of numbers we are concerned with, x+2 is positive, absolute value won't change them.

>>> x=4
>>> x-5
-1
>>> x=3.1
>>> x-5
-1.8999999999999999

If x<5, and you subtract 5 from it, you will get a negative number.

>>> abs(x-5)
1.8999999999999999
>>> x-5
-1.8999999999999999
>>> -(x-5)
1.8999999999999999

A negative number is not equal to its absolute value, because absolute value removes the minus sign, but if you know for sure that an expression will be negative, you can accomplish the same thing by adding another minus sign. Putting this together, we can write an equivalent expression to our original expression by removing the absolute value from the x+2, and replacing it with a minus sign for the x-5.

>>> def f1(x):
... return (x+2)+-(x-5)
...

This function should agree with the original for the kinds of x's that we are interested in.

>>> f(4)
7
>>> f1(4)
7
>>> f(3.1)
7.0
>>> f1(3.1)
7.0
>>> f(6)
9
>>> f1(6)
7
>>> f(-3)
9
>>> f1(-3)
7

Notice this work for 4 and 3.1, but not for 6 and -3, but that's okay, because 6 and -3 are not between 3 and 5.

Notice also that there are a lot of 7's here. This is no coindidence. Notice that after this alternation our expression can be simplified to x+2-x+5=7. We can define a new function to reflect this.

>>> def f2(x):
... return 7
...
>>> f(4),f2(4)
(7, 7)

(This is a useful shortcut for checking to see if the functions are equivalent.)

>>> f(5),f2(5)
(7, 7)

It works for 5 too even though 5 is not in or interval, but that's okay. We are not requiring that it should only work for 3<x<5, just that it should work for those x's.

>>> f(9),f2(9)
(15, 7)

It doesn't work for 9, but that's okay, because we are not presently interested in 9.

>>> f(0),f2(0)
(7, 7)

It works for 0, but that doesn't matter.

So the end result is that for 3<x<5,

|x+2|+|x-5| =7


Now let's do something more advanced that we don't do in my Math 107 class, but shows up in some Calculus books as an exercise. Lets try to write an equivalent expression without using absolute values that works for *all* x's. We can do this by using a bracket kind of function, a function that is made by gluing together functions. What we need to do in order to do this is to look at all the possibilities of signs of x-5 and x+2.

>>> x=6
>>> abs(x-5)
1
>>> x-5
1
>>> x=5
>>> abs(x-5)
0
>>> x-5
0
>>> x=4.999
>>> abs(x-5)
0.001000000000000334
>>> x-5
-0.001000000000000334
>>> -(x-5)
0.001000000000000334

Whenever x>5, x-5>0, so absolute value doesn't effect it, but when x<5 it turns it into its opposite. If you want to be systematic about figuring this out, you can solve the inequality x-5>0 by the standard techniques of solving inequalities. Add 5 to both sides to get x>5.

Now what about |x+2|?

>>> x=0
>>> abs(x+2)
2
>>> x+2
2
>>> x=-2
>>> abs(x+2)
0
>>> x+2
0
>>> x=-2.0001
>>> abs(x+2)
0.00010000000000021103
>>> x+2
-0.00010000000000021103
>>> -(x+2)
0.00010000000000021103

Notice that whenever x>-2, absolute value doesn't effect x+2, but when x<-2, it has the same effect as taking the opposite.

Putting these together gives us 4 possibilities (but we are going to see that they will collapse to 3),

Case 1: x>5, x>-2

Both x-5 and x+2 are greater than or equal to 0, so they are not effected by absolute value and we get

|x+2|+|x-5|=(x+2)+(x-5)=2x-3

like in our first example problem. Since whenever x>5 it is certainly greater than -2, this condition can be simplified to simply x>5 .

Case 2: x>5, x<-2

This case is nonsense, it can't happen because a number that is greater than or equal to 5 cannot possible also be less than -2, which is why I said this reduces to 3 possibilities, but if there was such a strange number, we would get

|x+2|+|x-5|=-(x+2)+(x-5)=-7,

for such numbers. Of course we can tell this must be nonsense, because it would mean that the sum of two absolute values, which are positive, would be -7.

Case 3: x<5, x>-2:

This one is possible, and we can write it in shorter form as -2<x<5. In this case we get

|x+2|+|x-5|=(x+2)+-(x-5)=x+2+-x+5=7,

like in our second example.

Case 4: x<5, x<-2:

Since whenever x<-2 it is certainly less than 5, this case can be simplified to x<-2, and since both x+2 and x-5 are negative, we get

|x+2|+|x-5|=-(x+2)+-(x-5)=-x-2-x+5=-2x+3.

All together this means that if x<-2 it simplifies to -2x+3, if -2<x<5 it simplifies to 7, and if x>5 it simplifies to 2x-3.

>>> def f1(x):
... return 2*x-3
...

>>> def f2(x):
... return 7
...

>>> def f3(x):
... return -2*x+3
...
>>> f(-3),f3(-3)
(9, 9)
>>> f(-1),f2(-1)
(7, 7)
>>> f(6),f3(6)
(9, -9)
>>> f(6),f1(6)
(9, 9)

Then we can glue all these expression together as a bracket expression like this.

[under construction]

This means we substitute x into -2x+3 when it is less then -2, but we substitute it into 7 if it is greater than or equal to -2 and less than 5, and substitute it into 2x-3 if it is greater than or equal to 5.

We can even represent this function in Python, but it is a bit more complicated. We need to use branching.

>>> def g(x):
...    if x<-2:
...       return -2*x+3
...    elif x<5:
...       return 7
...    else:
...        return 2*x-3
...

Here elif is short for else if, and the logic goes like this. First it tests to see if x<-2. If that is true then it computes -2x+3. If that isn't true, in other words x>-2, then it checks to see if x<5. In that case we are in the -2<x<5 case, so it returns 7. If that isn't true, then x must be greater than or equal to 5, so it returns 2x-3, just like the bracket function describes. Now this function should agree with the f function no matter what we subtitute in it.

>>> f(-5),g(-5)
(13, 13)
>>> f(-2),g(-2)
(7, 7)
>>> f(0),g(0)
(7, 7)
>>> f(5),g(5)
(7, 7)
>>> f(10),g(10)
(17, 17)

Try it out on a few more values if you like. To avoid having to type so much in, we can define another function h that will give us both f(x) and g(x) at once for comparison.

>>> def h(x):
... return f(x),g(x)
...
>>> h(18)
(33, 33)
>>> h(-1)
(7, 7)
>>> h(2)
(7, 7)
>>> h(-3)
(9, 9)

Try it for a few more values, and then try doing the same thing for |x+3|+|x+5| for extra credit if you are in my Math 107 class.

Links

Python Home Page

Home Page

Questions and Comments