Domains, Derivatives and Variables
In this section we’ll explore additional options and operations with functions in DoenetML. We’ll begin with domains!
Specifying the Domain of a Function
So far the functions we’ve used have been defined for all real numbers. If you want to restrict the domain of a function, you can use the domain attribute in the <function> tag. Any attempt to evaluate the function outside of its specified domain will return an empty result. In the example below, this happens for .
As of this writing, DoenetML supports any interval (open, closed, half open / half closed) as the domain of a single variable function. This includes intervals which stretch to infinity, e.g.
<function name="f" domain="[1,infinity)">1/x</function>Computing Derivatives
In DoenetML, we can compute the derivative of an elementary function using the <derivative> tag. The code
<derivative name="fPrime">$f</derivative>creates a new DoenetML function named fPrime which is the derivative of . The new function fPrime can be used in all the same ways as the original function, including evaluation using the $$ macro. Here’s an example.
If your original function has a specified domain, its derivative will have the same domain.
Functions with Different Variables
The default variable in a DoenetML function is
, but we can switch to a different variable using the variable attribute. This affects how the function is rendered on screen, and the <derivative> tag will use the correct variable as well.
Higher Order Derivatives
You can compute higher order derivatives through repeated use of <derivative> tags. For example, the following code defines a function along with its first and second derivatives.
<function name="f">x sin(x)</function>
<derivative name="fP">$f</derivative>
<derivative name="fPP">$fP</derivative>However, if you don’t need , you can jump straight to the second (or higher) derivative using the derivVariables attribute, as demonstrated in the following example.
Multivariable Functions and Partial Derivatives
For those familiar with multivariable calculus: now it’s time to tie all of the concepts in this section together! We can define a multivariable function using the variables attribute with <function>. For example,
<function name="f" variables="x y">x^2 cos(y)</function>defines the function in DoenetML. The input variables should be listed (in order) in the variables attribute, separated by spaces. “In order” means that, if we evaluate $$f(1,2), Doenet will set and because was listed first in the variables attribute, and second.
To compute partial derivatives, we’ll use the same derivVariables attribute from above. We can list one variable to specify which partial derivative to take. We can provide a list of variables to compute higher order (and mixed) partial derivatives.
Although it’s not demonstrated in these examples, you can also define a domain for multivariable functions. In the following definition,
<function name="f" variables="x y" domain="[0,1] (2,3)">x+y</function>the function f would be defined for and ; in other words, would be defined on . As of this writing, DoenetML only supports multivariable function domains which are Cartesian products of intervals.
Next Steps
At this point, you’ve learned all the basics of using functions in Doenet. Next up, we’ll learn how to get user input using sliders, and then move on to creating graphics with DoenetML code.