3

How is this type of piecewise function as follows in the picture represented and calculated?

enter image description here

Use:

Clear["Global`*"]
f[x_] = Piecewise[{{Cos[(\[Pi] x)/2], x <= 0}, {f[x - 1] + 1, x > 0}}]

This prompt appears:

enter image description here

The process of calculating the corresponding function value is as follows: such as f[2]

enter image description here

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
csn899
  • 3,953
  • 6
  • 13
  • 1
    I think you'll need to use SetDelayed. – lericr Jun 05 '23 at 23:49
  • In[5]:= Clear["Global`*"] f[x_] := Piecewise[{{Cos[([Pi] x)/2], x <= 0}, {f[x - 1] + 1, x > 0}}]

    In[7]:= f[2]

    Out[7]= 3

    – csn899 Jun 05 '23 at 23:51
  • 2
    As a general rule, I tend to write any function definition using SetDelayed as a matter of course, unless I have a special reason not to do so (and add a ClearAll[functionName] before the definition as well). – MarcoB Jun 06 '23 at 00:53

1 Answers1

5
Clear["Global`*"]

f[x_?NumericQ] :=
 Piecewise[{{Cos[(π x)/2], x <= 0}, {f[x - 1] + 1, x > 0}}]

Plot[f[x], {x, -6, 10}]

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198