# Piecewise of different functions

You know, NDsolve coupled with Piecewise can be used to solve a series of discontinuous differential equations. But what if one of the function is different? For example:

$$z''(t)=\begin{cases}x(t)-(1+y(t)), & \text{if } x>(1+y) \\ \\ \\ x(t)+(1+y(t)), & \text{if } x<-(1+y) \end{cases}$$ $$\rm{and}$$ $$z'(t)=0, \rm{if } |x|<(1+y)$$ I tried something like below

xyz = First@NDSolve[{x''[t] == -2.25 Cos[1.5 t] - x[t] - x'[t],
x == 0, x' == 0,
y''[t] == -1.125 Cos[1.5 t] - 4 y[t] - y'[t],
y == 0, y' == 0,
z''[t] == Piecewise[{
{x[t] - (1 + y[t]), x[t] > 1 + y[t]},
{0, Abs[x[t]] <= 1 + y[t]},
{x[t] + (1 + y[t]), x[t] < -1 - y[t]}}],
z == 0},
{x, y, z}, {t, 0, 30}];


but have no idea how to correctly incorporate $z'(t)$ condition.

• Please write down the code you've tried Jan 23 '14 at 14:58
• @belisarius What f are you talking about? Jan 23 '14 at 16:55
• @SjoerdC.deVries I need a new pair of lenses Jan 23 '14 at 17:03

Without making any claims about whether the problem is well posed, it appears possible to make Mathematica compute a solution. Maybe I've taken too many liberties with the problem, but consider the following. $$z'(t) = \begin{cases} 0, & \text{if}~|x|<(1+y) \\ q(t), & \text{if}~|x|>(1+y) \end{cases}$$ $$q'(t)=\begin{cases} x(t)-(1+y(t)), & \text{if}~x>(1+y) \\ x(t)+(1+y(t)), & \text{if}~x<-(1+y) \\ 0, & \text{if}~|x|<(1+y) \end{cases}$$

The code below gives a result for $z(t)$ that looks like a staircase. Is that what you expect?

T = 30;
xySol = First@NDSolve[{
x''[t] == -2.25 Cos[1.5 t] - x[t] - x'[t], x == 0, x' == 0,
y''[t] == -1.125 Cos[1.5 t] - 4 y[t] - y'[t], y == 0, y' == 0}, {x, y}, {t, 0, T}];

zSol = First@NDSolve[{
q'[t] == Piecewise[{
{x[t] - (1 + y[t]), x[t] > (1 + y[t])},
{x[t] + (1 + y[t]), x[t] < -(1 + y[t])}} /. xySol, 0],
z'[t] == Piecewise[{
{0, Abs[x[t]] < (1 + y[t])}} /. xySol,
q[t]],
q == 0,z == 0}, {q, z}, {t, 0, T}]