3
$\begingroup$

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

$\endgroup$
3
  • 1
    $\begingroup$ I think you'll need to use SetDelayed. $\endgroup$
    – lericr
    Commented Jun 5, 2023 at 23:49
  • $\begingroup$ 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 $\endgroup$
    – csn899
    Commented Jun 5, 2023 at 23:51
  • 2
    $\begingroup$ 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). $\endgroup$
    – MarcoB
    Commented Jun 6, 2023 at 0:53

1 Answer 1

5
$\begingroup$
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

$\endgroup$
3

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