# How to use interpolating functions in NSolve with a piecewise function?

I have a system of two non-linear differential equations that are piecewise, the solutions of which I would like to use to obtain the values of a third variable that is also piecewise. I have trouble with this last step. Here is a general example:

ndsol = NDSolve[{x'[t] == Piecewise[{{f1[x[t],y[t]], h1[x[t],y[t]]>0},
{f2[x[t],y[t]]], h2[x[t],y[t])>0}}],y'[t] ==
Piecewise[{{f3[x[t],y[t]],h3[x[t],y[t]]>0}, {f4[x[t],y[t]]],
h4[x[t],y[t])>0}}], x == initx, y == inity}, {x, y}, {t, 0, 10}]


where $x[t]$ and $y[t]$ are state variables, and $f_1$ to $f_4$ and $h_1$ to $h_4$ are non-linear functions. Mathematically I know that this system yields unique solutions for $x[t]$ and $y[t]$ for each $t$ and initial condition.

This yields the interpolating functions

 {{x->InterpolatingFunction[{{0,10}},"<>"],y->InterpolatingFunction[{{0,10}},"<>"]}}


Then I get the solutions for $x[t]$ and $y[t]$ out

ndsolx[t_] = Evaluate[x[t] /. ndsol[]]
ndsoly[t_] = Evaluate[y[t] /. ndsol[]]


And now I want to use the two solutions to obtain the time path of another variable $z[t]$, which is given by

ndsolA = NSolve[A[t] ==  Piecewise[{g1[x[t],y[t]], k1[x[t],y[t]]>0 },
{g1[x[t],y[t],A[t]]+A[t], k2[x[t],y[t]]>0 }}] , A[t], Reals]


In ndsolA above I therefore replace $x[t]$ by its solution $ndsolx[t]$ and $y[t]$ by its solution $ndsoly[t]$.

Thus, I want to have Mathematica return me a function for $A[t]$, from $t=0$ to $t=10$, given the solutions that I found above for $x[t]$ and $y[t]$, and knowing that $A[t]=g_1[x[t],y[t]]$ in domain $k_1[x[t],y[t]]>0$ and $g_1[x[t],y[t],A[t]]=0$ over domain $k_2[x[t],y[t]]>0$.

The output that I get is very large, has strangely many solutions (there should be only one solution for $A[t]$ at each point in $t$) and I cannot plot it, e.g. with

ndsolAa[t_] = Evaluate[A[t] /. ndsolA[]]
Plot[ndsolAa[t], {t,0,10}]


Thus I suspect I am doing something wrong in ndsolA. I would be very grateful if someone could help out. Thank you!

• cant you do something like this: ndsolA = Piecewise[{A[t] -> g1[x[t], y[t]], k1[x[t], y[t]] > 0}, {NSolve[g1[x[t], y[t], A[t]] == 0, A[t]], k2[x[t], y[t]] > 0}] – george2079 Mar 23 '16 at 18:42
• Thank you for your quick comment. This helped me to find the way. I solved for each domain separately, and then wrote A[t_]:=Piecewise[...], where piecewise now includes the domain-specific solutions. – user2085 Mar 24 '16 at 9:07