Using Desmos: Domains and Conditional Logic
Created: 2025/3/10

The Desmos Graphing Calculator allows you to specify domains, ranges, and simple conditional branching using curly braces {}.

Setting Ranges and Conditions

To define ranges or conditions for domains, ranges, or other expressions, you can use curly braces {} at the end of the expression.

You can also apply conditions to implicit functions.

1
y=x {x<0}y=x\ \{x<0\}
2
x=1 {y0}x=1\ \{y\geq 0\}
3
x>0 {x2+y2>4}x>0\ \{x^2+y^2>4\}

Setting Multiple Ranges and Conditions

To define multiple conditions, you can either separate them with commas inside a single set of curly braces {} or use multiple curly braces in parallel.

Consider the difference between the two expressions below:

1
x2+y2=1 {x>0,y>0}x^2+y^2=1\ \{x>0,\,y>0\}
2
x2+y2=4 {x>0}{y>0}x^2+y^2=4\ \{x>0\}\{y>0\}

When conditions are separated by commas inside a single set of curly braces, the graph is drawn if any of the conditions are satisfied. However, when conditions are written in parallel using multiple curly braces, the graph is drawn only if all conditions are satisfied.

Conditional Branching

You can define expressions for specific conditions using the format: { condition1:expression1, condition2:expression2, … }.

1
y={x<0:x+1,0x<1:1,1x:x}y=\{x<0:x+1,\,0\leq x<1:1,\,1\leq x:x\}
2
a=a=\cdots
3
(x1)2+(y+1)2={a<0:1,a>0:4}(x-1)^{2}+(y+1)^{2}=\{a<0:1,\,a>0:4\}

Complex conditions like "and" or "or" are not directly supported. Instead, you can rephrase them mathematically. For example:

a<1 and b>2a+1>0 and b2>0min(a+1,b2)>0\begin{align*}a<1\ \text{and}\ b>2 &\Leftrightarrow -a+1>0\ \text{and}\ b-2>0\\ &\Leftrightarrow \mathrm{min}(-a+1,\,b-2)>0\end{align*}

Alternatively, you can create complex conditional branching by nesting {}.

Details of {}

The two types of {} introduced earlier actually follow the same underlying rules.

Specifically, when using { condition1:expression1, condition2:expression2, … }, the behavior is as follows:

Behavior of Curly Braces {}

  1. Conditions are evaluated from left to right. The first satisfied condition determines the corresponding expression to use.
  2. If no expression is specified for a satisfied condition, it defaults to11.
  3. If none of the conditions are satisfied, the entire expression containing {} is not drawn or executed.

For example, the usage introduced earlier for setting ranges and conditions is a special case where the expression corresponding to the condition is omitted. When an expression is omitted, it defaults to 1. Consider the following example:

1
y=x {x<0}y=x\ \{x<0\}

In this expression, when x0x\geq 0, the condition is not satisfied, so the expression is not drawn. However, when x<0x<0, the condition {x<0}\{x<0\} evaluates to 1, resulting in:

1
y=x1y=x\cdot 1

As a result, this expression is drawn.

Now, consider another example:

1
x2+y2=4 {x>0}{y>0}x^2+y^2=4\ \{x>0\}\{y>0\}

Here, the graph is drawn only when both x>0x>0 and y>0y>0 are satisfied. This is because each condition evaluates to 1 when true, so the expression becomes:

1
x2+y2=411x^2+y^2=4\cdot 1\cdot 1

Thus, the graph is displayed only in the region where both conditions are met.

SHARE
Images are created with the Desmos Graphing Calculator, used with permission from Desmos Studio PBC.
© 2024 UNS Physics. All Rights Reserved.