0
$\begingroup$

I define:

 f[x_] := Piecewise[{{5, 0 <= x < 10}, {g[x], 10 <= x < 15}, {h[x], x > 15}}]

Then I try to solve ODE:

 a = 0.7
 NDSolve[{g'[x] == Sin[x] + 2*With[{t = a*x}, Refine[f[t], 0 <= t < 10]], g[10] == 5},
 g[x], {x, 10, 14.2857}]

It gives $g[x]$ as InterpolatingFunction.

Then - plot it:

 Plot[f[x], {x, 0, 11}]

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.

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?

$\endgroup$
0

2 Answers 2

1
$\begingroup$
f[x_] := Piecewise[{{5, 0 <= x < 10}, {g[x], 10 <= x < 15}, {h[x], x > 15}}]

a = 0.7
NDSolve[{g'[x] == Sin[x] + 2*With[{t = a*x}, Refine[f[t], 0 <= t < 10]], g[10] == 5},
 g[x], {x, 10, 14.2857}]

g[x_] = g[x] /. % // First

Plot[f[x], {x, 0, 15}]

Plot

$\endgroup$
0
$\begingroup$
Clear[g];

f[x_] = Piecewise[{{5, 0 <= x < 10}, {g[x], 10 <= x < 15}, {h[x], x > 15}}]

enter image description here

a = 0.7;
Clear[g];
g[x_] = g[x] /. 
   NDSolve[{g'[x] == Sin[x] + 2*With[{t = a*x}, Refine[f[t], 0 <= t < 10]], 
      g[10] == 5}, g[x], {x, 10, 14.2857}][[1]];

Plot[f[x], {x, 0, 11}, AxesOrigin -> {0, 0}]

enter image description here

However, DSolve can be used rather than NDSolve

Clear[g];
g[x_] = g[x] /. 
  DSolve[{g'[x] == Sin[x] + 2*With[{t = a*x}, Refine[f[t], 0 <= t < 10]], 
     g[10] == 5}, g[x], x][[1]]

-95 + 10 x + Cos[10] - Cos[x]

Plot[f[x], {x, 0, 11}, AxesOrigin -> {0, 0}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.