Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a function of two variables, f[x_, y_], and I would like to restrict the domain to values of x and y greater than zero. How do I do this?

I also want to plot the function for restricted values. I have tried to use RegionFunction -> Function[{x, y}, ...], but this doesn't work.

share|improve this question
4  
f[x_?Positive, y_?Positive] := ... –  The Toad Apr 21 '13 at 21:30
2  
3D plots, by the basic syntax of Plot3D, are restricted to a rectangular domain x ∈ {x_min, x_max} and y ∈ {y_min, y_max}. If doesn't work for you, please tell us what is the shape of the domain you have in mind. –  m_goldberg Apr 21 '13 at 23:09

2 Answers 2

You can also use Boole ... provided you are happy for your func to return 0 when the domain conditions are not met. For example:

f[x_, y_] := Boole[x > 0 && y > 0]  Cos[x] Sin[y] 

Plot3D[f[x, y], {x, -1, 2}, {y, -1, 2}]

Alternatively, you can set up a Piecewise function with explicit settings for what is to be returned (e.g. Indeterminate) when your conditions are not satisfied.

share|improve this answer

You may try "Putting constrains on patterns".

Mathematica provides a general mechanism for specifying constraints on patterns. All you need do is to put /; condition at the end of a pattern to signify that it applies only when the specified condition is True. You can read the operator /; as "slash-semi", "whenever" or "provided that".

Example:

Clear[f];
f[x_ /; x < 4] := x + 1;

or

f[x_] := x + 1 /; x < 4

Result:

(* defined *)
f[3]
4
(* undefined *)
f[5]
f[5]

Note:

In general, you can put /; condition at the end of any := definition or :> rule to tell Mathematica that the definition or rule applies only when the specified condition holds. Note that /; conditions should not usually be put at the end of = definitions or -> rules, since they will then be evaluated immediately.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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