21
$\begingroup$

I'm studying calculus and in some exercises I am asked to find the domain and range of a function. Does Mathematica have already a built-in function for this?

I can imagine some ways of doing so, but at the moment I want to know if there's a built-in function or some easy way of doing it.

$\endgroup$

5 Answers 5

22
$\begingroup$

When this question was originally asked, the closest thing to a built in function was via the built-in WolframAlpha functionality. As of V10, there's FunctionDomain and FunctionRange.

FunctionDomain[x + x/(x (x^2 - 1)), x]
(* Out: x < -1 || -1 < x < 1 || x > 1 *)

FunctionRange[x/(x (x^2 - 1)), x, y]
(* Out: y <= -1 || y > 0 *)

Both functions produce output that is often compatible with the groovy new NumberLinePlot function.

NumberLinePlot[%, {y, -2, 2}]

enter image description here

Original answer via WolframAlpha interface

Here's how to access domain and range queries via WolframAlpha.

WolframAlpha["domain of x+x/(x(x^2-1))",
 {{"Result", 1}, "Output"}]
WolframAlpha["range of x/(x(x^2-1))",
 {{"Result", 1}, "Output"}]
HoldComplete[x < -1 || -1 < x < 0 || 0 < x < 1 || x > 1]

HoldComplete[y < -1 || y > 0]
$\endgroup$
19
$\begingroup$

If you have a domain, you can often find a range using Interval.

Examples:

In[1]:= Sin@Interval[{0, 2 Pi}]
Out[1]= Interval[{-1, 1}]

In[2]:= Sin@Interval[{0, Pi}]   
Out[2]= Interval[{0, 1}]
$\endgroup$
2
  • $\begingroup$ I'd love to see this answer expanded. +1 $\endgroup$
    – Mr.Wizard
    Jul 7, 2012 at 9:32
  • $\begingroup$ @Mr.Wizard I think there are so many additional comments on the same thing that there is not much I can expand on. I was travelling again, so couldn't react earlier. $\endgroup$
    – Szabolcs
    Jul 9, 2012 at 7:10
11
$\begingroup$

Interval is a proper way for your task, but sometimes it is not the way to go. There are functions which can return neither their ranges nor domains, e.g. function yielding k-th non-trivial zero of the Riemman zeta function. E.g. ZetaZero[1000]//N yields 0.5 + 1419.42 I, while N[ ZetaZero @ Interval[{1000, 10000}]] yields ZetaZero[Interval[{1000, 10000}]], however since it is weakly monotonic on the critical line with respect to the imaginary part (i.e. 0.5 + t I, where t is a real number, )

N[ ZetaZero @ {1000, 10000}]
{0.5 + 49.7738 I, 0.5 + 236.524 I}

It is known that ( purely mathematically speaking) the domain of ZetaZero are integer numbers, but there are certain limitations of its implementation in Mathematica, e.g. there is no way to find its domain, only a kind of "divide and conquer" approach, e.g.

ZetaZero[10^7] // N

ZetaZero[10^7 + 1] // N
0.5 + 4.99238*10^6 I

ZetaZero::largp: Argument 10000001 in ZetaZero[10000001] is too large for this 
                   implementation. >>
ZetaZero[10000001]

There is a second argument in ZetaZero[k, t] representing the k-th zero with imaginary part greater than t. Neither this works with Interval, though it is Listable with respect to the second argument :

ZetaZero[105, Interval[{15., 35.5}]]
ZetaZero[105, {15., 35.5}]
ZetaZero[105, Interval[{15., 35.5}]]
{1/2 + 247.137 I, 1/2 + 253.07 I}  

There are similar issues with e.g. PrimePi ( the Mathematica counterpart of the prime counting function $\pi(x)$) and Prime.

PrimePi[ Interval[{10, 100}]]
PrimePi[ Interval[{10, 100}]]   

but

