0
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – bbgodfrey
    Mar 20, 2015 at 13:47
  • $\begingroup$ I think that you should be to see Condition $\endgroup$
    – Ukiyo-E
    Mar 20, 2015 at 13:47

2 Answers 2

7
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ 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 $\endgroup$
    – adolzi
    Mar 20, 2015 at 18:30
  • $\begingroup$ @adolzi Please add to the question examples of the actual input and output that you expect from your function. $\endgroup$
    – Mr.Wizard
    Mar 21, 2015 at 2:06
1
$\begingroup$

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

??

$\endgroup$
1
  • $\begingroup$ 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 $\endgroup$
    – adolzi
    Mar 20, 2015 at 14:12

Your Answer

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

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