1
$\begingroup$

I wanna be able to use the Piecewise function such that I don't have to manually enter all the pieces. For example, say I define

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

Consider now

Plot[Piecewise[{{f[x][[1]],0<x<1},{f[x][[2]],1<x<2},{f[x][[3]],2<x<3}},{x,0,3}]

I want to be able to do this using some kind of loop inside Piecewise. How can I implement this

$\endgroup$

1 Answer 1

5
$\begingroup$

Perhaps:

cond = # < x < # + 1 & /@ Range[0, 2];
f = {x, x^2, x^3};
Plot[Piecewise[Transpose[{f, cond}]], {x, 0, 3}]

enter image description here

$\endgroup$

Your Answer

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

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