Lesson goal: Graphing inequalities on the x-axis

Previous: Checking random numbers | Home | Next: Graphing inequalities (xy-plane)

In math class, sometimes you are asked to graph an inequality along a number line. Problems are often stated like "Graph $|x|>2$" or "Sketch $|x+2|<5$." These problems are often quite difficult and involve getting some logic of absolute values ironed out.

Turns out, they are a snap to solve on the computer using a for-loop, if statement, and some simple graphics using pset. Here's how this will work. We'll clear the screen, and draw the xy-coordinate grid in a white color. Then we'll select 1000 random values for $x$ between -250 and 250 (the width of the plotting screen). The if statement asks if the inequality is true..if so, we'll draw a small circle at the random $x$ coordinate chosen. The resulting graph will be the graphed inequality! Wow!

Now you try. Put some inequality relation in between the if and then. Try math.abs(x) > 20 to graph $|x|>20$. When the plot is finished, you can investigate it using the mouse pointer and the x,y coordinates over which you move it.

Type your code here:


See your results here:

The code here will not run. The true/false question in the if statement needs to filled in with the absolute value relation that you wish to plot. For example, if you want to graph the inequality $|x|<10$, then you'd program math.abs(x) < 10 in between the if and then statement. Dismiss.
Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.