# Function with condition

I am starting my adventure with Mathematica. I would like to know if is there possibility to create function described by formula for example f(x):=1+2^n where 2^n < x. I have problem with this condition. I tried something like f[x_]:=(1+2^n) && (2^n< x) but probably it's wrong way. I'll be gratefull for any help

Edit:

Ok, let's take the function described by f(x)=1+2^n where n is solution of inequality 2^(-n) <= |x-1| < 2^(1-n) (n is integer) and we can define f(1)=1.

so for example f(3)=1+2^(-1) because n=-1 is solution of 2^(-n) <= |3-1| < 2^(1-n).

I'd like to create a plot of this or similar function. I tried something like: f[x_]:= 1 + 2^(n) /; n = Reduce[{2^(-n) <= Abs[ x - 1] < 2^(1 - n)}, k, Integers] but without success. I don't know how correctly put n value.

• Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. Mar 20, 2015 at 13:47
• I think that you should be to see Condition Mar 20, 2015 at 13:47

The question of what you want to happen when the condition is not met will determine how you should proceed. If you wish the function to remain unevaluated try Condition:

f[x_] := (1 + 2^n) /; 2^n < x

n = 7;

f
f

f

129


See:

For plotting or mathematical operations consider Piecewise or ConditionalExpression:

Plot[
ConditionalExpression[1 + 2^n, 2^n < x],
{x, 0, 200},
Frame -> True
] Plot[
Piecewise[{{1 + 2^n, 2^n < x}}],
{x, 0, 200},
Frame -> True
] • Thank you, it's very useful. But I'd like to dynamically compute n value. I tried something like for example: f[x_] = 1 + 2^(n) /; n = Reduce[{2^(-n) <= Abs[ x - 1] < 2^(1 - n)}, k, Integers] But doesn't work correctly Mar 20, 2015 at 18:30
• @adolzi Please add to the question examples of the actual input and output that you expect from your function. Mar 21, 2015 at 2:06

Do you mean this:

f[x_] := 1 + 2^IntegerPart[x];


looking as

Plot[f[x], {x, 0, 3}, AxesLabel -> {"x", "f"}] ??

• Not exactly, but thank you, it's clever :) but I will change conditions flexible. For example I am going to create a plot of function described by formule f(x)=1+2^n where 2^n <= |x| < 2^(1-n) where n is Integer and extra f(0)=1 Mar 20, 2015 at 14:12