For 0<=t<=10, if 5t<=n<=5t+2, then f[n]=4n, and if 5t+3<=n<=5t+4, then f[n]=4n+1?

I would like then to use the specific values of f[n], for all n between 0 and 54 (t here is any integer between 0 and 10).

I tried:

f[n_] := Piecewise[{{4 t,     5 t     <= n <= 5 t + 2}, 
                    {4 t + 1, 5 t + 3 <= n <= 5 t + 4}}]

I tried f[5] (after Clear[t]). I got: f[5] is 4t if 5t<=5<=5t+2 and 4t+1 if 5t+3<=5<5t+4.

I would have liked to simply get 4.

  • $\begingroup$ Please post the code you tried, the output it gives you, and the output you would like to get instead. $\endgroup$ – Szabolcs Feb 1 '15 at 18:16
  • $\begingroup$ So you have actually f[n,t] instead of f[n]? $\endgroup$ – mgamer Feb 1 '15 at 18:22
  • 1
    $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) 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! $\endgroup$ – user9660 Feb 1 '15 at 18:25
  • $\begingroup$ I added the code... Thanks! I am new to this, Your help would be greatly appreciated. No, f is a function of n as I see it. $\endgroup$ – Lola Feb 1 '15 at 18:26
  • 1
    $\begingroup$ You say, "When I type f[5] I get 0", but this is actually not the case unless t was assigned a value. Can you give a complete example, including all relevant definitions? Do you have a definition for t? This site gives good guidelines on how to ask a question: sscce.org $\endgroup$ – Szabolcs Feb 1 '15 at 18:31

Let me know if this was what was intended:

c1 = {4 n, 5 # <= n <= 5 # + 2} & /@ Range[0, 10];
c2 = {4 n + 1, 5 # + 3 <= n <= 5 # + 4} & /@ Range[0, 10];
f[u_] := Piecewise[Join[c1, c2] /. n -> u]
DiscretePlot[f[x], {x, 0, 54}, PlotRange -> All]

enter image description here

|improve this answer|||||
  • $\begingroup$ This is great! It works! (typo: it should be 4# instead of 4n - will edit this myself). Thanks!!! $\endgroup$ – Lola Feb 2 '15 at 15:35

For 0<=t<=10, if 5t<=n<=5t+2, then f[n]=4t, and if 5t+3<=n<=5t+4, then f[n]=4t+1


I often use such defenition of condition-depending expressions, if I'm not going to differentiate or integrate them

|improve this answer|||||
  • $\begingroup$ Thanks! This is useful. The only thing is that I would really need a function f of n alone, without keeping track of the t. $\endgroup$ – Lola Feb 1 '15 at 21:12
  • $\begingroup$ I guess you can solve for t in terms of n and plug it in the function f[n,t], so I guess this will do it in this particular example. However, I am still curious on how would one define a function of n without solving for t. This will allow for a more complicated condition instead of 5t<=n<=5t+3... $\endgroup$ – Lola Feb 1 '15 at 21:31
  • $\begingroup$ f depends on two variables. $\endgroup$ – mgamer Feb 1 '15 at 22:01
  • $\begingroup$ No, f as I wrote it, is a function of n alone. In order to define it, I used the auxiliary variable t. In other words, what I wrote means: f[n]=0 if 0<=n<2, f[n]=1 if 3<=n<=4, f[n]=4 if 5<=n<=7, f[n]=5 if 8<=n<=9, f[n]=8 if 10<=n<=12, etc $\endgroup$ – Lola Feb 2 '15 at 2:45
  • $\begingroup$ I wanted to have an efficient way of defining this function. Something that would work when the range is much larger or the conditional more complicated. $\endgroup$ – Lola Feb 2 '15 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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