# Manipulate functions in different range

I'm trying to plot the following two functions in different color in one diagram with Manipulate. The first function in blue is

$$y=s$$ for $$x \in [0,\frac{d-rk}{1-r}]$$

$$y=\frac{d+rx-x}{r}$$ for $$x \in [\frac{d-rk}{1-r},k]$$

$$y=\frac{d+rk-x}{r}$$ for $$x \in [k,s]$$

and the second function in red is

$$y=d+rk-rx$$ for $$x \in [0,\frac{d+rk-k}{r}]$$

$$y=\frac{d-rx}{1-r}$$ for $$x \in [\frac{d+rk-k}{r},k]$$

$$y=\frac{d-rk}{1-r}$$ for $$x \in [k,s]$$

under the conditions of: $$y \in [0,s]$$, $$s \in [2,4]$$, $$d \in [0,1]$$, $$k \in [d,2d]$$, $$r \in [1-\frac{d}{k},\frac{d}{k}]$$.

My Mathematica code is the following:

Manipulate[Show[Plot[s, {x, 0, (d - r*k)/(1 - r)}, PlotStyle -> Blue], Plot[(d + r*x - x)/r, {x, (d - r*k)/(1 - r), k}, PlotStyle -> Blue], Plot[(d + r*k - x)/r, {x, k, s}, PlotStyle -> Blue], Plot[d + r k - r x, {x, 0, (d + r k - k)/r}, PlotStyle -> Red], Plot[(d - r*x)/(1 - r), {x, (d + r k - k)/r, k}, PlotStyle -> Red], Plot[(d - r*k)/(1 - r), {x, k, s}, PlotStyle -> Red]], {s, 2, 4}, {d, 0, 1}, {k, d, 2 d}, {r, 1 - d/k, d/k}]


which yields an error. Can anyone help please?

• The main problem is that the initial values of $k$ and $r$ lead to divide-by-zero situations. Using {k, 0.1, 2} and {r, 0.1, 2} will clear the error conditions. Also, add something like PlotRange -> { {-4, 4}, {-5, 5} } as an option to your Show command. Commented Dec 23, 2019 at 4:56
• Plot[s, {x, 0, (d - r*k)/(1 - r)}, PlotStyle -> Blue] what do you mean by this ? You are varying s as a Manipulate variable but you are plotting it inside plot. This does not make sense. Commented Dec 23, 2019 at 4:56

Clear["Global*"]


Define your functions using Piecewise

EDIT: Added hard limiters to y1 and y2 using Clip

Manipulate[
y1[x_] := Clip[Piecewise[{
{s, 0 <= x < (d - r*k)/(1 - r)},
{(d + r*x - x)/r, (d - r*k)/(1 - r) <= x < k},
{(d + r*k - x)/r, k <= x <= s}}], {0, s}];
y2[x_] := Clip[Piecewise[{
{d + r*k - r*x, 0 <= x < (d + r*k - k)/r},
{(d - r*x)/(1 - r), (d + r*k - k)/r <= x < k},
{(d - r*k)/(1 - r), k <= x <= s}}], {0, s}];
Plot[{y1[x], y2[x]}, {x, 0, s}, PlotStyle -> {Blue, Red},
Exclusions -> False],
{{s, 3}, 2, 4, 0.02, Appearance -> "Labeled"},
{{d, 1}, 0, 1, 0.01, Appearance -> "Labeled"},
{{k, 1.5 d}, d, Max[2 d, 0.01], Appearance -> "Labeled"},
{{r, 1 - d/k}, 1 - d/k, d/k, Appearance -> "Labeled"}]


• Thanks so much! Can you please help me once more in adding a restriction of $y1 \in [0,s]$ and $y2 \in [0,s]$ so that the diagram has both x-axis and y-axis symmetrical to each other? I added {y1,0,s},{y2,0,s}, but it is not working. In fact, the two functions are inverse to each other, so I would like to have both functions to appear symmetrical in the diagram.
– ppp
Commented Dec 23, 2019 at 13:03
• May I ask why 0.01, 0.02, and Max[2 d,0.01] are used? And what Exclusions -> False is doing?
– ppp
Commented Dec 23, 2019 at 13:26
• The 0.01 and 0.02 are used to define the step size in the controls. You can change them to whatever you want or eliminate them. Since k is restricted to the interval {d, 2d}; when d == 0 this would attempt and fail to define the control interval for k to {0, 0}. The Max is used to avoid this null interval. Commented Dec 23, 2019 at 14:32
• Exclusions -> False shows the steps at the Piecewise boundaries (discontinuities) rather than just showing a gap. If you prefer a gap, just eliminate the option. Commented Dec 23, 2019 at 15:33
• To eliminate the hard limiters remove Clip functions. To see only first quadrant, specify PlotRange -> {0, s}. To have axes with same length, since both plot ranges will be {0, s} use AspectRatio -> 1`. The documentation includes the various options for functions. Commented Dec 23, 2019 at 19:16