2

I have a piecewise function like this.

fP = Piecewise[{{x^2, x < 0}, {x, x > 0}}];
Plot[fFu, {x, -2, 2}]

Now I want to get the y value at a certain x position, e.g at x=1 the result should be 1.

But when I do

Print[fP[1]];

I just get the peacewise function itself as output and not the value.

Ilyssis
  • 121
  • 1

1 Answers1

1

You can allways use Functions (see How to Work with Variables and Functions)

f[x_] := x^2
Plot[f[x], {x, -3, 3}]

enter image description here

Plot[{f[x], Piecewise[{{f[x], x < 0}, {x, x > 0}}]}, {x, -2, 6}]

enter image description here

Piecewise[{{f[x], x < 0}, {x, x > 0}}]

enter image description here

f[4]

16

Piecewise[{{f[4], x < 0}, {x, x > 0}}]

enter image description here

Plot[{f[x], Piecewise[{{f[x], x < 0}, {x, x > 0}}]}, {x, -2, 6}, 
Epilog -> {Red, PointSize[Large], Point[{4, 16}], Dashed, 
Line[{{4, 0}, {4, 16}}], Line[{{0, 16}, {4, 16}}]}]

enter image description here

  • This doesn't address the question of how to extract the value from a piecewise function for a given x-value. You show how to get the value for f[4], when f is not a piecewise function, but that some thing doesn't work when it is. – Aaron Bramson Feb 22 '17 at 08:52