PrimePi[{10, 100}]
{4, 25}
Plot[PrimePi[x], {x, 10, 100}, AxesOrigin -> {0, 0}, PlotStyle -> Thick]

enter image description here

The maximal evaluatable argument for PrimePi is 25 10^13 -1, while with Prime there is a bigger problem, like e.g. an apparently extensible domain :

Prime @ {# + 1, #, # + 1} & 7783516045221
Prime::largp: Argument 7783516045222 in Prime[7783516045222] is too large for
this implementation. >>
{Prime[7783516045222], 249999997909357, 249999997909367}

You can find related details here : What is so special about Prime ?.

There are infinitely many primes, however values of $\pi(x)$ are known up to $x = 10^{23}$, look at e.g. Prime Counting Function on MathWorld .

$\endgroup$
6
  • $\begingroup$ Well, since these functions are expected to only take integer arguments, Interval[] is certainly not expected to interact nicely with them... $\endgroup$ Jul 7, 2012 at 9:50
  • $\begingroup$ Well, PrimePi evaluates on real arguments, e.g. PrimePi[1000. Pi] yields 446. $\endgroup$
    – Artes
    Jul 7, 2012 at 10:15
  • $\begingroup$ What do I need to know for understanding your answer? Your answer is amazing but I read it almost as if I were reading Japanese (I don't know japanese). Notice that this is not a critic, I just need references for understanding it. $\endgroup$
    – Red Banana
    Jul 7, 2012 at 21:55
  • 1
    $\begingroup$ @GustavoBandeira My intentions were 1.to explain certain limitations of using Interval. 2. to show limitations of built-in functions, especially with respect to their domains. 3. to demonstrate that sometimes representing of mathematical objects is far from high-fidelity. There are 4 links where you can read more on these issues. I think that careful reading of the M documentation concerning the functions which I mentioned would be very helpful. If there are other doubts you should specify them more strictly. I hope I could help more and possibly edit and update my answer. $\endgroup$
    – Artes
    Jul 7, 2012 at 22:11
  • 1
    $\begingroup$ @GustavoBandeira I think that MathWorld is an interesting repository of knowledge. If you have more detailed questions, try to ask them and I'll add a bit more extended clarifications. $\endgroup$
    – Artes
    Jul 7, 2012 at 22:21
10
$\begingroup$

Let me just add something about finding the domain:

You can often find the domain of a function f[x] using Reduce. Assuming the domain is a subset of the real numbers, the syntax would be

Reduce[Abs[f[x]] < Infinity, x, Reals]

Here are some examples:

(1)

A piecewise defined function:

Clear[f]; f[x_] := Piecewise[
  {
   {-1.5, x < 0},
   {3, x > 1},
   {Undefined, 0 < x < 1}
   }]

Plot[f[x], {x, -1, 2}]

piecewise

Reduce[Abs[f[x]] < Infinity, x, Reals]

(* ==> x <= 0 || x >= 1 *)

This means x has to be less than 0 or larger than 1.

(2)

A function with singularities:

Clear[f]; f[x_] := 1/Sin[x]

Plot[f[x], {x, -4 Pi, 4 Pi}]

inverseSine

Reduce[Abs[f[x]] < Infinity, x, Reals]

(*
==> 
C[1] \[Element] 
  Integers && (-Pi + 2 Pi C[1] < x < 2 Pi C[1] || 
   2 Pi C[1] < x < Pi + 2 Pi C[1])
*)

Last[%] /. C[1] -> n

(*
==> -Pi + 2 n Pi < x < 2 n Pi || 
 2 n Pi < x < Pi + 2 n Pi
*)

On the last line I've just made the conditions easier to read by introducing an integer n instead of the default constant C[1].

$\endgroup$
1
  • $\begingroup$ Haha. You also solved another doubt I had: "How to make piecewise defined functions on Mathematica" I just didn't ask it because I could solve using a manual method but here it is. Thanks. $\endgroup$
    – Red Banana
    Jul 7, 2012 at 21:50
2
$\begingroup$

In mathematics, a set can be write in two(or more) form: one is {3x | x in [0,1]}, the other {x | P(x)}, where P is called a propositional function or predicate. Since Mathematica have a great power of dealing with quantifiers, once you can write a set in the second form above, you can do plenty of things and tricks that you would have thought to have nothing to do with programming before. As we may write down the range of a function f in math this way: {f(x) | x in Dom f}, however, f(x) is not a "predicate", it's more closely to be thought as an object, so putting it into Mathematica programming might be difficult. What's lucky, we can write it using another way! That is{ y | ∃x such that y=f(x)}, where ∃x such that y=f(x) is a propositional function of x. From now, you can use the build-in functions Reduce to get the all possible values of y.

For example, if you have a function y = x^3 + x + 6 in math, and you want to find its range(w.r.t whole domain of f) or image of some proper set of its domain, try to use the the quantifier-family, ie Reduce, ForAll and Exists.

For example, here is the way to find the image of a set [1,15]

Reduce[Exists[x, 1 <= x <= 15, y == x^3 + x + 6], y, Reals]
Output: 8 <= y <= 3396

or find a range (w.r.t the whole domain of that function)

Reduce[Exists[x, y ==  (x^2+x)/(4-x)], y, Reals]
Output: y <= -9 - 4 Sqrt[5] || y >= 1/(-9 - 4 Sqrt[5])

After you become sophisticated with quantifiers, you are on a highly level of programming.

Finding the multiples of 6 among 1~100.

Reduce[Exists[x, x \[Element] Integers, y == 2 x] && 
  Exists[x, x \[Element] Integers, y == 3 x] && 
  1 <= y <= 100, y, Integers]
Output: y == 6 || y == 12 || y == 18 || y == 24 || y == 30 || y == 36 || y == 42 || y == 48 || y == 54 || y == 60 || y == 66 || y == 72 ||  y == 78 || y == 84 || y == 90 || y == 96

Finding the distance between a fixed point and an arbitrary curve.

This example is also very cool. Given a fixed point, take (3,4) for example and, an arbitrary curve defined by an equation, take x^3 + y^3 + 2 x^2 y - 7 x y^2 + 8 x - 7 y + 46 == 0. How to get the distance between the point and the graph?

First we need to insure that the point does not belong to the graph.

x^3 + y^3 + 2 x^2 y - 7 x y^2 + 8 x - 7 y + 46 == 0 /. {x -> 3, y -> 4}
Output: False

And we now try to get the set of every distance. This set collects all the distance from every points in the graph to the fixed point we picked.

Reduce[Exists[{x, y}, 
   x^3 + y^3 + 2 x^2 y - 7 x y^2 + 8 x - 7 y + 46 == 0 && 
   Sqrt[(x - 3)^2 + (y - 4)^2] == d], d, Reals]
Output: d >= Root[-3228313885871685449 + 3580719856462877084 #1^2 - 
        1341308868741195007 #1^4 + 229192171361605440 #1^6 - 
        21541588953050597 #1^8 + 1444606027764877 #1^10 - 
        58640497566169 #1^12 + 1358762455214 #1^14 - 17611698786 #1^16 + 
        102703185 #1^18 &, 6]

% //N
Output: d >= 1.33688

Perfect!

Another logic to do the same thing is:

N[Reduce[ForAll[{x, y}, 
  x^3 + y^3 + 2 x^2 y - 7 x y^2 + 8 x - 7 y + 46 == 0, 
  Sqrt[(x - 3)^2 + (y - 4)^2] >= dMin]], 10]

And we get: dMin <= 1.336884157

Have fun playing with quantifiers!

$\endgroup$
1
  • $\begingroup$ Also, Reduce[{1 <= x <= 15, y == x^3 + x + 6}, y, {x}, Reals], Reduce[ y == (x^2 + x)/(4 - x), y, {x}, Reals] $\endgroup$
    – matrix42
    Aug 16, 2014 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.