# Defining Piecewise Functions [duplicate]

I'm having trouble defining piecewise functions. What I have so far is a function that outputs something in a suitable format for Piecewise:

envpart = getpsipieces[LUs[[1]], LUs[[2]], eenv, ms, hbar]

{{(0.707118 + 0. I) E^((-0.138441 + 0. I) x) + (0.707082 + 0.00436397 I) E^((0.138441 + 0. I) x),
0 < x < 0.47619},
{(0.708113 + 0.0127916 I) E^((0. - 0.617118 I) (-0.47619 + x)) +
(0.709159 - 0.00813024 I) E^((0. + 0.617118 I) (-0.47619 + x)),
0.47619 < x < 0.524036},
{(0.755281 + 0.00032215 I) E^((-0.138441 + 0. I) (-0.524036 + x)) + (0.661991 +
0.00436804 I) E^((0.138441 + 0. I) (-0.524036 + x)),
0.524036 < x < 1.}}


What I want to be able to do is make a function that looks like

f[x_]:=Piecewise[envpart]


for simple plotting, integrating, etc.

When I try this, f[x0] returns Piecewise[envpart], f[x_]=Piecewise[envpart] works, but it locks up Piecewise and I want three of these types of functions working at once.

f[x_]=Piecewise[(writing out actual argument)] works perfectly, but one of the functions I want to build has hundreds of cases, so this isn't feasible.

Use Evaluate for this purpose:

 f[x_] := Evaluate@Piecewise[envpart]


Make sure that x has no value assigned. ClearAll[x] before will handle this.

This operation is described in Evaluate documentation. 3rd/4th example in Applications section.

Why it does not worki without it? x is scoped when creating the function and if you later provide envpart it's just different x. Evaluate provides it before it is scoped.

• This is exactly what I needed. Thanks for your quick response, concise solution and explanation. – user12308 Feb 11 '14 at 7:38
• @user12308 Please take a look at the link I've just found. Do not worry if question is closed. It will stay here as a link to the generic case. – Kuba Feb 11 '14 at 7:46