# Scoping problem with NDSolve [closed]

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 == 1}, y[x], {x, 0, 5}]

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

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

z[x] /. s2 /. 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!

• 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. – bbgodfrey Mar 4 '16 at 4:46
• Please could you show me how to make your suggestion in the above example ? – Jeiveison Gobério Maia Mar 4 '16 at 4:53
• Almost the same example is explored in the "Basic Examples" of the documentation for ParametricNDSolve. – Michael E2 Mar 4 '16 at 12:08
• @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. – bbgodfrey Mar 4 '16 at 14:46
• @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. – Michael E2 Mar 4 '16 at 17:17

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

s1 = First@ParametricNDSolve[{y'[x] == -b y[x], y == 1}, y, {x, 0, 5}, b];
s2 = First@ParametricNDSolve[{z'[x] == -(y[a] /. s1) z[x], z == 1},
z, {x, 0, 5}, a];
z /. 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 == 1}, y, {x, 0, 5}] /. b -> a;
s4 = First@DSolve[{z'[x] == -(y /. s3) z[x], z == 1}, z, {x, 0, 5}];
N[z /. s4 /. a -> 1]
(* 0.966871 *)


as desired.