Using parts of piecewise function - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-09-21T14:36:40Z https://mathematica.stackexchange.com/feeds/question/77199 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/77199 0 Using parts of piecewise function newt https://mathematica.stackexchange.com/users/25246 2015-03-13T13:18:49Z 2015-05-13T10:18:38Z <p>I define:</p> <pre><code> f[x_] := Piecewise[{{5, 0 &lt;= x &lt; 10}, {g[x], 10 &lt;= x &lt; 15}, {h[x], x &gt; 15}}] </code></pre> <p>Then I try to solve ODE: </p> <pre><code> a = 0.7 NDSolve[{g'[x] == Sin[x] + 2*With[{t = a*x}, Refine[f[t], 0 &lt;= t &lt; 10]], g == 5}, g[x], {x, 10, 14.2857}] </code></pre> <p>It gives \$g[x]\$ as InterpolatingFunction. </p> <p>Then - plot it:</p> <pre><code> Plot[f[x], {x, 0, 11}] </code></pre> <p>But it plots \$f[x]\$ only in {x, 0, 10}, where the function is defined manually. So, plotting \$g[x]\$ also doesn't give any result. I tried other definitions of Piecewise function (such as \$f\$), but it also didn't work. Solving ODE for \$f[x]\$ (not for \$g[x]\$) is helpless.</p> <p>So, my question is: Can I use different undefined parts of Piecewise function for calculations? If not, are there another possibilities to do this in Mathematica maybe? </p> https://mathematica.stackexchange.com/questions/77199/-/77203#77203 1 Answer by Karsten 7. for Using parts of piecewise function Karsten 7. https://mathematica.stackexchange.com/users/18476 2015-03-13T13:29:14Z 2015-03-13T13:34:17Z <pre><code>f[x_] := Piecewise[{{5, 0 &lt;= x &lt; 10}, {g[x], 10 &lt;= x &lt; 15}, {h[x], x &gt; 15}}] a = 0.7 NDSolve[{g'[x] == Sin[x] + 2*With[{t = a*x}, Refine[f[t], 0 &lt;= t &lt; 10]], g == 5}, g[x], {x, 10, 14.2857}] g[x_] = g[x] /. % // First Plot[f[x], {x, 0, 15}] </code></pre> <blockquote> <p><img src="https://i.stack.imgur.com/5EkQU.png" alt="Plot"></p> </blockquote> https://mathematica.stackexchange.com/questions/77199/-/77207#77207 0 Answer by Bob Hanlon for Using parts of piecewise function Bob Hanlon https://mathematica.stackexchange.com/users/9362 2015-03-13T14:11:01Z 2015-03-13T14:11:01Z <pre><code>Clear[g]; f[x_] = Piecewise[{{5, 0 &lt;= x &lt; 10}, {g[x], 10 &lt;= x &lt; 15}, {h[x], x &gt; 15}}] </code></pre> <p><img src="https://i.stack.imgur.com/3kAAR.png" alt="enter image description here"></p> <pre><code>a = 0.7; Clear[g]; g[x_] = g[x] /. NDSolve[{g'[x] == Sin[x] + 2*With[{t = a*x}, Refine[f[t], 0 &lt;= t &lt; 10]], g == 5}, g[x], {x, 10, 14.2857}][]; Plot[f[x], {x, 0, 11}, AxesOrigin -&gt; {0, 0}] </code></pre> <p><img src="https://i.stack.imgur.com/PJ3ER.png" alt="enter image description here"></p> <p>However, <code>DSolve</code> can be used rather than <code>NDSolve</code></p> <pre><code>Clear[g]; g[x_] = g[x] /. DSolve[{g'[x] == Sin[x] + 2*With[{t = a*x}, Refine[f[t], 0 &lt;= t &lt; 10]], g == 5}, g[x], x][] </code></pre> <blockquote> <p>-95 + 10 x + Cos - Cos[x]</p> </blockquote> <pre><code>Plot[f[x], {x, 0, 11}, AxesOrigin -&gt; {0, 0}] </code></pre> <p><img src="https://i.stack.imgur.com/wjSUY.png" alt="enter image description here"></p>