2
$\begingroup$

I'm trying to solve a more complex problem , however , this simple problem can represent well :

b := a;

s1[a_] := First @ NDSolve[{y'[x] == -b y[x], y[0] == 1}, y[x], {x, 0, 5}]

c = y[x] /. s1[a] /. x -> 5

s2[a_] := First @ NDSolve[{z'[x] == -c z[x], z[0] == 1}, z[x], {x, 0, 5}]

z[x] /. s2[1] /. x -> 5

I wish to solve the s1 equation as a function of b and later specify a only when the s2 equation is to be solved.

How can I solve this problem? Any suggestions will be appreciated!

$\endgroup$
  • 4
    $\begingroup$ Your example may be to simple. For what you have in the question, use ParametricNDSolve to obtain a solution in terms of b, and then insert the value of a for b. $\endgroup$ – bbgodfrey Mar 4 '16 at 4:46
  • $\begingroup$ Please could you show me how to make your suggestion in the above example ? $\endgroup$ – Jeiveison Gobério Maia Mar 4 '16 at 4:53
  • 1
    $\begingroup$ Almost the same example is explored in the "Basic Examples" of the documentation for ParametricNDSolve. $\endgroup$ – Michael E2 Mar 4 '16 at 12:08
  • $\begingroup$ @MichaelE2 Certainly, the s1 equation replicates one in the documentation. However, the s2 equation, added in a subsequent edit, differs in that it uses the s1 parametric solution as part of its ODE. Although one might argue that the difference is not significant, we both know from 108938 that Mathematica sometimes goes astray when combining functions. $\endgroup$ – bbgodfrey Mar 4 '16 at 14:46
  • $\begingroup$ @bbgodfrey If this question conveys a meaning to you, perhaps you could edit it to clarify. As it stands, there are too many ambiguities, and I would have to guess what is intended. Since the OP's setup fails at the first step, I thought my recommendation was a good place to start. $\endgroup$ – Michael E2 Mar 4 '16 at 17:17
4
$\begingroup$

If I understand your question correctly (and I may not), the solution is

s1 = First@ParametricNDSolve[{y'[x] == -b y[x], y[0] == 1}, y, {x, 0, 5}, b];
s2 = First@ParametricNDSolve[{z'[x] == -(y[a][5] /. s1) z[x], z[0] == 1}, 
    z, {x, 0, 5}, a];
z[1][5] /. s2
(* 0.966871 *)

Verification

Because this particular set of equations can be solved symbolically, the result just obtained can be verified by

s3 = First@DSolve[{y'[x] == -b y[x], y[0] == 1}, y, {x, 0, 5}] /. b -> a;
s4 = First@DSolve[{z'[x] == -(y[5] /. s3) z[x], z[0] == 1}, z, {x, 0, 5}];
N[z[5] /. s4 /. a -> 1]
(* 0.966871 *)

as desired.

$\endgroup$

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