2
$\begingroup$

I want NDSolve to solve the problem $$y'(x)=0, y(0)=1, x\in[0,1] $$ and then use $y(x)$ to solve $$z'(x)=y(x), z(0)=0, x\in[0,1] $$.

This is a toy model of the real problem.

In the real problem the equations cannot be solved analytically nor in the same NDSolve.

Here is my attempt:

    (*First ODE gives y as output*)

s = NDSolve[{y'[x] == 0, y[0] == 1}, y, {x, 0, 1}]
yy[x_] := Evaluate[y[x] /. s]
Plot[yy[x], {x, 0, 2}, PlotRange -> All]

(*Second ODE must use y as input*)

s2 = NDSolve[{z'[x] == yy[x], z[0] == 0}, z, {x, 0, 1}]
zz[x_] := Evaluate[z[x] /. s2]
Plot[zz[x], {x, 0, 2}, PlotRange -> All]

Mathematica cannot manage this as it "is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing".

$\endgroup$
  • 1
    $\begingroup$ I'm curious as to why you can't just concatenate the arguments of the individual NDSolve calls (i.e., something like NDSolve[{y'[x] == 0, y[0] == 1, z'[x] == y[x], z[0] == 0}, ...]. I'm happy to take this as a constraint, but I just can't envision the problem that wouldn't allow it. $\endgroup$ – Michael Seifert Nov 15 '18 at 17:50
  • $\begingroup$ The solution should only be considered valid within the interval defined by the NDSolve, i.e., {x, 0, 1}. Consequently, plotting beyond that range with {x, 0, 2} should not be trusted outside the interval {0, 1}. $\endgroup$ – Bob Hanlon Nov 15 '18 at 18:32
0
$\begingroup$

This maybe?

s = NDSolve[{y'[x] == 0, y[0] == 1}, y, {x, 0, 1}]
yy[x_] := Evaluate[y[x] /. s] // First
Plot[yy[x], {x, 0, 2}, PlotRange -> All]

s2 = NDSolve[{z'[x] == yy[x], z[0] == 0}, z, {x, 0, 1}]
zz[x_] := Evaluate[z[x] /. s2]
Plot[zz[x], {x, 0, 2}, PlotRange -> All]
$\endgroup$
2
$\begingroup$

(I see that this is the solution suggested by @Michael, and the OP apparently thinks a single NDSolve can't be used. Perhaps the OP can provide an example showing why a single NDSolve can't be used?)

Why not just use one NDSolve (or even better, NDSolveValue)?

sol = NDSolveValue[{y'[x]==0, y[0]==1, z'[x]==y[x], z[0]==0}, {y,z}, {x,0,1}];

Visualization:

Plot[Evaluate @ Through @ sol @ t, {t, 0, 1}]

enter image description here

$\endgroup$
2
$\begingroup$

I' m a liitle bit to late, but the problem can be solved very easy using NDSolveValue two times:

Y = NDSolveValue[{y'[x] == 0, y[0] == 1}, y, {x, 0, 1}]  (* 1st solution Y[x] *)
Z = NDSolveValue[{z'[x] == Y[x], z[0] == 0}, z, {x, 0, 1}](*2nd solution Z[x]*)

plot both results:

Plot[{Y[x], Z[x]}, {x, 0, 2}, PlotRange -> All]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy