# Define a function with a condition

I defined a function u[y].

Λ = -30;
u[η_] := (2*η - 2*η^3 + η^4) + Λ / 6*(η - 3*η^2 + 3*η^3 - η^4);
θ = Integrate[u[η]*(1 - u[η]), {η, 0, 1}] // N;
δ = 1/θ;
u[y_] := (2*y/δ - 2*(y/δ)^3 + (y/δ)^4) + Λ/6*((y/δ) - 3*(y/δ)^2 + 3*(y/δ)^3 - (y/δ)^4);


For values y / δ >1 the function has to return u[y] = 1. How to define this condition?

The result of the function for 0 < y < 80 has to be like the image:

• Check Piecewise or Condition. Note your code is difficult to read and could be much simplified. – anderstood Feb 1 '18 at 16:46
• How can I use greek letters in this forum in order to simplify the read? @anderstood – Mateus Feb 1 '18 at 16:53

Just add the definition (or condition) u[y_ /; y > δ] := 1:

Λ = -30;
u[η_] := (2*η - 2*η^3 + η^4) + Λ/6*(η - 3*η^2 + 3*η^3 - η^4);
θ = Integrate[u[η]*(1 - u[η]), {η, 0, 1}] // N;
δ = 1/θ;
u[y_ /; y > δ] := 1
u[y_] := (2*y/δ -2*(y/δ)^3 + (y/δ)^4) + Λ/6*((y/δ) - 3*(y/δ)^2 + 3*(y/δ)^3 - (y/δ)^4);


Thus:

Plot[u[y], {y, 0, 100}, PlotRange -> All]


You also can use Piecewise function to get the same plot:

u[y_] := Piecewise[{{1, y > δ}}, (2*y/δ - 2*(y/δ)^3 +(y/δ)^4) + Λ/6*((y/δ) - 3*(y/δ)^2 +
3*(y/δ)^3 - (y/δ)^4)]

• This condition will be applied to the derivative of u[y] as well ? – Mateus Feb 1 '18 at 18:06
• you mean that u'[y]=1 for $y>\delta$? – José Antonio Díaz Navas Feb 1 '18 at 18:08
• I think if you plot u'[y] it will not consider the condition (y>δ) for u[y]. It will be plot u'[y] based on u[y]without consider the condition for u[y](y>δ). – Mateus Feb 1 '18 at 18:14
• Yes, in that case, I suggest you use the definition via Piecewise – José Antonio Díaz Navas Feb 1 '18 at 18:28
• Perfect! Thank you! – Mateus Feb 1 '18 at 18:47
u2[y_] := u[Clip[y, {-∞, δ}]]
Plot[u2[y], {y, 0, 100}, PlotRange -> All]