0

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.

adolzi
  • 383
  • 3
  • 11

2 Answers2

7

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[100]
f[200]
f[100]

129

See:

For plotting or mathematical operations consider Piecewise or ConditionalExpression:

Plot[
 ConditionalExpression[1 + 2^n, 2^n < x],
 {x, 0, 200},
 Frame -> True
]

enter image description here

Plot[
 Piecewise[{{1 + 2^n, 2^n < x}}],
 {x, 0, 200},
 Frame -> True
]

enter image description here

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 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 – adolzi Mar 20 '15 at 18:30
  • @adolzi Please add to the question examples of the actual input and output that you expect from your function. – Mr.Wizard Mar 21 '15 at 02:06
1

Do you mean this:

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

looking as

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

enter image description here

??

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96
  • 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 – adolzi Mar 20 '15 at 14:12