# How to restrict the domain of a function

I realize this question has been asked many times, but I am fairly new at Mathematica and the other answers are very complex, and certainly too complicated for this simple code I am trying to write.

I want to restrict the function below to be between -1 and 1. I do not want to restrict the plot; I want to restrict the function itself.

f[x_] := -(x + c)^2


Is there a simple way I can do this?

• Is this what you are after: reference.wolfram.com/language/ref/Condition.html? – Kuba Jan 15 '18 at 19:17
• So far I haven't had any luck with this method. I suppose I could be doing it wrong though. – Physika Jan 15 '18 at 19:25
• I prefer ConditionalExpression than Condition – Artes Jan 15 '18 at 19:55
• Possible duplicate of Can I limit PlotRange for 1 function in a Plot? – Artes Jan 15 '18 at 20:03
• Try this: f[x_]:=If[-1<=x<=1, -(x+c)^2, Nothing]. – Alexei Boulbitch Jan 15 '18 at 20:20

I think the easiest way is to use Condition which has the operator form /;.

f[x_ /; -1 <= x <= 1, c_] := -(x + c)^2


Then

With[{c = -1/4}, Plot[f[x, c], {x, -2, 2}]]


• This worked! Thank you! – Physika Jan 15 '18 at 23:45
• @Physika consider also reading (1835) – Mr.Wizard Jan 16 '18 at 7:40

Amplifying on m_goldberg's answer: The Condition can include a Message to use when the argument is outside the allowed interval.

ClearAll[f]

f::arg = "Argument value of 1 must be in the closed interval {-1, 1}.";

f[x_, c_] /; If[TrueQ[-1 <= x <= 1], True, Message[f::arg, x]] := -(x + c)^2


Then, an argument outside the interval generates a Message

f[2, c]


The Message can be turned Off

With[{c = -1/4}, Off[f::arg]; Plot[f[x, c], {x, -2, 2}]]